第四章习题

来源:互联网 发布:地板革淘宝网 编辑:程序博客网 时间:2024/06/05 03:51
<span style="font-size:48px;">第五题</span>
#include<iostream.h>class Rec{public:Rec();void display();Rec operator+ (Rec &c);friend ostream& operator <<(ostream& output ,Rec &c);friend istream& operator >>(istream& input ,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;}istream& operator >>(istream& input , Rec &c){cout << "请输入一个2行3列的矩阵"  << endl;for(int i = 0; i < 2; i++)for(int j = 0 ; j < 3 ;j++)input >> c.a[i][j];return input;}ostream& operator <<(ostream& output ,Rec &c){for(int i = 0; i< 2; i++){for(int j = 0; j<  3; j++)cout <<" "<< c.a[i][j];cout << endl;}return output;}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;cin >> a;cin >> b;c = a + b;cout  << a << endl;cout << b << endl;cout << c << endl;return 0;}

 

 

 

 

第一题

 

 

#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;}


 

 

 

 

 

 

第二题

 

#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.h>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;}<img src="http://img.blog.csdn.net/20150510062749873?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGVsbG93b3JsZDEyNzA0MjQ3ODk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
 
 
 
 
 
 
 
 
第四题
 
#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="http://img.blog.csdn.net/20150510063117449?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGVsbG93b3JsZDEyNzA0MjQ3ODk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
 
 
 
 
 
 
 
 
 
 
第六题
<pre class="cpp" name="code">#include<iostream.h>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;}

<img src="http://img.blog.csdn.net/20150510063449373?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGVsbG93b3JsZDEyNzA0MjQ3ODk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
 
 
 
最后一题。。。
#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="http://img.blog.csdn.net/20150510063602584?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGVsbG93b3JsZDEyNzA0MjQ3ODk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
 
 
 
0 0
原创粉丝点击