9-3

#include<iostream>
using namespace std;
class Date{public://有默认参数的构造函数不能重载,会引起歧义Date(int=1,int=1,int=2005);void display();
private:int month;int year;int day;
};
Date::Date(int m,int d,int y):month(m),day(d),year(y){}
void Date::display() {cout<<month<<"/"<<day<<"/"<<year<<endl;
}
int main(){Date d1(10,13,2005);Date d2(12,30);Date d3(10);Date d4;d1.display();d2.display();d3.display();d4.display();return 0;
}

9-4

#include<iostream>
using namespace std;
class Student{public:Student(int ,float );void display();
private:int num;float score;
};
Student::Student(int n,float s):num(n),score(s){}
void Student::display() {cout<<num<<" "<<score<<endl;
}
int main(){Student stu[5]={Student(101,78.5),Student(102,85.5),Student(103,98.5),Student(104,100.0),Student(105,95.5)},*p;p=stu;int i;for(i=0;i<5;i+=2)(p+i)->display();return 0;
}

9-5

#include<iostream>
using namespace std;
class Student{public:Student(int ,float );int max_value(Student *s);void display();int num;float score;
};
Student::Student(int n,float s):num(n),score(s){}
int Student::max_value(Student *s){float max_m=s[0].score;int m=0;for(int i=0;i<5;i++)if(s[i].score>max_m){max_m=s[i].score;m=i;}return m;
}
void Student::display() {cout<<num<<" "<<score<<endl;
}
int main(){Student stu[5]={Student(101,78.5),Student(102,85.5),Student(103,98.5),Student(104,100.0),Student(105,95.5)},*p;int m;m=stu[5].max_value(stu);cout<<stu[m].score<<" has best score."<<endl;return 0;
}

9-7-1

#include<iostream>
using namespace std;
class Student{public:Student(int n,float s):num(n),score(s){}void change(int n,float s){num=n;score=s;}//常对象只能调用常成员函数void display() const{cout<<num<<" "<<score<<endl;
}
private:int num;float score;
};
int main(){const Student stud(101,78.5);stud.display();return 0;
}

9-7-2

#include<iostream>
using namespace std;
class Student{public:Student(int n,float s):num(n),score(s){}void change(int n,float s) const{num=n;score=s;}void display() const{cout<<num<<" "<<score<<endl;
}
private:mutable int  num;mutable int  score;
};int main(){//想修改常函数,改为mutable,成员函数改为constconst Student stud(101,78.5);stud.display();stud.change(101,80.5);stud.display();return 0;
}

9-7-3

//通过指针修改
#include<iostream>
using namespace std;
class Student{public:Student(int n,float s):num(n),score(s){}void change(int n,float s){num=n;score=s;}void display() {cout<<num<<" "<<score<<endl;
}
private:int num;float score;
};
int main(){Student stud(101,78.5);Student *p=&stud;p->display();p->change(101,80.5);p->display();return 0;
}

9-7-4

#include<iostream>
using namespace std;
class Student{public:Student(int n,float s):num(n),score(s){}void change(int n,float s){num=n;score=s;}void display() const{cout<<num<<" "<<score<<endl;
}
private:int num;float score;
};
int main(){Student stud(101,78.5);//指向常对象的指针变量,不通过指针修改即可,成员函数constconst Student *p=&stud;p->display();stud.change(101,80.5);p->display();return 0;
}

9-7-5

#include<iostream>
using namespace std;
class Student{public:Student(int n,float s):num(n),score(s){}void change(int n,float s){num=n;score=s;}void display(){cout<<num<<" "<<score<<endl;
}
private:int num;float score;
};
int main(){Student stud(101,78.5);//指向对象的常指针,不改变指针指向的对象即可Student *const p=&stud;p->display();p->change(101,80.5);p->display();return 0;
}

9-8

#include<iostream>
using namespace std;
class Student{public:Student(int n,float s):num(n),score(s){}void change(int n,float s){num=n;score=s;}void display(){cout<<num<<" "<<score<<endl;
}
private:int num;float score;
};
void fun(Student &stu);
int main(){Student stud(101,78.5);fun(stud);return 0;
}
void fun(Student &stud){stud.display();stud.change(101,80.5);stud.display();
}

9-9

#include<iostream>
using namespace std;
class Product{public:Product(int n,int q,float p):num(n),quantity(q),price(p){};void total();static float average();static void display();
private:int num;int quantity;float price;static float discount;static float sum;static int n;};
float Product::discount=0.05;
float Product::sum=0;
int Product::n=0;
int main(){Product pro[3]={Product(101,5,23.5),Product(102,12,24.56),Product(103,100,21.5)};for(int i=0;i<3;i++)pro[i].total();Product::display();return 0;
}
void Product::total(){if(quantity>=10)sum+=quantity*price*0.98*(1-discount);elsesum+=quantity*price*(1-discount);n+=quantity;
}
float Product::average(){return sum/n;
}
void Product::display(){cout<<"Sum:"<<sum<<" "<<"average:"<<average()<<endl;
}

9-10

#include<iostream>
using namespace std;
class Date;
class Time{public:Time(int,int,int);friend void display(Date &d,Time &t);
private:int hour;int minute;int sec;
};
class Date{public:Date(int,int,int);friend void display(Date &d,Time &t);
private:int month;int day;int year;
};
Time::Time(int h,int m,int s){hour=h;minute=m;sec=s;
}
Date::Date(int m,int d,int y){month=m;day=d;year=y;
}
int main(){Time t1(10,13,56);Date d1(12 ,25 ,2004);display(d1,t1);return 0;
}
void display(Date &d,Time &t){cout<<d.month<<"/"<<d.day<<"/"<<d.year<<endl;cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl;
}

9-11

#include<iostream>
using namespace std;
class Date;
class Time{public:Time(int,int,int);void display( Date &);
private:int hour;int minute;int sec;
};
class Date{public:Date(int,int,int);friend Time;
private:int month;int day;int year;
};Time::Time(int h,int m,int s){hour=h;minute=m;sec=s;
}
void Time::display( Date &d){cout<<d.month<<"/"<<d.day<<"/"<<d.year<<endl;cout<<hour<<":"<<minute<<":"<<sec<<endl;
}Date::Date(int m,int d,int y){month=m;day=d;year=y;
}int main(){Time t1(10,13,56);Date d1(12 ,25 ,2004);t1.display(d1);return 0;
}

9-12

#include<iostream>
using namespace std;
template <class numtype>
class Compare{public:Compare(numtype a,numtype b);numtype max_value();numtype min_value();
private:numtype x,y;
};
template <class numtype>
Compare<numtype>::Compare(numtype a,numtype b){x=a;y=b;
}
template <class numtype>
numtype Compare<numtype>::max_value(){return (x>y)?x:y;
}
template <class numtype>
numtype Compare<numtype>::min_value(){return (x<y)?x:y;
}int main(){Compare<int> cmp1(3,7);cout<<cmp1.max_value()<<" is the Maximum of two integer numbers."<<endl;cout<<cmp1.min_value()<<" is the Minimum of two integer numbers."<<endl<<endl;Compare<float> cmp2(45.78,93.6);cout<<cmp2.max_value()<<" is the Maximum of two float numbers."<<endl;cout<<cmp2.min_value()<<" is the Minimum of two float numbers."<<endl<<endl;Compare<char> cmp3('a','A');cout<<cmp3.max_value()<<" is the Maximum of two characters."<<endl;cout<<cmp3.min_value()<<" is the Minimum of two characters."<<endl;return 0;
}

10-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(){return real;}double get_imag(){return imag;}void display();
private:double real;double imag;
};
Complex operator + (Complex &c1,Complex &c2);
void Complex::display(){cout<<"("<<real;if(imag>=0)cout<<"+";cout<<imag<<"i)"<<endl;
}
int main(){Complex c1(4,3),c2(5,-10),c3;c3=c1+c2;cout<<"c1=";c1.display();cout<<"c2=";c2.display();cout<<"c3=";c3.display();return 0;
}
Complex operator + (Complex &c1,Complex &c2){return Complex(c1.get_real()+c2.get_real(),c1.get_imag()+c2.get_imag());
}

10-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){return Complex(real+c2.real,imag+c2.imag);
}
Complex Complex::operator-(Complex &c2){return Complex(real-c2.real,imag-c2.imag);
}
Complex Complex::operator*(Complex &c2){return Complex(real*c2.real-imag*c2.imag,real*c2.imag-imag*c2.real);
}
Complex Complex::operator/(Complex &c2){double a=c2.real*c2.real-c2.imag*c2.imag;real=real*c2.real+imag*c2.imag;imag=imag*c2.real-real*c2.imag;return Complex(real/a,imag/a);
}
void Complex::display(){cout<<real;if(imag>=0)cout<<"+";cout<<imag<<"i"<<endl;
}
int main(){Complex c1(2,1),c2(4,2),c3;cout<<"c1=";c1.display();cout<<"c2=";c2.display();cout<<"c3=";c3=c1+c2;c3.display();cout<<"c3=";c3=c1-c2;c3.display();cout<<"c3=";c3=c1*c2;c3.display();cout<<"c3=";c3=c1/c2;c3.display();return 0;
}

10-3

 #include<iostream>
using namespace std;
class Complex{public:Complex(){real=0;imag=0;}Complex(double r,double i){real=r;imag=i;}Complex(double r) {real=r;imag=0;}friend Complex operator+(Complex c1, Complex c2);void display();
private:double real;double imag;
};
Complex operator+(Complex c1, Complex c2){return Complex(c1.real+c2.real,c1.imag+c2.imag);
}
void Complex::display(){cout<<real;if(imag>=0)cout<<"+";cout<<imag<<"i"<<endl;
}
int main(){Complex c1(2,1),c2(4,2),c3;int i=5;cout<<"c1=";c1.display();cout<<"c2=";c2.display();cout<<"c3=";c3=c1+c2;c3.display();cout<<"c3=";c3=c1+i;c3.display();cout<<"c3=";c3=i+c1;c3.display();return 0;
}

10-4

#include<iostream>
#include<iomanip>
using namespace std;
class Matrix{public:Matrix();Matrix operator+(Matrix &m);void input();void display();
private:int a[2][3];
}ma;
Matrix::Matrix(){int i,j;for(i=0;i<2;i++)for(j=0;j<3;j++)a[i][j]=0;
}
void Matrix::input(){int i,j;for(i=0;i<2;i++)for(j=0;j<3;j++)cin>>a[i][j];
}
void Matrix::display(){int i,j;for(i=0;i<2;i++){for(j=0;j<3;j++)cout<<setw(5)<<a[i][j]<<"  ";cout<<endl;}
}
Matrix Matrix::operator+(Matrix &m){int i,j;for(i=0;i<2;i++)for(j=0;j<3;j++)ma.a[i][j]=a[i][j]+m.a[i][j];return ma;
}
int main(){Matrix m1,m2,m3;cout<<"Please enter m1:";m1.input();cout<<endl;cout<<"Please enter m2:";m2.input();cout<<endl;cout<<"t1:"<<endl;m1.display();cout<<"t2:"<<endl;m2.display();cout<<"t3:"<<endl;m3=m1+m2;m3.display();return 0;
}

10-5

#include<iostream>
#include<iomanip>
using namespace std;
class Matrix{public:Matrix();Matrix operator+(Matrix &m);friend ostream& operator <<(ostream &output,Matrix &m);friend istream& operator >>(istream &input,Matrix &m);
private:int a[2][3];
}ma;
Matrix::Matrix(){int i,j;for(i=0;i<2;i++)for(j=0;j<3;j++)a[i][j]=0;
}
Matrix Matrix::operator+(Matrix &m){int i,j;for(i=0;i<2;i++)for(j=0;j<3;j++)ma.a[i][j]=a[i][j]+m.a[i][j];return ma;
}
ostream& operator <<(ostream &output,Matrix &m){int i,j;output<<"The matrix is : "<<endl;for(i=0;i<2;i++){for(j=0;j<3;j++)output<<setw(5)<<m.a[i][j]<<"  ";output<<endl;}return output;
}
istream& operator >>(istream &input,Matrix &m){int i,j;cout<<"Please enter the matrix : "<<endl;for(i=0;i<2;i++){for(j=0;j<3;j++)input>>m.a[i][j];}return input;
}
int main(){Matrix m1,m2,m3;cin>>m1;cin>>m2;cout<<m1;cout<<m2;m3=m1+m2;cout<<m3;return 0;
}

10-6

#include<iostream>
#include<iomanip>
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;if(imag>=0)cout<<"+";cout<<imag<<"i"<<endl;
}
int main(){Complex c1(3,-4);double d1=2.5,d2;d2=d1+c1;cout<<"d2="<<d2<<endl;cout<<"c2=";Complex(d2).display();return 0;
}

10-7

#include<iostream>
using namespace std;
class Student{public:Student(int nu ,string na,char se,float sc){num=nu;name=na;sex=se;score=sc;}int get_num(){return num;}string get_name(){return name;}char get_sex(){return sex;}float get_score(){return score;}void display(){cout<<"num:"<<num<<"   name:"<<name<<"   sex:"<<sex<<"   score:"<<score<<endl;}
private:int num;string name;char sex;float score;
};
class Teacher{public:Teacher(){}Teacher(Student &stu){num=stu.get_num();name=stu.get_name();sex=stu.get_sex();pay=2000;}Teacher(int nu ,string na,char se,float p){num=nu;name=na;sex=se;pay=p;}void display(){cout<<"num:"<<num<<"   name:"<<name<<"   sex:"<<sex<<"   pay:"<<pay<<endl;}
private:int num;string name;char sex;float pay;//工资
};
int main(){Teacher t1(1001,"Tang",'f',2500),t2;Student s1(1002,"Zhou",'m',90);cout<<"Teacher 1 : ";t1.display();cout<<endl<<"Student 2 : ";s1.display();t2=Teacher(s1);cout<<endl<<"Teacher 2 : ";t2.display();return 0;
}

11-1

#include<iostream>
using namespace std;
class Student{public:void get_value(){cin>>num>>name>>sex;}void display(){cout<<"num: "<<num<<endl;cout<<"name: "<<name<<endl;cout<<"sex: "<<sex<<endl;}
private:int num;string name;char sex;
};
class Student1:public Student{public:void get_value_1(){cin>>age>>addr;}void display_1(){cout<<"age: "<<age<<endl;cout<<"address: "<<addr<<endl;}
private:int age;string addr;
};
int main(){Student1 stu;stu.get_value();stu.get_value_1();stu.display();stu.display_1();return 0;
}

11-2

#include<iostream>
using namespace std;
class Student{public:void get_value(){cin>>num>>name>>sex;}void display(){cout<<"num: "<<num<<endl;cout<<"name: "<<name<<endl;cout<<"sex: "<<sex<<endl;}
private:int num;string name;char sex;
};
class Student1:private Student{public:void get_value_1(){get_value();cin>>age>>addr;}void display_1(){display();cout<<"age: "<<age<<endl;cout<<"address: "<<addr<<endl;}
private:int age;string addr;
};
int main(){Student1 stu;stu.get_value_1();stu.display_1();return 0;
}

11-3

#include<iostream>
using namespace std;
class Student{public:
protected:int num;string name;char sex;
};
class Student1:protected Student{public:void get_value_1(){cin>>num>>name>>sex;cin>>age>>addr;}void display_1(){cout<<"num: "<<num<<endl;cout<<"name: "<<name<<endl;cout<<"sex: "<<sex<<endl;cout<<"age: "<<age<<endl;cout<<"address: "<<addr<<endl;}
private:int age;string addr;
};
int main(){Student1 stu;stu.get_value_1();stu.display_1();return 0;
}

11-4

#include<iostream>
using namespace std;
class Student{public:
protected:int num;string name;char sex;
};
class Student1:public Student{public:void get_value_1(){cin>>num>>name>>sex;cin>>age>>addr;}void display_1(){cout<<"num: "<<num<<endl;cout<<"name: "<<name<<endl;cout<<"sex: "<<sex<<endl;cout<<"age: "<<age<<endl;cout<<"address: "<<addr<<endl;}
private:int age;string addr;
};
int main(){Student1 stu;stu.get_value_1();stu.display_1();return 0;
}

11-9

#include<iostream>
#include<string>
using namespace std;
class Teacher{public:Teacher(){}Teacher(string nam,int a,char s,string ad,string t,string tit);void display();
protected:int age;string name;char sex; string addr;string tell;string title;//职称
};
class Cadre{public:Cadre(){}Cadre(string nam,int a,char s,string ad,string t,string p);
protected:int age;string name; char sex;string addr; string tell;string post;//职务
};
class Teacher_Cadre:public Teacher,public Cadre {public:Teacher_Cadre(){}Teacher_Cadre(string nam,int a,char s,string ad,string t,string tit,string p,float w):Teacher(nam,a,s,ad,t,tit),Cadre(nam,a,s,ad,t,p),wages(w){}void show();
private:float wages;
};
Teacher::Teacher(string nam,int a,char s,string ad,string t,string tit){age=a;name=nam;sex=s;addr=ad;tell=t;title=tit;}
void Teacher::display(){cout<<"name: "<<name<<endl;cout<<"age: "<<age<<endl;cout<<"sex: "<<sex<<endl;cout<<"address: "<<addr<<endl;cout<<"tell: "<<tell<<endl;cout<<"title: "<<title<<endl;}
Cadre::Cadre(string nam,int a,char s,string ad,string t,string p){age=a;name=nam;sex=s;addr=ad;tell=t;post=p;}
void Teacher_Cadre::show(){Teacher::display();cout<<"post: "<<Cadre::post<<endl;cout<<"wage: "<<wages<<endl;}
int main(){Teacher_Cadre tc("Tang",21,'f',"Shanghai","1888888","Professor","minister",3000);tc.show();return 0;
}

11-10

#include<iostream>
#include<string>
using namespace std;
class Teacher{public:Teacher(string n,int a,char s):name(n),age(a),sex(s){}void display(){cout<<"Name:"<<name<<endl;cout<<"Age:"<<age<<endl;cout<<"Sex:"<<sex<<endl;}
private:int age;string name;char sex;
};
class BirthDate{public:BirthDate(int y,int m,int d):year(y),month(m),day(d){}void change(int y,int m,int d){year=y;month=m;day=d;}void display_1(){cout<<"Birthday:"<<year<<"/"<<month<<"/"<<day<<endl;}
private:int year;int month;int day;
};
class Professor:public Teacher {public:Professor(string n,int a,char s,int y,int m,int d): Teacher( n,a,s),birthday(y,m,d){}void change(int y,int m,int d){birthday.change(y,m,d);}void show(){Teacher::display();birthday.display_1();}
private:BirthDate birthday;
};int main(){Professor p("Zhang",21,'f',1999,2,16);p.show();p.change(1999,7,14);p.show();return 0;
}

谭浩强C++(第三版)(3)-9-11章相关推荐

  1. 输出所有3位数的水仙花数,例:153=1的3次方+5的3次方+3的3次方 谭浩强《c语言程序设计》第五章第八题

    题目 本题是谭浩强<c语言程序设计>第五章第八题 题目:输出所有3位数的水仙花数,例:153=1的3次方+5的3次方+3的3次方 提示:以下是本篇文章正文内容,欢迎朋友们进行指正,一起探讨 ...

  2. 1个球从100m落下,每次时,反跳原高度的一半,再落,再反弹,求第10次落地共经过多少m,第10次反弹多高。 谭浩强《c语言程序设计》第五章第十一题

    题目 本题是谭浩强<c语言程序设计>第五章第十一题 题目:1个球从100m落下,每次时,反跳原高度的一半,再落,再反弹,求第10次落地共经过多少m,第10次反弹多高. 提示:以下是本篇文章 ...

  3. 谭浩强C语言程序设计代码示例第5章(笔记)

    谭浩强C语音程序设计代码示例第四章(新手入门笔记) 第一章包含参考书,在线编译工具,代码,注释等. 遵循共享互助原则 谭浩强C语言程序设计代码示例(1-3章) 谭浩强C语言程序设计代码示例第4章(笔记 ...

  4. 有一个已排好序的数组,要求输入一个数后,按原来排序的规律将它插入数组中,谭浩强《c语言程序设计》第六章第四题

    题目 本题是谭浩强<c语言程序设计>第六章第四题 题目:有一个已排好序的数组,要求输入一个数后,按原来排序的规律将它插入数组中. 以下是本篇文章正文内容,欢迎朋友们进行指正,一起探讨,共同 ...

  5. 谭浩强C语言程序设计代码示例第6章(笔记)

    谭浩强C语音程序设计代码示例第六章(新手入门笔记) 第一章包含参考书,在线编译工具,代码,注释等. 遵循共享互助原则 谭浩强C语言程序设计代码示例(1-3章) 谭浩强C语言程序设计代码示例第4章(笔记 ...

  6. 用筛选法求100之内的素数。谭浩强《c语言程序设计》第六章第一题

    题目 本题是谭浩强<c语言程序设计>第六章第一题 题目:用筛选法求100之内的素数. 以下是本篇文章正文内容,欢迎朋友们进行指正,一起探讨,共同进步.--来自考研路上的lwj 一.解题思路 ...

  7. C语言程序设计谭浩强(第四版)部分课后习题作答——第六章-输出杨辉三角,输出魔方阵

    代码: 杨辉三角题目较为简单,魔方阵题目实现了输出任意整数即可输出对应的魔方阵 #include<stdio.h> #include<stdlib.h> #include< ...

  8. 《C程序设计》(谭浩强:第四版)笔记梳理

    1. 程序设计和C语言(体验C语言编程) 要求:了解C语言的特点和C程序的组成,理解程序设计的环境 1.1 最简单的C语言程序举例 例1.1 (P6)#include <stdio.h> ...

  9. C语言谭浩强(第五版)答案6、8章

    第六章 利用数组处理批量数据 1.用筛选法求100之内的素数. int main() {int a[100],i,j;for(i=0;i<100;i++)a[i]=i+1;a[0]=0;//因为 ...

  10. C语言程序设计谭浩强(第四版)部分课后习题作答——第七章——7.8,7.10,7.16

    7.8:输入一个4位数,输出这4个数字字符 7.10:输入一行字符,输出最长的单词 7.16:十六进制转十进制 #include<stdio.h> #include<stdlib.h ...

最新文章

  1. 虚拟化帮您打造绿色数据中心
  2. 《高效程序员的45个习惯》-末篇
  3. block与inline,inline和inline-block,块级和行内元素,行内替换和行内非替换元素
  4. react(87)--控制禁用disabled
  5. 数据库工作笔记002---Linux下开启,重启,关闭mysql
  6. 强大的端到端语音识别框架—RNN-T
  7. 在windows 2008 R2上无法运行vc 2015的程序,显示缺少api-ms-win-crt-string-|1-1-0.dll的解决
  8. 封面型网页html,网站设计常用网页版式
  9. Metasploit---端口扫描模块
  10. xlsx的python处理
  11. rtx2060为什么叫智商卡_显卡怎么选,GTX1660TI亦是RTX2060?
  12. TweenMax 参数说明
  13. 三、C语言的数据类型—浮点型数据
  14. 小白入行,测试点随手记
  15. 大脑分区与功能简介汇总
  16. 22春天津大学《国际金融学》在线作业二
  17. 微信扫码背后的图像超分辨率技术
  18. java pdf 签名_java – PDFBox 1.8.10:填充和签名PDF生成无效签名
  19. 家国梦自动收取金币、货物、升级建筑、拆相册等脚本
  20. 游戏角色捏脸原理分析

热门文章

  1. Ubuntu 14.04安装IB卡驱动详细教程
  2. C语言:饮料促销问题
  3. PHPnow PHP环境套件包
  4. Android中的Launcher介绍
  5. git每次git pull,git push时提示enter passphrase for key ‘~/.ssh/id_rsa‘需要输入密码
  6. java handlebars_前端模板引擎Handlebars的使用总结(一)
  7. centos7安装gitblit1.8
  8. 关于win10启动蓝屏恢复修复失败-无法访问应用程序或系统错误代码0xc000000e的修复
  9. 分布式架构关于数据分页问题
  10. 移动平均法 VS 指数平滑法