第一题
#include "iostream"
using std::cin;
using std::cout;
using std::endl;
using std::ostream;
using std::istream;
class Complex
{
private:
public:
double rel;
double img;
Complex(double=0.0,double=0.0);
};
Complex::Complex(double x,double y)
:rel(x),img(y)
{
}
Complex operator + (Complex &c,Complex &i)
{
return Complex(c.rel+i.rel,c.img+i.img);
}
ostream& operator <<(ostream &output,const Complex &c)
{
output<<"("<<c.rel<<"+"<<c.img<<"i)"<<endl;
return output;
}
int main(int argc, char const *argv[])
{
Complex A(1,2);
Complex B(3,2);
cout<<(A+B)<<endl;
return 0;
}
第二题
#include "iostream"
using std::cin;
using std::cout;
using std::endl;
using std::ostream;
using std::istream;
class Complex
{
private:
double rel;
double img;
public:
Complex(double=0.0,double=0.0);
Complex operator + (Complex &i);
Complex operator - (Complex &i);
Complex operator * (Complex &i);
Complex operator / (Complex &i);
friend ostream& operator <<(ostream &output,const Complex &c);
};
Complex::Complex(double x,double y)
:rel(x),img(y)
{
}
Complex Complex::operator + (Complex &i)
{
return Complex(rel+i.rel,img+i.img);
}
Complex Complex::operator - (Complex &i)
{
return Complex(rel-i.rel,img-i.img);
}
Complex Complex::operator * (Complex &i)
{
return Complex((rel*i.rel-img*i.img),(rel*i.img+img*i.rel));
}
Complex Complex::operator / (Complex &i)
{
double a=rel;
double b=img;
double c=i.rel;
double d=i.img;
return Complex((a*c+b*d)/(c*c-d*d),(b*c-a*d)/(c*c-d*d));
}
ostream& operator <<(ostream &output,const Complex &c)
{
output<<"("<<c.rel<<"+"<<c.img<<"i)";
return output;
}
int main(int argc, char const *argv[])
{
Complex A(1,1);
Complex B(2,3);
cout<<A<<"+"<<B<<"="<<(A+B)<<endl;
cout<<A<<"-"<<B<<"="<<(A-B)<<endl;
cout<<A<<"*"<<B<<"="<<(A*B)<<endl;
cout<<A<<"/"<<B<<"="<<(A/B)<<endl;
return 0;
}
第三题
#include "iostream"
using std::cin;
using std::cout;
using std::endl;
using std::ostream;
using std::istream;
class Complex
{
private:
double rel;
double img;
public:
Complex(double=0.0,double=0.0);
Complex operator + (Complex &i);
Complex operator + (double i);
friend Complex operator + (double i,const Complex &c);
friend ostream& operator <<(ostream &output,const Complex &c);
};
Complex::Complex(double x,double y)
:rel(x),img(y)
{
}
Complex Complex::operator + (Complex &i)
{
return Complex(rel+i.rel,img+i.img);
}
Complex Complex::operator + (double i)
{
return Complex(rel+i,img);
}
Complex operator + (double i,const Complex &c)
{
return Complex(c.rel+i,c.img);
}
ostream& operator <<(ostream &output,const Complex &c)
{
output<<"("<<c.rel<<"+"<<c.img<<"i)";
return output;
}
int main(int argc, char const *argv[])
{
Complex A(1,1);
Complex B(2,3);
double i=1.2;
cout<<A<<"+"<<B<<"="<<(A+B)<<endl;
cout<<A<<"+"<<i<<"="<<(A+i)<<endl;
cout<<i<<"+"<<A<<"="<<(i+A)<<endl;
return 0;
}
第四题
第五题
#include "iostream"
using std::cin;
using std::cout;
using std::endl;
using std::ostream;
using std::istream;
class Matrix
{
private:
int a[2][3];
public:
Matrix operator + (const Matrix &m);
friend ostream& operator << (ostream &output,const Matrix &m);
friend istream& operator >> (istream &input,Matrix &m);
};
Matrix Matrix::operator + (const Matrix &m)
{
Matrix m1;
for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
{
m1.a[i][j]=a[i][j]+m.a[i][j];
}
}
return m1;
}
ostream& operator << (ostream &output,const Matrix &m)
{
for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
{
output<<m.a[i][j]<<" ";
}
output<<endl;
}
return output;
}
istream& operator >> (istream &input,Matrix &m)
{
for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
{
input>>m.a[i][j];
}
}
return input;
}
int main(int argc, char const *argv[])
{
Matrix A;
Matrix B;
cin>>A;
cin>>B;
cout<<"A+B="<<endl;
cout<<(A+B)<<endl;
return 0;
}
第六题
#include "iostream"
using std::cin;
using std::cout;
using std::endl;
using std::ostream;
using std::istream;
class Complex
{
private:
double rel;
double img;
public:
Complex(double=0.0,double=0.0);
friend Complex operator + (const Complex &c,const double i);
operator double();
friend ostream& operator << (ostream &output,const Complex &c);
};
Complex::Complex(double x,double y)
:rel(x),img(y){}
Complex operator + (const Complex &c,const double i)
{
return Complex(c.rel+i,c.img);
}
Complex::operator double()
{
return rel;
}
ostream& operator << (ostream &output,const Complex &c)
{
output<<"("<<c.rel<<"+"<<c.img<<"i"<<")";
return output;
}
int main(int argc, char const *argv[])
{
Complex A(1.1,2.1);
cout<<"A="<<A<<endl;
cout<<"A+3.1="<<(A+3.1)<<endl;
cout<<"(double)(A+3.1)="<<(double)(A+3.1)<<endl;
return 0;
}
第七题
#include "iostream"
#include "string"
using std::cin;
using std::cout;
using std::endl;
using std::string;
class Student
{
private:
string name;
int age;
string sex;
public:
Student();
Student(string,int,string);
string Get_Name() const;
int Get_age() const;
string Get_sex() const;
};
Student::Student()
{}
Student::Student(string na,int ag,string se)
:name(na),age(ag),sex(se)
{}
string Student::Get_Name() const
{
return name;
}
int Student::Get_age() const
{
return age;
}
string Student::Get_sex() const
{
return sex;
}
class Teacher
{
private:
string name;
int age;
string sex;
public:
Teacher();
Teacher(const Student&);
Teacher(string,int,string);
string Get_Name() const;
int Get_age() const;
string Get_sex() const;
};
Teacher::Teacher()
{}
Teacher::Teacher(const Student &S)
:name(S.Get_Name()),age(S.Get_age()),sex(S.Get_sex())
{}
Teacher::Teacher(string na,int ag,string se)
:name(na),age(ag),sex(se)
{}
string Teacher::Get_Name() const
{
return name;
}
int Teacher::Get_age() const
{
return age;
}
string Teacher::Get_sex() const
{
return sex;
}
int main(int argc, char const *argv[])
{
Student s1("nihao",18,"M");
cout<<"student:"<<endl;
cout<<s1.Get_Name()<<endl;
cout<<s1.Get_age()<<endl;
cout<<s1.Get_sex()<<endl;
Teacher t1(s1);
cout<<"teacher:"<<endl;
cout<<t1.Get_Name()<<endl;
cout<<t1.Get_age()<<endl;
cout<<t1.Get_sex()<<endl;
return 0;
}