01.1.#include <iostream>
02.using namespace std;
03.class Complex{
04.public:
05. Complex()
06. {
07. real=0;
08. imag=0;
09. }
10. Complex(double r,double i)
11. {
12. real=r;
13. imag=i;
14. }
15. double getreal()
16. {
17. return real;
18. }
19. double getimag()
20. {
21. return imag;
22. }
23. void display()
24. {
25. cout<<"("<<real<<"+"<<imag<<"i)"<<endl;
26. }
27.private:
28. double real;
29. double imag;
30.};
31.Complex operator +(Complex &c1,Complex &c2)
32.{
33. return Complex(c1.getreal()+c2.getreal(),c1.getimag()+c2.getimag());
34.}
35.int main()
36.{
37. Complex c1(1,2),c2(3,4),c3;
38. c3=c1+c2;
39. cout<<"c3=";
40. c3.display();
41. return 0;
42.}
<img src="https://img-blog.csdn.net/20150517164954206" alt="" />
01.2. #include <iostream>
02.using namespace std;
03.class Complex{
04.public:
05. Complex()
06. {
07. real=0;
08. imag=0;
09. }
10. Complex(double r,double i)
11. {
12. real=r;
13. imag=i;
14. }
15. Complex operator +(Complex &);
16. Complex operator -(Complex &);
17. Complex operator *(Complex &);
18. Complex operator /(Complex &);
19. void display()
20. {
21. cout<<"("<<real<<"+"<<imag<<"i)"<<endl;
22. }
23.private:
24. double real;
25. double imag;
26.};
27.Complex Complex::operator +(Complex &c2)
28.{
29. return Complex(real+c2.real,imag+c2.imag);
30.}
31.Complex Complex::operator -(Complex &c2)
32.{
33. return Complex(real-c2.real,imag-c2.imag);
34.}
35.Complex Complex::operator *(Complex &c2)
36.{
37. return Complex(real*c2.real,imag*c2.imag);
38.}
39.Complex Complex::operator /(Complex &c2)
40.{
41. return Complex(real/c2.real,imag/c2.imag);
42.}
43.int main()
44.{
45. Complex c1(2,4),c2(1,2),c3,c4,c5,c6;
46. c3=c1+c2;
47. cout<<"c3=c1+c2=";
48. c3.display();
49. c4=c1-c2;
50. cout<<"c4=c1-c2=";
51. c4.display();
52. c5=c1*c2;
53. cout<<"c5=c1*c2=";
54. c5.display();
55. c6=c1/c2;
56. cout<<"c6=c1/c2=";
57. c6.display();
58. return 0;
59.}
<img src="https://img-blog.csdn.net/20150517164920381" alt="" />
01.3.#include <iostream.h>
02.class Complex{
03.public:
04. Complex()
05. {
06. real=0;
07. imag=0;
08. }
09. Complex(double r,double i)
10. {
11. real=r;
12. imag=i;
13. }
14. Complex operator +(Complex &);
15. Complex operator +(int &);
16. friend Complex operator +(int &,Complex &);
17. void display()
18. {
19. cout<<"("<<real<<"+"<<imag<<"i)"<<endl;
20. }
21.private:
22. double real;
23. double imag;
24.};
25.Complex Complex::operator +(Complex &c2)
26.{
27. return Complex(real+c2.real,imag+c2.imag);
28.}
29.Complex Complex::operator +(int &i)
30.{
31. return Complex(real+i,imag);
32.}
33.Complex operator +(int &i,Complex &c3)
34.{
35. return Complex(i+c3.real,c3.imag);
36.}
37.int main()
38.{
39. int i=2;
40. Complex c1(2,4),c2(1,2),c3,c4,c5;
41. c3=c1+c2;
42. cout<<"c3=c1+c2=";
43. c3.display();
44. c4=c1+i;
45. cout<<"c4=c1+i=";
46. c4.display();
47. c5=i+c2;
48. cout<<"c5=i+c2=";
49. c5.display();
50. return 0;
51.}
<img src="https://img-blog.csdn.net/20150517164928166" alt="" />
01.4. #include <iostream.h>
02.class Matrix{
03.public:
04. Matrix();
05. friend Matrix operator +(Matrix &,Matrix &);
06. void input();
07. void display();
08.private:
09. int mat[2][3];
10.};
11.Matrix::Matrix()
12.{
13. for(int i=0;i<2;i++)
14. for(int j=0;j<3;j++)
15. mat[i][j]=0;
16.}
17.Matrix operator +(Matrix &a,Matrix &b)
18.{
19. Matrix c;
20. for(int i=0;i<2;i++)
21. for(int j=0;j<3;j++)
22. c.mat[i][j]=a.mat[i][j]+b.mat[i][j];
23. return c;
24.}
25.void Matrix::input()
26.{
27. for(int i=0;i<2;i++)
28. for(int j=0;j<3;j++)
29. cin>>mat[i][j];
30.}
31.void Matrix::display()
32.{
33. for(int i=0;i<2;i++)
34. for(int j=0;j<3;j++)
35. {cout<<mat[i][j]<<" ";
36. cout<<endl;}
37.}
38.int main()
39.{
40. Matrix a,b,c;
41. a.input();
42. b.input();
43. c=a+b;
44. c.display();
45. return 0;
46.}
<img src="https://img-blog.csdn.net/20150517164935888" alt="" />
01.5. #include <iostream.h>
02.class Matrix{
03.public:
04. Matrix();
05. friend Matrix operator +(Matrix &,Matrix &);
06. friend istream& operator >>(istream &,Matrix &);
07. friend ostream& operator <<(ostream &,Matrix &);
08.private:
09. int mat[2][3];
10.};
11.Matrix::Matrix()
12.{
13. for(int i=0;i<2;i++)
14. for(int j=0;j<3;j++)
15. mat[i][j]=0;
16.}
17.Matrix operator +(Matrix &a,Matrix &b)
18.{
19. Matrix c;
20. for(int i=0;i<2;i++)
21. for(int j=0;j<3;j++)
22. c.mat[i][j]=a.mat[i][j]+b.mat[i][j];
23. return c;
24.}
25.istream & operator >>(istream & in,Matrix &c)
26.{
27. for(int i=0;i<2;i++)
28. for(int j=0;j<3;j++)
29. in>>c.mat[i][j];
30. return in;
31.}
32.ostream & operator <<(ostream & out,Matrix &c)
33.{
34. for(int i=0;i<2;i++)
35. for(int j=0;j<3;j++)
36. {
37. out<<c.mat[i][j]<<" ";
38. cout<<endl;
39. }
40. return out;
41.}
42.int main()
43.{
44. Matrix a,b,c;
45. cin>>a;
46. cout<<a;
47. cin>>b;
48. cout<<b;
49. c=a+b;
50. cout<<c;
51. return 0;
52.}
<img src="https://img-blog.csdn.net/20150517165026763" alt="" />
01.6. #include <iostream>
02.using namespace std;
03.class Complex{
04.public:
05. Complex()
06. {
07. real=0;
08. imag=0;
09. }
10. Complex(double r)
11. {
12. real=r;
13. imag=0;
14. }
15. Complex(double r,double i)
16. {
17. real=r;
18. imag=i;
19. }
20. operator double()
21. {
22. return real;
23. }
24. void display()
25. {
26. cout<<"("<<real<<"+"<<imag<<"i)"<<endl;
27. }
28.private:
29. double real;
30. double imag;
31.};
32.int main()
33.{
34. int i=2;
35. Complex c1(2,4),c2;
36. double d1;
37. d1=2.5+c1;
38. cout<<"d1="<<d1<<endl;
39. c2=Complex(d1);
40. cout<<"c2="<<c2.display<<endl;
41. return 0;
42.}
<img src="https://img-blog.csdn.net/20150517164948430" alt="" />
01.7. #include <iostream>
02.using namespace std;
03.#include <cstring>
04.class Student{
05.public:
06. Student(int,char[],char,float);
07. int get_num()
08. {
09. return num;
10. }
11. char * get_name()
12. {
13. return name;
14. }
15. char get_sex()
16. {
17. return sex;
18. }
19. void display()
20. {
21. cout<<"num="<<num<<endl;
22. cout<<"name="<<name<<endl;
23. cout<<"sex="<<sex<<endl;
24. cout<<"score="<<score<<endl;
25. }
26.private:
27. int num;
28. char name[20];
29. char sex;
30. float score;
31.};
32.Student::Student(int n,char nam[],char s,float so)
33.{
34. num=n;
35. strcpy(name,nam);
36. sex=s;
37. score=so;
38.}
39.class Teacher{
40.public:
41. Teacher(){}
42. Teacher(Student &stud)
43. {
44. num=stud.get_num();
45. strcpy(name,stud.get_name());
46. sex=stud.get_sex();
47. pay=3000;
48. }
49. Teacher(int n,char nam[],char s,float p)
50. {
51. num=n;
52. strcpy(name,nam);
53. sex=s;
54. pay=p;
55. }
56. void display()
57. {
58. cout<<"num="<<num<<endl;
59. cout<<"name="<<name<<endl;
60. cout<<"sex="<<sex<<endl;
61. cout<<"pay="<<pay<<endl;
62. }
63.private:
64. int num;
65. char name[20];
66. char sex;
67. float pay;
68.};
69.//Teacher::Teacher(int n,char nam[],char s,float p){ num=n; strcpy(name,nam); sex=s; pay=p;}
70.//Teacher::Teacher(Student &stud){num=stud.get_num();strcpy(name,stud.get_name());sex=stud.get_sex();pay=3000;}
71.int main()
72.{
73. Teacher t1;
74. Student s1(1,"xian",'F',100);
75. cout<<"s1:"<<endl;
76. s1.display();
77. t1=Teacher(s1);
78. cout<<"t1:"<<endl;
79. t1.display();
80. return 0;
81.}
<img src="https://img-blog.csdn.net/20150517165040273" alt="" />