C++第四章

来源:互联网 发布:汉客拉杆箱怎么样知乎 编辑:程序博客网 时间:2024/06/05 21:50
  1. (1)#include<iostream>  
  2. using namespace std;  
  3. class complex{  
  4.     public:  
  5.         complex(){real=0;imag=0;}  
  6.         complex(double r,double i){real=r;imag=i;}  
  7.         void display();  
  8.         double real;  
  9.         double imag;          
  10. };  
  11. complex operator +(complex &c1,complex &c2)  
  12. {  
  13.     complex c;  
  14.     c.real=c1.real+c2.real;  
  15.     c.imag=c1.imag+c2.imag;  
  16.     return c;  
  17. }  
  18. void complex::display()  
  19. {  
  20.     cout<<"("<<real<<","<<imag<<"i)"<<endl;  
  21. }  
  22. int main()  
  23. {  
  24.     complex c1(3,4),c2(1,3),c;  
  25.     operator +(c1,c2);  
  26.     c=c1+c2;  
  27.     c.display();  
  28.     return 0;  
  29. }  

2)#include<iostream>
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;
 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;
}
void complex::display()
{
 cout<<"("<<real<<","<<imag<<"i)"<<endl;
}
int main()
{
 complex c1(3,4),c2(1,3),c;
 c=c1+c2;
 c.display();
 c=c1-c2;
 c.display();
 c=c1*c2;
 c.display();
 c=c1/c2;
 c.display();
 return 0;
}

 

 

(3)#include<iostream>
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;}
     friend complex operator +(complex c1,complex c2);
     //operator double(){return real;}
     void display();
 private:
  double real;
  double imag;
};
complex operator +(complex c1,complex c2)
{
 complex c;
 c.real=c1.real+c2.real;
 c.imag=c1.imag+c2.imag;
 return c;
}
void complex::display()
{
 cout<<"("<<real<<","<<imag<<"i)"<<endl;
}
int main()
{
 complex c1(3,4),c2(1,3),c;
 double i=2.5;
 c=c1+c2;
 c.display();
 c=c1+i;
 c.display();
 c=i+c1;
 c.display(); 
}

(4)#include <iostream.h>  

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()  

{  

 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;  

 c.display();  

 return 0;  

}  

(5)

include <iostream.h>  

class Matrix{  

public:   

    Matrix();  

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

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

    friend ostream& operator <<(ostream &,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 &c)  

{  

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

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

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

     return in;  

}  

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

{  

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

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

     {  

     out<<c.mat[i][j]<<" ";  

     cout<<endl;  

     }  

     return out;  

}  

int main()  

{  

 Matrix a,b,c;  

 cin>>a;  

 cout<<a;  

 cin>>b;  

 cout<<b;  

 c=a+b;  

 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;}
     complex(double r,double i){real=r;imag=i;}
     //friend complex operator +(complex c1,complex c2);
     operator double(){return real;}
     void display();

  double real;
  double imag;
};
void complex::display()
{
 cout<<"("<<real<<","<<imag<<"i)"<<endl;
}
int main()
{
 complex c1(3,4),c;double dl;
 double i=2.5;
 dl=c1+i;
 cout<<dl<<endl;
 c.real=dl;
 c.imag=c1.imag;
 c.display();
}

 

(7)

#include<iostream>
#include<string>
#include<cstring>
using namespace std;
class Student
{
public:
 int    num;
 char name[10];
 char   sex;
 Student(int n, char nam[],char s):num(n),sex(s){
  strncpy(name, nam,10);
 }
 void display();
};
class Teacher
{
public:
    Teacher(Student &s):num(s.num),sex(s.sex)
 {
  //num=s.num;
  strcpy(name,s.name);
  //sex=s.sex;
 }
    void display();
private:
    int num;
    char name[10];
    char sex;
};
void Student::display()
{
 cout<<"nun:"<<num<<endl; 
 cout<<"name:"<<name<<endl;
 cout<<"sex:"<<sex<<endl;
}
void Teacher::display()
{
 cout<<"nun:"<<num<<endl;
 cout<<"name:"<<name<<endl;
 cout<<"sex:"<<sex<<endl;
}
int main()
{
 Student s(10,"zhangsan",'m') ;
 s.display();
 Teacher t1(s);
 t1.display();
 return 0;
}

 



0 0
原创粉丝点击