好文档 - 专业文书写作范文服务资料分享网站

c++面向对象程序设计课后习题答案(谭浩强版) 

天下 分享 时间: 加入收藏 我要投稿 点赞

int i=5; c3=c1+c2;

cout<<\ c3.display(); c3=i+c1;

cout<<\ c3.display(); c3=c1+i;

cout<<\ c3.display(); return 0; } 4:

#include using namespace std;

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() {cout<<\ for(int i=0;i<2;i++) for(int j=0;j<3;j++) cin>>mat[i][j]; }

//定义Matrix类 //默认构造函数 //重载运算符“+” //输入数据函数 //输出数据函数 //定义构造函数 //定义重载运算符“+”函数 //定义输入数据函数

void Matrix::display() //定义输出数据函数 {for (int i=0;i<2;i++) {for(int j=0;j<3;j++)

{cout<

int main() {Matrix a,b,c; a.input(); b.input();

cout<

cout<

c=a+b; 加

cout<

#include //using namespace std; class Matrix {public: Matrix();

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

friend ostream& operator<<(ostream&,Matrix&); friend istream& operator>>(istream&,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 &m) {cout<<\ for(int i=0;i<2;i++) for(int j=0;j<3;j++) in>>m.mat[i][j]; return in; }

ostream& operator<<(ostream &out,Matrix &m) {for (int i=0;i<2;i++) {for(int j=0;j<3;j++)

{out<

int main() { Matrix a,b,c; cin>>a; cin>>b;

cout<

cout<

#include 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;} operator double(){return real;} void display(); private:

double real;

double imag; };

void Complex::display()

{cout<<\

int main()

{Complex c1(3,4),c2; double d1; d1=2.5+c1;

cout<<\ c2=Complex(d1); cout<<\ c2.display(); return 0; } 7:

#include using namespace std; class Student {public:

Student(int,char[],char,float); int get_num(){return num;}

char * get_name(){return name;} char get_sex(){return sex;} void display()

{cout<<\ private: int num;

char name[20]; char sex; float score; };

Student::Student(int n,char nam[],char s,float so) {num=n;

strcpy(name,nam); sex=s; score=so; }

class Teacher {public: Teacher(){}

Teacher(Student&);

Teacher(int n,char nam[],char sex,float pay); void display(); private: int num;

char name[20]; char sex; float pay; };

Teacher::Teacher(int n,char nam[],char s,float p) {num=n;

strcpy(name,nam); sex=s; pay=p; }

Teacher::Teacher(Student& stud) {num=stud.get_num();

strcpy(name,stud.get_name()); sex=stud.get_sex(); pay=1500;}

void Teacher::display()

{cout<<\

int main()

{Teacher teacher1(10001,\ Student student1(20010,\ cout<<\ student1.display();

teacher2=Teacher(student1); cout<<\ teacher2.display(); return 0; }

第五章

1:

#include using namespace std; class Student

1snua7w6oy37lyd0yejn
领取福利

微信扫码领取福利

微信扫码分享