c++面向对象程序设计课后题答案 谭浩强 第四章

1:

#include <iostream>

using namespace std;

class Complex

{public:

Complex(){real=0;imag=0;}

Complex(double r,double i){real=r;imag=i;}

double get_real();

double get_imag();

void display();

private:

double real;

double imag;

};

double Complex::get_real()

{return real;}

double Complex::get_imag()

{return imag;}

void Complex::display()

{cout<<"("<<real<<","<<imag<<"i)"<<endl;}

Complex operator + (Complex &c1,Complex &c2)

{

return Complex(c1.get_real()+c2.get_real(),c1.get_imag()+c2.get_imag());

}

int main()

{Complex c1(3,4),c2(5,-10),c3;

c3=c1+c2;

cout<<"c3=";

c3.display();

return 0;

}

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

2:

#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=imag*c2.real+real*c2.imag;

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 c1(3,4),c2(5,-10),c3;

c3=c1+c2;

cout<<"c1+c2=";

c3.display();

c3=c1-c2;

cout<<"c1-c2=";

c3.display();

c3=c1*c2;

cout<<"c1*c2=";

c3.display();

c3=c1/c2;

cout<<"c1/c2=";

c3.display();

return 0;

}

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

3:

#include <iostream>        //用VC++时改为∶ #include <iostream.h>

using namespace std;      //用VC++时为取消此行

class Complex

{public:

Complex(){real=0;imag=0;}

Complex(double r,double i){real=r;imag=i;}

Complex operator+(Complex &c2);

Complex operator+(int &i);

friend Complex operator+(int&,Complex &);

void display();

private:

double real;

double imag;

};

Complex Complex::operator+(Complex &c)

{return Complex(real+c.real,imag+c.imag);}

Complex Complex::operator+(int &i)

{return Complex(real+i,imag);}

void Complex::display()

{cout<<"("<<real<<","<<imag<<"i)"<<endl;}

Complex operator+(int &i,Complex &c)

{return Complex(i+c.real,c.imag);}

int main()

{Complex c1(3,4),c2(5,-10),c3;

int i=5;

c3=c1+c2;

cout<<"c1+c2=";

c3.display();

c3=i+c1;

cout<<"i+c1=";

c3.display();

c3=c1+i;

cout<<"c1+i=";

c3.display();

return 0;

}

谭浩强

4:

#include <iostream>

using namespace std;

class Matrix                                          //定义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<<"input value of matrix:"<<endl;

for(int i=0;i<2;i++)

for(int j=0;j<3;j++)

cin>>mat[i][j];

}

void Matrix::display()                                //定义输出数据函数

{for (int i=0;i<2;i++)

{for(int j=0;j<3;j++)

{cout<<mat[i][j]<<" ";}

cout<<endl;}

}

int main()

{Matrix a,b,c;

a.input();

b.input();

cout<<endl<<"Matrix a:"<<endl;

a.display();

cout<<endl<<"Matrix b:"<<endl;

b.display();

c=a+b;                                         //用重载运算符“+”实现两个矩阵相加

cout<<endl<<"Matrix c = Matrix a + Matrix b :"<<endl;

c.display();

return 0;

}

c++面向对象程序设计课后题答案

5:

#include <iostream.h>

//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<<"input value of matrix:"<<endl;

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<<m.mat[i][j]<<" ";}

out<<endl;}

return out;

}

int main()

{ Matrix a,b,c;

cin>>a;

cin>>b;

cout<<endl<<"Matrix a:"<<endl<<a<<endl;

cout<<endl<<"Matrix b:"<<endl<<b<<endl;

c=a+b;

cout<<endl<<"Matrix c = Matrix a + Matrix b :"<<endl<<c<<endl;

return 0;

}

6:

#include <iostream>

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<<"("<<real<<", "<<imag<<")"<<endl;}

int main()

{Complex c1(3,4),c2;

double d1;

d1=2.5+c1;

cout<<"d1="<<d1<<endl;

c2=Complex(d1);

cout<<"c2=";

c2.display();

return 0;

}

7:

#include <iostream>

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<<"num:"<<num<<"\nname:"<<name<<"\nsex:"<<sex<<"\nscore:"<<score<<"\n\n";}

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<<"num:"<<num<<"\nname:"<<name<<"\nsex:"<<sex<<"\npay:"<<pay<<"\n\n";}

int main()

{Teacher teacher1(10001,"Li",'f',1234.5),teacher2;

Student student1(20010,"Wang",'m',89.5);

cout<<"student1:"<<endl;

student1.display();

teacher2=Teacher(student1);

cout<<"teacher2:"<<endl;

teacher2.display();

return 0;

}

c++面向对象程序设计 课后题 答案 谭浩强 第四章的更多相关文章

  1. c++面向对象程序设计 谭浩强 第三章答案

    2: #include <iostream> using namespace std; class Date {public: Date(int,int,int); Date(int,in ...

  2. javase程序设计课后题答案

    ;第1章 Java概述 编译java application源程序文件将产生相应的字节码文件,这些字节码文件别的扩展名为.java 执行一个java程序fristapp的方法是运行java frist ...

  3. c++面向对象程序设计 谭浩强 第五章答案

    1: #include <iostream> using namespace std; class Student {public: void get_value() {cin>&g ...

  4. 谭浩强第四版第九章课后习题12>>>建立一个链表,每个节点包括:学号、姓名、性别、年龄。输入一个年龄,若链表 中的结点所包含的年龄等于此年龄,则删除此结点。

    #include<stdio.h> #include<stdlib.h> #define N sizeof(link) typedef struct lin { struct ...

  5. C程序设计(谭浩强)第五版课后题答案 第一章

    大家好,这篇文章分享了C程序设计(谭浩强)第五版课后题答案,所有程序已经测试能够正常运行,如果小伙伴发现有错误的的地方,欢迎留言告诉我,我会及时改正!感谢大家的观看!!! 1.什么是程序?什么是程序设 ...

  6. c++面向对象程序设计 谭浩强 第一章答案

    c++面向对象程序设计 谭浩强 答案 第一章 目录: c++面向对象程序设计 谭浩强 答案 第一章 c++面向对象程序设计 谭浩强 答案 第二章 c++面向对象程序设计 谭浩强 答案 第三章 c++面 ...

  7. c++面向对象程序设计 谭浩强 第二章答案

    类体内定义成员函数 #include <iostream> using namespace std; class Time { public: void set_time(); void ...

  8. 挂羊头卖狗肉蓄意欺骗读者——谭浩强《C程序设计(第四版)》中所谓的“按照C99”(二)

    挂羊头卖狗肉蓄意欺骗读者——谭浩强<C程序设计(第四版)>中所谓的“按照C99”(二) 在<谭C>p4:“本书的叙述以C99标准为依据”,下面从C89到C99的主要变化方面来看 ...

  9. 再论谭浩强《C语言程序设计》

    一些同学学不好C语言,把罪责归于“因为教材是谭浩强写的”实在是很滑稽. 谭浩强老先生 1934 年生,现在已经 80 岁了.他 1958 年从清华大学自动控制系毕业,那年 24 岁.要知道 C 语言那 ...

随机推荐

  1. SSRS 报表 如何加参数

    SSRS 报表 如何加参数 连接上以后出现一个问题 就是给报表加上参数以后报表不断刷新,跟上次那个报表刷新是同样的问题.那么下面我们来解决一下. 1. 这是给报表添加默认参数进入页面后就不断的刷新刷新 ...

  2. Centos7 docker nginx容器搭建

    一.安装docker http://www.cnblogs.com/WJ--NET/p/8553807.html 二.创建Dockerfile #创建文件夹 mkdir centos_nginx cd ...

  3. 基于HTML5陀螺仪实现ofo首页眼睛移动效果

    最近用ofo小黄车App的时候,发现以前下方扫一扫变成了一个眼睛动的小黄人,觉得蛮有意思的,这里用HTML5仿一下效果. ofo眼睛效果 效果分析 从效果中不难看出,是使用陀螺仪事件实现的. 这里先来 ...

  4. NOSQL:redis mongodb

    redis 1 概念及其基本操作:http://blog.nosqlfan.com/html/3139.html 2 安装配置及其操作:http://blog.fens.me/linux-redis- ...

  5. JAVA代理方式使用示例总结

    JAVA代理方式使用示例总结 一.    代理方式概括 Java的代理方式主要包含了静态代理,动态代理两种方式,其中,动态代理根据实现的方式不同,又可以划分为jdk动态代理和cglib动态代理. 二. ...

  6. Python学习笔记基础篇-(1)Python周边

    一.系统命令 1.Ctrl+D 退出Python IDLE input方法中输入EOF字符,键入Ctrl+D 2.命令行选项: -d   提供调试输出 -O 生成优化的字节码(.pyo文件) -S 不 ...

  7. springboot-注解讲解

    @Configuration:声明我们JdbcConfig是一个配置类 @PropertySource:指定属性文件的路径是:classpath:jdbc.properties 通过@Value为属性 ...

  8. 【转】Oracle基础结构认知—进程及逻辑结构 礼记八目 2017-12-17 19:33:21

    原文地址:https://www.toutiao.com/i6500477672349499917/ 一. Process Structure进程结构 Oracle有两种类型的进程: 服务器进程和后台 ...

  9. 关于TCP中对于ACK报文是否需要确认的理解

    首先,TCP是一个面向字节流的协议,它不会对自己的内容做出任何的解释,也不需要做出解释,具体的解释由上层的协议来处理. 其次,TCP是一个面向字节流的协议,它会对它发送的每一个字节负责,确保每一个字节 ...

  10. Python数据分析5-----数据规约

    1.数据规约概念和目的 数据规约是产生更小且保留数据完整性的新数据集. 意义:降低无效错误数据的影响.更有效率.降低存储成本. 2.属性规约 (1)属性合并(降维):比如PCA (2)删除不相关属性 ...