第一题
#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);
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;
}
void Complex::display()
{
cout << "(" << real << "," << imag << "i)" << endl;
}
int main()
{
Complex a(4, 5), b(3, 1), c;
c = a + b;
cout << "a = " ; a.display() ;
cout << "b = " ; b.display();
cout << "a + b = " ; c.display();
return 0;
}
<img src="https://img-blog.csdn.net/20150622200952610?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbG9uZ3hpdWh1YQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);" alt="" />
第二题
#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 - imag * c2.imag;
c.imag = real * c2.imag + imag * c2.real;
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 << "(" << real << "," << imag << "i)" << endl;
}
int main()
{
Complex a(4, 5), b(3, 1), c, d, e, f;
c = a + b;
d = a - b;
e = a * b;
f = a / b;
cout << "a = " ; a.display() ;
cout << "b = " ; b.display();
cout << "a + b = " ; c.display();
cout << "a - b = " ; d.display();
cout << "a * b = " ; e.display();
cout << "a / b = " ; f.display();
return 0;
}
第三题
#include<iostream>
using namespace std;
class Complex
{
public:
Complex(){real = 0 ; imag = 0;}
Complex(double r, double i){real = r ; imag = i;}
friend Complex operator+ (Complex &c1 , int &i);
friend Complex operator+ (int &i, Complex &c1);
friend Complex operator+ (Complex &c1, Complex &c2);
void display();
private:
double real;
double imag;
};
Complex operator+(Complex &c1 , int& i)
{
Complex c;
c.real = c1.real + i;
c.imag = c1.imag;
return c;
}
Complex operator+(int& i , Complex &c1)
{
Complex c;
c.real = c1.real + i;
c.imag = c1.imag;
return c;
}
Complex operator+(Complex &c1 , Complex &c2)
{
return Complex(c1.real + c2.real , c1.imag + c2.imag);
}
void Complex::display()
{
cout << "(" << real << "," << imag << "i)" << endl;
}
int main()
{
Complex a(4, 5), b(3, 1) , c , d , e;
int i = 5;
c = a + b;
d = a + i;
e = i + a;
cout << "a = "; a.display();
cout << "b = "; b.display();
cout << "a + i = "; d.display();
cout << "i + a = "; e.display();
return 0;
}
第四题
#include<iostream>
using namespace std;
class Rec
{
public:
Rec();
void input();
void display();
Rec operator+ (Rec &c);
private:
int a[2][3];
};
Rec::Rec()
{
for(int i = 0 ; i< 2 ;i++ )
for(int j = 0; j< 3; j++)
a[i][j] = 0;
}
void Rec::input()
{
cout <<"请输入一个2行3列的矩阵" << endl;
for(int i = 0; i < 2; i++)
for(int j = 0 ; j < 3 ;j++)
cin >> a[i][j];
}
void Rec::display()
{
for(int i = 0; i< 2; i++)
{
for(int j = 0; j< 3; j++)
cout <<" "<< a[i][j];
cout << endl;
}
}
Rec Rec::operator +(Rec& c)
{
Rec T;
for(int i = 0 ; i< 2; i++)
for(int j = 0; j< 3; j++)
T.a[i][j] = a[i][j] + c.a[i][j];
return T;
}
int main()
{
Rec a, b , c;
a.input();
b.input();
c = a + b;
a.display();
cout << endl;
b.display();
cout << endl;
c.display();
cout << endl;
return 0;
}
<img src="https://img-blog.csdn.net/20150622202459244?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbG9uZ3hpdWh1YQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
#include<iostream>
using namespace std;
class Rec
{
<span style="white-space:pre"> </span>
public:
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>Rec();
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>void display();
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>Rec operator+ (Rec &c);
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>friend ostream& operator <<(ostream& output ,Rec &c);
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>friend istream& operator >>(istream& input ,Rec &c);
<span style="white-space:pre"> </span>
private:
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>int a[2][3];
<span style="white-space:pre"> </span>
};
Rec::Rec()
{
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>for(int i = 0 ; i< 2 ;i++ )
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>for(int j = 0; j< 3; j++)
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>a[i][j] = 0;
}
istream& operator >>(istream& input , Rec &c)
{
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>cout << "请输入一个2行3列的矩阵" << endl;
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>for(int i = 0; i < 2; i++)
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>for(int j = 0 ; j < 3 ;j++)
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>input >> c.a[i][j];
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>return input;
}
ostream& operator <<(ostream& output ,Rec &c)
{
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>for(int i = 0; i< 2; i++)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>for(int j = 0; j< 3; j++)
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>cout <<" "<< c.a[i][j];
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>cout << endl;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>return output;
}
Rec Rec::operator +(Rec& c)
{
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>Rec T;
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>for(int i = 0 ; i< 2; i++)
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>for(int j = 0; j< 3; j++)
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>T.a[i][j] = a[i][j] + c.a[i][j];
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>return T;
}
int main()
{
<span style="white-space:pre"> </span>Rec a, b , c;
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>cin >> a;
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>cin >> b;
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>c = a + b;
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>cout << a << endl;
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>cout << b << endl;
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>cout << c << endl;
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>return 0;
}
<img src="https://img-blog.csdn.net/20150622202810817?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbG9uZ3hpdWh1YQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
第六题
#include<iostream>
using namespace std;
<pre class="cpp" name="code">class Complex
{
public:
Complex(){real = 0 ; imag = 0;}
Complex(double r , double i){real = r ; imag = i;}
friend Complex operator+ (Complex & c, double a);
operator double(){return real;}
friend ostream& operator<< (ostream& output , Complex& c);
private:
double real;
double imag;
};
Complex operator+ (Complex &c , double a )
{
return Complex(c.real + a , c.imag);
}
ostream& operator<< (ostream& output , Complex& c)
{
output << "(" << c.real <<" ," << c.imag <<"i)";
return output;
}
int main()
{
Complex a(4,5);
double c, b = 1.5;
c = a + b;
cout <<"a =" << a << endl;
cout << "b =" <<b << endl;
cout << "a + b =" << c << endl;
return 0;
}
第七题
<pre class="cpp" name="code">#include<iostream>
#include<string>
using namespace std;
class Student
{
public:
void display1()
{
cout << "当前这名学生一些信息是:" << endl;
cout << "the student's num is " << num << endl;
cout << "the student's name is " << name << endl;
cout << "the student's sex is " << sex << endl;
cout << "the student's tel is " << tel << endl;
}
Student();
Student(int n , string na , char s, int t):num(n) , name(na), sex(s), tel(t){}
protected:
int num;
string name;
char sex;
private:
int tel;
};
Student::Student()
{
num = 01432222;
name = "safsaf";
sex = 'M';
tel = 40082080;
}
class Teacher: public Student
{
public:
void get()
{
cout << "请输入这名从前是学生的老师的当前年龄" ; cin >> age;
}
void display()
{
cout << "the student's num is " << num << endl;
cout << "the student's name is " << name << endl;
cout << "the student's sex is " << sex << endl;
cout << "the student's age is " <<age << endl;
}
private:
int age;
};
int main()
{
Student A;
A.display1();
cout << "恭喜这名学生成功当上了老师哦" << endl;
Teacher B;
B.get();
B.display();
return 0;
}
<img src="https://img-blog.csdn.net/20150622203215607?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbG9uZ3hpdWh1YQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />