第四章习题



1.
#include<iostream>
using namespace std;
class complex
{
 public:
  double real;
  double imag;
  void get();
  void display();
  complex()
  {
   real=0;imag=0;
  }
};
void complex ::get()
{
 cin>>real>>imag;
}
void complex ::display()
{
 cout<<"("<<real<<","<<imag<<")";
}
 complex operator+(complex&c1,complex&c2)
{
 complex c;
 c.real=c1.real+c2.real;
 c.imag=c1.imag+c2.imag;
 return c;
}
int main()
{
 complex c1,c2,c3;
 cout<<"c1=";
 c1.get();
 cout<<"c2=";
 c2.get();
    c3=operator+(c1,c2);
    c3.display();
 }


2.
#include<iostream>
using namespace std;
class complex
{
 public:
  
  void get();
  void display();
  complex()//构造函数初始化
  { real=0;imag=0;}
 complex operator+(complex&c2);
 complex operator-(complex&c2);
 complex operator*(complex&c2);
 complex operator/(complex&c2);
  private:
  double real;
  double imag;
};
void complex ::get()
{
 cin>>real>>imag;
}
void complex ::display()
{
 cout<<"("<<real<<","<<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;
 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;
}
int main()
{
 complex c1,c2,c3;
 cout<<"c1=";
 c1.get();
 cout<<"c2=";
 c2.get();
    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();
 }


3.
#include<iostream>
using namespace std;
class complex
{
 public:
  void get();
  void display();
  complex()
  {
   real=0;imag=0;
  }
  complex(double r)//转化构造函数
  {
   real=r;imag=0;
  }
  complex operator+(complex&c2);
  private:
   double real;
         double imag;
};
void complex ::get()
{
 cin>>real>>imag;
}
void complex ::display()
{
 cout<<"("<<real<<","<<imag<<")";
}
  complex complex:: operator+(complex&c2)
{
 complex c;
 c.real=real+c2.real;
 c.imag=imag+c2.imag;
 return c;
}
int main()
{
 complex c1,c2,c3;
 cout<<"c1=";
 c1.get();
 cout<<"c2=";
 c2.get();
    c3=c1+c2;
    c3.display();
    int c;
    cout<<"d=";
    cin>>c;
    complex d(c);
    c3=c1+d;
    c3.display();
 }


4.
#include<iostream>
using namespace std;
class 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 of matrix:"<<endl;
  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<<mat[i][j]<<" ";

 cout<<endl;
}
}
int main()
{
  Matrix a,b,c;
  a.input();
  b.input();
  c=a+b;
  cout<<endl<<"Matrix c = Matrix a + Matrix b :"<<endl;
  c.display();
  return 0;
  }


5.
#include<iostream>
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&input,Matrix&mat)
{
  cout<<"input value of matrix:"<<endl;
  for(int i=0;i<2;i++)
  for(int j=0;j<3;j++)
 input>>mat.mat[i][j];
}
ostream& operator<<(ostream&output,Matrix&mat)
{
  for (int i=0;i<2;i++)
{
   for(int j=0;j<3;j++)
{
   output<<mat.mat[i][j]<<" ";

 cout<<endl;
}
}
int main()
{
  Matrix a,b,c;
  cin>>a;
  cin>>b;
  c=a+b;
  cout<<endl<<"Matrix c = Matrix a + Matrix b :"<<endl;
  cout<<c;
  return 0;
  }
6.
#include<iostream>
using namespace std;
class complex
{
 public:
  complex()//构造函数
  {real=0;imag=0;}
  complex(double r)//转化构造函数
  {real=r;imag=0;}
  operator double()//类型转换函数
  {return real;}
   double real;
         double imag;
};
int main()
{
 complex c1,c2; double d ,d1;
 cout<<"c1=";
 cin>>c1.real>>c1.imag;
 cout<<"d1=";
 cin>>d;
 d1=d+c1;
    cout<<"d1="<<d1<<endl;
    c2=complex(d1);
    cout<<"c2=("<<c2.real<<","<<c2.imag<<")";
 }


7.
#include<iostream>
using namespace std;
class student
{
 public:
  char name[10];
  char num[5];
  char sex[5];
  void get();
};
class teacher
{public:
  char name[10];
  char num[5];
  char sex[5];
  teacher()
  {
  }
  teacher(student& s)
  {
  strcpy(name,s.name);
  strcpy(num,s.num);
  strcpy(sex,s.sex);
  }
  void display();
};
void teacher::display()
{
 cout<<"请输出老师信息:";
 cout<<name[10]<<num[5]<<sex[5];
}
void student::get()
{
 cout<<"请输入学生信息: ";
 cin>>name[10]>>num[5]>>sex[5];
}
int main()
{
 student s1;
 s1.get();
 teacher t1;
 t1.display();
 return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值