第九章课后习题

1、构造函数&析构函数

构造函数是C++提供的对类的对象进行初始化的函数,它不需要用户调用,在对象建立时自动执行,构造函数可重载;P246
而析构函数与构造函数作用相反,是在对象生命周期结束自动执行的在对象内存撤销前完成的清理工作(不是删除对象),一个类只能有一个析构函数。P255
构造函数,提高了对对象初始化的效率,主要作用就是用户按照需求初始化,即使用户没有定义构造函数,系统也会自动生成空的构造函数;析构函数主要用于资源释放,对象最后一次调用的操作等,即使用户没有定义系统也会生成空的析构函数。

2、分析程序
#include<iostream>
using namespace std;//印刷少了一行!
class Date{public:Date(int,int ,int);Date(int,int);Date(int);Date();void display();private:int month;int day;int year;
};
Date::Date(int m,int d,int y):month(m),day(d),year(y)
{}//参数初始化表初始化
Date::Date(int m,int d):month(m),day(d){year=2005;
} //指定默认值
Date::Date(int m):month(m){day=1;year=2005;
}
Date::Date(){month=1;day=1;year=2005;
}
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;
}
/*
显然输出是:
10/13/2005
12/30/2005
10/1/2005
1/1/2005
*/
3、第二题第五行修改成Date(int =1,int=1,int=2005);是否可以?怎样修改才能得到正确的答案
//声明构造函数时指定默认参数是可以的,但全部默认参数构造函数会与构造函数重载产生歧义,所以这样显然是错误的P255页(4)条
//修改,直接删掉其它构造函数的重载即可,这样在传入参数按照参数值否则按照默认值,就可以得到和上一问相同的结果
#include<iostream>
using namespace std;
class Date{public:Date(int =1,int =1,int =2005);/*   Date(int,int);Date(int);Date();*/void display();private:int month;int day;int year;
};
Date::Date(int m,int d,int y):month(m),day(d),year(y)
{}//参数初始化表初始化
/*Date::Date(int m,int d):month(m),day(d){year=2005;
} //指定默认值
Date::Date(int m):month(m){day=1;year=2005;
}
Date::Date(){month=1;day=1;year=2005;
}*/
void Date::display() {cout<<month<<"/"<<day<<"/"<<year<<endl;
}
int main(){ Date d1(10,13);Date d2(12,30);Date d3(10);Date d4;d1.display();d2.display();d3.display();d4.display();              return 0;
}
4、建立对象输出,放入五个学生的数据,用指针指向数组首元素,输出1,3,5号学生的数据
#include<iostream>
using namespace std;
class Student{public:Student(int n,double s):num(n),score(s){}void display();private:int num;double score;
};
void Student::display() {cout<<"student:"<<num<<", score:"<<score<<endl;
}
int main(){ Student stu[5]={Student(1001,85),Student(1002,97),Student(1003,78),Student(1004,91),Student(1005,88.5)};Student *p;p=&stu[0];(*p).display();p=p+2;(*p).display();p=p+2;(*p).display();                return 0;
}
5、同上,用指向对象的指针作为函数参数求出五个学生中成绩最高者
#include<iostream>
using namespace std;
class Student{public:Student(int n,double s):num(n),score(s){}void display();double sc();private:int num;double score;
};
void Student::display() {cout<<"student:"<<num<<", score:"<<score<<endl;
}
double Student::sc(){return score;
}
void max(Student *p){double max;max=(*p).sc();//用一个公有成员函数得到scorefor(int i=0;i<5;i++){if((*p).sc()>max) max=(*p).sc();p++;}cout<<"max="<<max;
}
int main(){ Student stu[5]={Student(1001,85),Student(1002,97),Student(1003,78),Student(1004,91),Student(1005,88.5)};Student *p;p=&stu[0];max(p);  return 0;
}
6、阅读分析程序
#include<iostream>
using namespace std;
class Student{public:Student(int n,float s):num(n),score(s){}void display(){ cout<<num<<" "<<score<<endl; }void change(int n,float s){num=n;score=s;}private:int num;float score;
};
int main(){ Student stud(101,78.5);stud.display() ;stud.change(101,80.5);stud.display(); return 0;
}
/*
显然输出:
101 78.5
101 80.5
*/
7、修改6题,运行并分析
//(1)
#include<iostream>
using namespace std;
class Student{public:Student(int n,float s):num(n),score(s){}void display(){ cout<<num<<" "<<score<<endl; }void change(int n,float s){num=n;score=s;}private:int num;float score;
};
int main(){ const Student stud(101,78.5);//如果对象定义为常对象,不可被普通成员函数调用,无法运行 stud.display() ;stud.change(101,80.5);stud.display(); return 0;
}
//(2) P267
#include<iostream>
using namespace std;
class Student{public:Student(int n,float s):num(n),score(s){}void display() const{ cout<<num<<" "<<score<<endl; }//必须声明为常成员函数 void change (int n,float s) const{//声明为常成员函数 num=n;score=s;}private:mutable int num;//如果一定要改变常对象的值,声明为mutable mutable float score;
};
int main(){ const Student stud(101,78.5);stud.display() ;stud.change(101,80.5);stud.display(); return 0;
}
//(3)
#include<iostream>
using namespace std;
class Student{public:Student(int n,float s):num(n),score(s){}void display() { cout<<num<<" "<<score<<endl; }void change (int n,float s){num=n;score=s;}private:int num;float score;
};
int main(){ Student stud(101,78.5);Student *p=&stud; //指向对象的指针p->display() ;//与stud.display();等价p->change(101,80.5);p->display(); return 0;
}
//(4)
#include<iostream>
using namespace std;
class Student{public:Student(int n,float s):num(n),score(s){}void display() const{ cout<<num<<" "<<score<<endl; }void change (int n,float s){num=n;score=s;}private:int num;float score;
};
int main(){ Student stud(101,78.5);const Student *p=&stud; //指向常对象的指针 ,指向非const对象,不能通过指针改变指向对象 p->display() ;//而要通过指针调用成员函数,成员函数必须声明为const ,即不可修改本类的成员 stud.change(101,80.5);//对象本身可以改变,不可通过指针改变对象p->display(); return 0;
}
//(5)
#include<iostream>
using namespace std;
class Student{public:Student(int n,float s):num(n),score(s){}void display(){ cout<<num<<" "<<score<<endl; }void change (int n,float s){num=n;score=s;}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;
}
8、修改6题,增加一个fun用对象的引用作为形参
#include<iostream>
using namespace std;
class Student{public:Student(int n,float s):num(n),score(s){}void display(){ cout<<num<<" "<<score<<endl; }void change(int n,float s){num=n;score=s;}private:int num;float score;
};
void fun(Student &s){s.display();s.change(101,80.5);s.display();
}
int main(){ Student stud(101,78.5);fun(stud);return 0;
}
9、销售商品
//参考P283计算学生平均成绩
#include<iostream>
using namespace std;
class Sale{public:Sale(int n,int q,double p){num=n;quantity=q;price=p;}void sumsale(){//公有成员函数既可以访问静态成员也可以访问非静态成员if(quantity<=10){sum=quantity*price*discount;}else{sum=quantity*price*0.98*discount;}n=quantity;}static double average(){    //静态成员函数只访问静态数据成员不可访问非静态成员,(要访问必须限定对象,那么一定要将对象定义放在前面)return sum/n;}static void display(){cout<<"average:"<<average()<<endl;}    private:int num;int quantity;double price;static double sum;static int n;static double discount;
};
//一般情况下,静态成员函数访问静态数据成员,公有成员函数可以对所有数据成员进行处理
double Sale::discount=0.9;//静态成员只能在类外初始化,属于类
double Sale::sum=0.0;
int Sale::n=0;
int main(){ Sale s1(101,5,23.5);Sale s2(102,12,24.56);Sale s3(103,100,21.5);s1.sumsale();cout<<"No 1:  ";s1.display();s2.sumsale();cout<<"No 2:  ";s2.display();s3.sumsale();cout<<"No 3:  ";s3.display();return 0;
}
10、修改例题9.13,友元
//原程序
#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 void Time::display(Date &);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;
}
//修改
#include<iostream>
using namespace std;
class Date ;
class Time{public:Time(int,int,int);friend void display(Date &,Time &);private:int hour;int minute;int sec;
};
class Date{public:Date(int,int,int);friend void display(Date &,Time &);private:int month;int day;int year;
};
Time::Time(int h,int m,int s){hour=h;minute=m;sec=s;
}
void display(Date &d,Time &t){//普通函数声明为友元cout<<d.month<<"/"<<d.day<<"/"<<d.year<<endl;cout<<t.hour<<":"<<t.minute<<":"<<t.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);display(d1,t1);  return 0;
}
11、修改例题9.13
#include<iostream>
using namespace std;
class Date;
class Time{public:Time(int,int,int);void display(Date &d);//友元类形参仍因该是Date类对象的引用private:int hour;int minute;int sec;
};
class Date{public:Date(int,int,int);friend Time;//Time声明为Date的友元类,Time中所有成员函数可以访问Date成员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;
}
12、修改例题9.14
//原程序
//P292 例9.14类模板
#include<iostream>
using namespace std;
template <class numtype>//声明类模板,虚拟类型名numtype
class Compare{public:Compare(numtype a,numtype b){x=a;y=b;}numtype max(){//类里定义成员函数 return (x>y)?x:y;}numtype min(){return (x>y)?y:x;}private:numtype x,y;
};
int main(){Compare<int> cmp1(3,7);cout<<cmp1.max()<<" is the maximum of the two integers."<<endl;cout<<cmp1.min()<<" is the minimum of the two integers."<<endl<<endl;Compare<float> cmp2(47.8,96.34);cout<<cmp2.max()<<" is the maximum of the two float numbers."<<endl;cout<<cmp2.min()<<" is the minimum of the two float numbers."<<endl<<endl;Compare<char> cmp3('a','A');cout<<cmp3.max()<<" is the maximum of the two characters."<<endl;cout<<cmp3.min()<<" is the minimum of the two characters."<<endl<<endl;return 0;
}
//修改
#include<iostream>
using namespace std;
template <class numtype>//声明类模板,虚拟类型名numtype
class Compare{public:Compare(numtype a,numtype b){x=a;y=b;}numtype max();numtype min(); private:numtype x,y;
};
//template <class numtype> numtype Compare<numtype>::max(){//类外定义成员函数
template <class numtype>//类模板
numtype Compare<numtype>::max(){//注意,第一个numtype是函数的类型,Compare<numtype>是一个整体代表带参的类 return (x>y)?x:y;
}
template <class numtype> numtype Compare<numtype>::min(){ return (x>y)?y:x;
}
int main(){Compare<int> cmp1(3,7);cout<<cmp1.max()<<" is the maximum of the two integers."<<endl;cout<<cmp1.min()<<" is the minimum of the two integers."<<endl<<endl;Compare<float> cmp2(47.8,96.34);cout<<cmp2.max()<<" is the maximum of the two float numbers."<<endl;cout<<cmp2.min()<<" is the minimum of the two float numbers."<<endl<<endl;Compare<char> cmp3('a','A');cout<<cmp3.max()<<" is the maximum of the two characters."<<endl;cout<<cmp3.min()<<" is the minimum of the two characters."<<endl<<endl;return 0;
}

谭浩强C++ 第九章相关推荐

  1. C语言程序设计第五版谭浩强 第七章答案

    C语言程序设计第五版谭浩强著 第七章答案 第七章 用函数实现模块化程序设计 1.写两个函数,分别求两个整数的最大公约数和最小公倍数,用主函数调用这两个函数,并输出结果.两个整数由键盘输人. 题目解析: ...

  2. 谭浩强c语言第7章,清华大学C语言谭浩强第7章.ppt

    清华大学C语言谭浩强第7章.ppt 第7章复合结构类型,第7章 复合结构类型,7.1 结构体类型的概述 7.2 结构体类型的定义 7.3 结构体变量的定义及内存分配 7.4 结构体变量的初始化和引用 ...

  3. c语言教材课后题答案6,C语言谭浩强版6章课后练习题答案.doc

    C语言谭浩强版6章课后练习题答案 第6章课后练习题 单项选择题 以下程序执行后,a是: int a =0,i,j; for (i=5;i>0;i--) for(j=0;j<4;j++) a ...

  4. C++面向对象的程序设计谭浩强 第六章课后题

    以往章节 C++面向对象的程序设计谭浩强 第二章课后题 C++面向对象的程序设计谭浩强 第三章课后题 C++面向对象的程序设计谭浩强 第四章课后题 C++面向对象的程序设计谭浩强 第五章课后题 C++ ...

  5. C语言程序设计第五版 谭浩强 第四章 课后习题 答案

    谭浩强C语言程序设计第五版 第4章 课后习题 答案 点我看视频讲解+可运行源码 记得一键三连哦 第四章 选择结构程序设计 1. 什么是算术运算?什么是关系运算?什么是逻辑运算? [答案解析] 算熟运算 ...

  6. c++程序设计梳理(谭浩强)3-4章

    为什么80%的码农都做不了架构师?>>>    同学拿了58的offer,22万一年,内心多少有点不是滋味.自己享受着这么丁点的中兴薪酬,明显是学业不精的那一类.事实确实如此,可当现 ...

  7. c语言谭浩强第六章答案,C语言谭浩强版本第6章课后练习题答案..doc

    第6章课后练习题 单项选择题 以下程序执行后,a是: int a =0,i,j; for (i=5;i>0;i--) for(j=0;j<4;j++) a++; 20 25 24 30 执 ...

  8. 谭浩强C++ 第二章

    第二章课后习题 1.变量为什么先定义后使用. 变量必须强制定义:(1)保证程序中变量名使用正确:(2)确定类型编译时可以分配相应存储单元:(3)编译时,检查该变量运算是否合法.(见课本P27) 2.字 ...

  9. 谭浩强C++ 第一章(第三版)

    第一章课后习题 1.简述C++的特点,它对C的发展. C++的特点:保留并扩充了C基于过程语言的特征,增加了面向对象的机制,其主要特点是:抽象.封装(信息隐蔽).继承和多态性.(见课本P215~P21 ...

最新文章

  1. Java学习之if---elif语句
  2. IromPython .Net 的简介和第一个例子
  3. BLE-NRF51822教程11-手机动态修改设备名
  4. CodeForces - 888G Xor-MST(贪心+字典树+最小生成树)
  5. mysql 从库可以写入吗_mysql主从库配置读写分离以及备份
  6. Python深层解析json数据之JsonPath
  7. Quartz实线定期运行程序(Java)
  8. react里面的this_React 为啥要绑定this
  9. autogen.sh 的使用
  10. C 远程登录linux,远程登录Linux主机进行C编程的操作方法简述.doc
  11. 设计学习---《大象》之系统分析
  12. python标准库os_Python标准库 os
  13. [原创]测试用例设计之“功能图”法
  14. Spring源码的学习方法和知识地图
  15. 【古典密码】 凯撒密码 移位变换 仿射变换 多表代换 Python
  16. win10桌面管理文件收纳_放心的电脑桌面收纳工具必备,电脑桌面win10应用商店
  17. 微信小程序选项卡功能
  18. linux日志关键词高亮,【转载】Linux使用tailf高亮显示关键字
  19. 化学实用计算机技能,实用化学化工计算机软件基础
  20. 判断质数的所有方法详解(C语言)

热门文章

  1. 有若干只鸡兔同在一个笼子里,从上面数,有35个头;从下面数,有94只脚。求笼中各有几只鸡和兔?
  2. HyperLynx(六)参数扫描仿真
  3. 统计学的Python实现-012:峰度
  4. 【数模】数据统计中的峰度与偏度
  5. 提示猜数字范围python_Python 猜数字游戏
  6. 关于SVN:cleanup失败提示:failed to run the WC DB work queue associated with ‘E:XXX’
  7. LINUX漏洞复现篇之ShellShock漏洞
  8. 部署和访问HTTP接口
  9. JAVA SE程序设计及实践
  10. 外文文献下载网站;数据获取网站;中文文献下载网站;论文原创性保真网站;外包项目申请网站;大数据比赛收录网站;提高编程能力;代码分享网站