第四章

第一题

#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="" />

 



 
 
 
 









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值