C++第四章习题

1:定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算。将运算符函数重载为非成员、非友元的普通函数。编程序,求两个复数之和。

#include

using namespace std;

class Complex

 {public:

  Complex(){real=0;imag=0;}

  Complex(double r,double i){real=r;imag=i;}

  double get_real();

  double get_imag();

  void display();

 private:

  double real;

  double imag;

 };

 

double Complex::get_real()

{return real;}

 

double Complex::get_imag()

{return imag;}

 

void Complex::display()

{cout<<"("<

 

Complex operator + (Complex &c1,Complex&c2)

{

 returnComplex(c1.get_real()+c2.get_real(),c1.get_imag()+c2.get_imag());

}

 

int main()

{Complex c1(3,4),c2(5,-10),c3;

 c3=c1+c2;

 cout<<"c3=";

 c3.display();

 return 0;

}

C++第四章习题_第1张图片

2:定义一个复数类Complex,重载运算符“+”、“-”,“*”,“/,使之能用于复数的加减乘除。运算符重载函数作为Complex类的成员函数。编程序,分别求两个复数之和、差、积、商。

#include

using namespace std;

class Complex

 {public:

  Complex(){real=0;imag=0;}

  Complex(double r,double i){real=r;imag=i;}

  Complex operator+(Complex &c2);

  Complex operator-(Complex &c2);

  Complex operator*(Complex &c2);

  Complex operator/(Complex &c2);

  void display();

 private:

  double real;

  double imag;

 };

 

Complex Complex::operator+(Complex &c2)

{Complex c;

 c.real=real+c2.real;

 c.imag=imag+c2.imag;

 return c;}

 

Complex Complex::operator-(Complex &c2)

{Complex c;

 c.real=real-c2.real;

 c.imag=imag-c2.imag;

 return c;}

 

Complex Complex::operator*(Complex &c2)

{Complex c;

 c.real=real*c2.real-imag*c2.imag;

 c.imag=imag*c2.real+real*c2.imag;

 return c;}

 

Complex Complex::operator/(Complex &c2)

{Complex c;

 c.real=(real*c2.real+imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);

 c.imag=(imag*c2.real-real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);

 return c;}

 

void Complex::display()

{cout<<"("<

 

int main()

{Complex c1(3,4),c2(5,-10),c3;

 c3=c1+c2;

 cout<<"c1+c2=";

 c3.display();

 c3=c1-c2;

 cout<<"c1-c2=";

 c3.display();

 c3=c1*c2;

 cout<<"c1*c2=";

 c3.display();

 c3=c1/c2;

 cout<<"c1/c2=";

 c3.display();

 return 0;

}

C++第四章习题_第2张图片

3:定义一个复数类Complex,运算重载符“+”,使之能用于复数的加法运算。参加运算的两个运算量可以都是类对象,也可以其中有一个是整数,顺序任意。如c1+c2,i+c1,c1+i均合法(设i为整数,c1,c2为复数)。编程序,分别求两个复数之和、整数和复数之和。

#include         //VC++时改为∶ #include

using namespace std;      //VC++时为取消此行

class Complex

 {public:

  Complex(){real=0;imag=0;}

  Complex(double r,double i){real=r;imag=i;}

  Complex operator+(Complex &c2);

  Complex operator+(int &i);

  friend Complex operator+(int&,Complex &);

  void display();

 private:

  double real;

  double imag;

};

 

Complex Complex::operator+(Complex &c)

{return Complex(real+c.real,imag+c.imag);}

 

Complex Complex::operator+(int &i)

{return Complex(real+i,imag);}

 

void Complex::display()

{cout<<"("<

 

Complex operator+(int &i,Complex&c)

{return Complex(i+c.real,c.imag);}

 

int main()

{Complex c1(3,4),c2(5,-10),c3;

 inti=5;

 c3=c1+c2;

 cout<<"c1+c2=";

 c3.display();

 c3=i+c1;

 cout<<"i+c1=";

 c3.display();

 c3=c1+i;

 cout<<"c1+i=";

 c3.display();

 return 0;

}

C++第四章习题_第3张图片

4:有两个矩阵ab。均为23列,求两个矩阵之和。重载运算符“+”,使之能用于矩阵相加。如c=a+b.

#include

using namespace std;

class Matrix                                          //定义Matrix

 {public:

Matrix();                                          //默认构造函数

  friend Matrix operator+(Matrix &,Matrix &);        //重载运算符“+

  void input();                                      //输入数据函数

  void display();                                    //输出数据函数

 private:

  int mat[2][3];

 };

 

Matrix::Matrix()                                      //定义构造函数

{for(int i=0;i<2;i++)

 for(int j=0;j<3;j++)

  mat[i][j]=0;

}

 

Matrix operator+(Matrix &a,Matrix&b)                //定义重载运算符“+”函数

{Matrix c;

 for(int i=0;i<2;i++)

  for(int j=0;j<3;j++)

    {c.mat[i][j]=a.mat[i][j]+b.mat[i][j];}

 return c;

}

void Matrix::input()                                   //定义输入数据函数

{cout<<"input value ofmatrix:"<

 for(int i=0;i<2;i++)

 for(int j=0;j<3;j++)

  cin>>mat[i][j];

}

 

void Matrix::display()                                //定义输出数据函数

{for (int i=0;i<2;i++)

{for(int j=0;j<3;j++)

  {cout<

   cout<

}

 

int main()

{Matrix a,b,c;

 a.input();

 b.input();

 cout<

 a.display();

 cout<

 b.display();

 c=a+b;                                         //用重载运算符“+”实现两个矩阵相加

 cout<

 c.display();

 return 0;

}

C++第四章习题_第4张图片

5:

#include

using namespace std;

class Matrix

 {public:

  Matrix();

  friend Matrix operator+(Matrix &,Matrix &);

  friend ostream& operator<<(ostream&,Matrix&);

  friend istream& operator>>(istream&,Matrix&);

 private:

  int mat[2][3];

 };

 

Matrix::Matrix()

{for(int i=0;i<2;i++)

 for(int j=0;j<3;j++)

  mat[i][j]=0;

}

 

Matrix operator+(Matrix &a,Matrix&b)

{Matrix c;

 for(int i=0;i<2;i++)

  for(int j=0;j<3;j++)

    {c.mat[i][j]=a.mat[i][j]+b.mat[i][j];

    }

 return c;

}

 

istream& operator>>(istream&in,Matrix &m)

{cout<<"input value ofmatrix:"<

 for(int i=0;i<2;i++)

 for(int j=0;j<3;j++)

  in>>m.mat[i][j];

 return in;

}

 

ostream& operator<<(ostream&out,Matrix &m)

{for (int i=0;i<2;i++)

 {for(int j=0;j<3;j++)

  {out<

   out<

 return out;

}

 

int main()

{ Matrix a,b,c;

 cin>>a;

 cin>>b;

 cout<

cout<

 c=a+b;

 cout<

 return 0;

}

C++第四章习题_第5张图片

6:

#include

using namespace std;

class Complex

 {public:

  Complex(){real=0;imag=0;}

  Complex(double r){real=r;imag=0;}

  Complex(double r,double i){real=r;imag=i;}

  operator double(){return real;}

  void display();

 private:

  double real;

  double imag;

 };

 

void Complex::display()

{cout<<"("<

 

int main()

{Complex c1(3,4),c2;

 double d1;

 d1=2.5+c1;

 cout<<"d1="<

 c2=Complex(d1);

 cout<<"c2=";

 c2.display();

 return 0;

}

C++第四章习题_第6张图片

7:

#include

using namespace std;

class Student

{public:

Student(int,char[],char,float);

  intget_num(){return num;}

 char * get_name(){return name;}

 char get_sex(){return sex;}

 void display()

  {cout<<"num:"<

 private:

  intnum;

 char name[20];

 char sex;

 float score;

};

 

Student::Student(int n,char nam[],chars,float so)

 {num=n;

 strcpy(name,nam);

 sex=s;

 score=so;

}

 

class Teacher

 {public:

 Teacher(){}

 Teacher(Student&);

  Teacher(int n,char nam[],char sex,float pay);

  void display();

 private:

  int num;

  char name[20];

  char sex;

  float pay;

};

 

Teacher::Teacher(int n,char nam[],chars,float p)

{num=n;

 strcpy(name,nam);

 sex=s;

 pay=p;

}

 

Teacher::Teacher(Student& stud)

 {num=stud.get_num();

 strcpy(name,stud.get_name());

 sex=stud.get_sex();

 pay=1500;}

 

void Teacher::display()

{cout<<"num:"<

 

 

int main()

{Teacherteacher1(10001,"Li",'f',1234.5),teacher2;

 Studentstudent1(20010,"Wang",'m',89.5);

 cout<<"student1:"<

 student1.display();

 teacher2=Teacher(student1);

 cout<<"teacher2:"<

 teacher2.display();

 return 0;

}

你可能感兴趣的:(C++第四章习题)