├── README.md ├── chapter_02 ├── 100~200不能被3整除的数 P51.cpp ├── 习题2-16 输入一个数并显示.cpp ├── 习题2-26 现在正在下雨吗.cpp ├── 习题2-27 考了多少分.cpp ├── 例2-1 Hello C++.cpp ├── 例2-10 统计正负数个数.cpp ├── 例2-11 体育比赛.cpp ├── 例2-2判断是否闰年.cpp ├── 例2-3比较两数大小.cpp ├── 例2-4 转换星期输出.cpp ├── 例2-5 1~10累加.cpp ├── 例2-6 数字反转输出.cpp ├── 例2-7 求和1-10dowhile.cpp ├── 例2-8 求因子.cpp ├── 例2-9输出图案.cpp ├── 循环结构嵌套.cpp ├── 比较while和dowhile1 P47.cpp └── 比较while和dowhile2 P47.cpp ├── chapter_03 ├── 例3-1 求x的n次方.cpp ├── 例3-10 汉诺塔问题.cpp ├── 例3-11 两个数交换次序输出(值传递).cpp ├── 例3-12 两个数交换次序输出(引用传递).cpp ├── 例3-13 值传递与引用传递的比较.cpp ├── 例3-14 内联函数求圆面积.cpp ├── 例3-15 带默认形参值的函数举例 求长方体体积.cpp ├── 例3-16 重载函数举例 求平方和.cpp ├── 例3-17 输出三角函数值.cpp ├── 例3-2 二进制转十进制.cpp ├── 例3-3 求PI的值.cpp ├── 例3-4 求11~999的回文数.cpp ├── 例3-5 分段函数.cpp ├── 例3-6 投骰子游戏.cpp ├── 例3-7 求两数平方和.cpp ├── 例3-8 求n的阶乘.cpp └── 例3-9 n个人里选k个人的组合数.cpp ├── chapter_04 ├── 例4-1 Clock类.cpp ├── 例4-10 结构体存储成绩信息.cpp ├── 例4-2 Point类.cpp ├── 例4-3 游泳池改造预算 Circle类.cpp ├── 例4-4 类的组合Line类.cpp ├── 例4-7 结构体表示学生信息.cpp ├── 例4-8 联合体保存成绩信息.cpp └── 例4-9 个人银行账户管理程序.cpp ├── chapter_05 ├── 例5-1 作用域实例.cpp ├── 例5-10 具有静态数据、函数成员的Point类,多文件组织 │ ├── Point.h │ ├── 例5-10 具有静态数据、函数成员的Point类,多文件组织.dev │ ├── 文件2类实现.cpp │ └── 文件3主函数.cpp ├── 例5-11 个人银行账户管理程序 │ ├── 5_11.cpp │ ├── account.cpp │ ├── account.h │ └── 例5-11 个人银行账户管理程序.dev ├── 例5-2 变量的生存期与可见性.cpp ├── 例5-3 具有静态和动态生存期对象的时钟程序.cpp ├── 例5-4 具有静态数据成员的Point类.cpp ├── 例5-5 具有静态数据和函数成员的Point类.cpp ├── 例5-6 使用友元函数计算两点间的距离.cpp ├── 例5-7 常成员函数举例.cpp ├── 例5-8 常数据成员举例.cpp └── 例5-9 常引用作形参.cpp ├── chapter_06 ├── 例6-1 数组的声明与使用.cpp ├── 例6-10 整数部分小数部分分别输出.cpp ├── 例6-11 函数指针实例.cpp ├── 例6-12 使用指针来访问Point类的成员.cpp ├── 例6-13 访问对象公有成员函数的不同方式.cpp ├── 例6-14 通过指针访问类的静态数据成员.cpp ├── 例6-15 通过指针访问类的静态函数成员.cpp ├── 例6-16 动态创建对象.cpp ├── 例6-17 动态创建对象数组.cpp ├── 例6-18 动态数组类.cpp ├── 例6-19 动态创建多维数组.cpp ├── 例6-2 使用数组名作为函数参数.cpp ├── 例6-20 vector应用举例.cpp ├── 例6-21 对象的浅复制.cpp ├── 例6-22 对象的深复制.cpp ├── 例6-23 string类应用举例.cpp ├── 例6-24 用getline输入字符串.cpp ├── 例6-25 个人银行账户管理程序改进 │ ├── 6_25.cpp │ ├── account.cpp │ ├── account.h │ ├── date.cpp │ ├── date.h │ └── 例6-25 个人银行账户管理程序改进.dev ├── 例6-3 对象数组应用举例 │ ├── 6_3.cpp │ ├── Point.cpp │ ├── Point.h │ └── 例6-3 对象数组应用举例.dev ├── 例6-4 利用Point类进行点的线性组合 │ ├── 6_4.cpp │ ├── Point.h │ └── 例6-4 利用Point类进行点的线性组合.dev ├── 例6-5 指针的定义、赋值与使用.cpp ├── 例6-6 void类型指针的使用.cpp ├── 例6-7 三种方法输出int型数组 │ ├── 法一 使用数组名和下标.cpp │ ├── 法三 使用指针变量.cpp │ └── 法二 数组名和指针运算.cpp ├── 例6-8 利用指针数组输出单位矩阵.cpp └── 例6-9 二维数组举例.cpp ├── chapter_07 ├── 例7-1 Point类公有继承 │ ├── 7_1.cpp │ ├── Point.h │ ├── Rectangle.h │ └── 例7-1 Point类公有继承.dev ├── 例7-10 个人银行账户管理程序 │ ├── 7_10.cpp │ ├── account.cpp │ ├── account.h │ ├── accumulator.h │ ├── date.cpp │ ├── date.h │ └── 例7-10 个人银行账户管理程序.dev ├── 例7-2 Point类私有继承 │ ├── 7_2.cpp │ ├── Point.h │ ├── Rectangle.h │ └── 例7-2 Point类私有继承.dev ├── 例7-3 类型兼容规则实例.cpp ├── 例7-4 派生类构造函数举例(多继承、含有内嵌对象).cpp ├── 例7-5 派生类析构函数举例(多继承、含有嵌入对象).cpp ├── 例7-6 多继承同名隐藏举例(1).cpp ├── 例7-7 多继承同名隐藏举例(2).cpp ├── 例7-8 虚基类举例.cpp ├── 例7-8-2 虚基类声明带参数的构造函数.cpp └── 例7-9 全选主元高斯消去法解线性方程组 │ ├── 7_9.cpp │ ├── LinearEqu.cpp │ ├── LinearEqu.h │ ├── Matrix.cpp │ ├── Matrix.h │ └── 例7-9 全选主元高斯消去法解线性方程组.dev ├── chapter_08 ├── 例8-1 复数类加减法运算重载为成员函数形式.cpp ├── 例8-10 typeid用法示例.cpp ├── 例8-2 将单目运算符“++”重载为成员函数形式.cpp ├── 例8-3 以非成员函数形式重载Complex的加减法运算和左移运算符.cpp ├── 例8-4 虚函数成员.cpp ├── 例8-5 虚析构函数举例.cpp ├── 例8-6 抽象类举例.cpp ├── 例8-7 变步长梯形积分法求函数的定积分 │ ├── 8_7.cpp │ ├── Trapzint.cpp │ ├── Trapzint.h │ └── 例8-7 变步长梯形积分法求解函数的定积分.dev ├── 例8-8 个人银行账户管理程序改进 │ ├── 8_8.cpp │ ├── account.cpp │ ├── account.h │ ├── accumulator.h │ ├── date.cpp │ ├── date.h │ └── 例8-8 个人银行账户管理程序改进.dev └── 例8-9 dynamic_cast用法示例.cpp └── chapter_09 ├── 例9-1 函数模板的示例.cpp └── 例9-2 类模板应用举例.cpp /README.md: -------------------------------------------------------------------------------- 1 | # Cpp_4th 2 | 清华大学计算机系列教材 《C++语言程序设计(第4版)》 郑莉 董渊 何江舟 编著 清华大学出版社 3 | -------------------------------------------------------------------------------- /chapter_02/100~200不能被3整除的数 P51.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | for(int n=100;n<=200;n++){ 5 | if(n%3!=0) 6 | cout< 4 | using namespace std; 5 | 6 | int main(){ 7 | int a; 8 | cout<<"请输入一个数:"; 9 | cin>>a; 10 | cout<<"这个数为:"< 7 | using namespace std; 8 | 9 | int main(){ 10 | char que; 11 | cout<<"现在下雨吗?(请输入Y或N)"<>que; 13 | while(que){ 14 | if(que=='Y'){ 15 | cout<<"现在正在下雨。"; 16 | break; 17 | } 18 | else if(que=='N'){ 19 | cout<<"现在没有下雨。"; 20 | break; 21 | } 22 | else 23 | cout<<"现在下雨吗?(请输入Y或N)"<>que; 25 | continue; 26 | } 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /chapter_02/习题2-27 考了多少分.cpp: -------------------------------------------------------------------------------- 1 | //习题2-27.向用户提问“你考试考了多少分?(0~100)”,接受输入后判断其等级显示出来。规则如下: 2 | //优(90~100)、良(80~90)、中(60~80)、差(0~60)。 3 | 4 | 5 | 6 | 7 | #include 8 | using namespace std; 9 | 10 | int main(){ 11 | int score; 12 | cout<<"你考试考了多少分?(0~100)"<>score; 14 | if(score>=90&&score<=100) 15 | cout<<"优"<=80&&score<90) 17 | cout<<"良"<=60&&score<80) 19 | cout<<"中"< 2 | using namespace std; 3 | int main(){ 4 | cout<<"Hello!"< 2 | using namespace std; 3 | 4 | int main(){ 5 | int i=0,j=0,n; 6 | cout<<"Enter some integers please(enter 0 to quit):"<>n; 8 | while(n!=0){ 9 | if(n>0) i+=1; 10 | if(n<0) j+=1; 11 | cin>>n; 12 | } 13 | 14 | cout<<"Count of positive integers: "< 2 | using namespace std; 3 | 4 | enum GameResult {WIN,LOSE,TIE,CANCEL}; 5 | 6 | int main(){ 7 | GameResult result; 8 | enum GameResult omit=CANCEL; 9 | 10 | for(int count=WIN;count<=CANCEL;count++){ 11 | result=GameResult(count); 12 | if(result==omit) 13 | cout<<"The game was cancelled"< 2 | using namespace std; 3 | int main(){ 4 | int year; 5 | bool isLeapYear; 6 | 7 | cout<<"Enter the year:"; 8 | cin>>year; 9 | isLeapYear=((year%4==0&&year%100!=0)||(year%400==0)); 10 | 11 | if(isLeapYear) 12 | cout< 4 | using namespace std; 5 | int main(){ 6 | int x,y; 7 | cout<<"Enter x and y:"; 8 | cin>>x>>y; 9 | 10 | if(x!=y) 11 | if(x>y) 12 | cout<<"x>y"< 2 | using namespace std; 3 | int main(){ 4 | int day; 5 | 6 | cin>>day; 7 | switch (day){ 8 | case 0: 9 | cout<<"Sunday"< 2 | using namespace std; 3 | int main(){ 4 | int i=1,sum=0; 5 | while(i<=10){ 6 | sum+=i; 7 | i++; 8 | } 9 | cout<<"sum="< 2 | using namespace std; 3 | 4 | int main(){ 5 | int n,right_digit,newnum=0; 6 | cout<<"Enter the number:"; 7 | cin>>n; 8 | 9 | cout<<"The number in reverse order is:"; 10 | do{ 11 | right_digit=n%10; 12 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | int i=1,sum=0; 6 | do{ 7 | sum+=i; 8 | i++; 9 | }while(i<=10); 10 | cout<<"sum="< 2 | using namespace std; 3 | 4 | int main(){ 5 | int n; 6 | 7 | cout<<"Enter a positive integer:"; 8 | cin>>n; 9 | cout<<"Number "< 2 | using namespace std; 3 | 4 | int main(){ 5 | const int N=4; 6 | for(int i=1;i<=N;i++){ 7 | for(int j=1;j<=30;j++) 8 | cout<<' '; 9 | for(int j=1;j<=8-2*i;j++) 10 | cout<<' '; 11 | for(int j=1;j<=2*i-1;j++) 12 | cout<<'*'; 13 | cout< 2 | using namespace std; 3 | int main() { 4 | int i=1,a=0; 5 | for (;i<=5;i++){ 6 | do { 7 | i++; 8 | a++; 9 | } while(i<3); 10 | i++; 11 | } 12 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | int i,sum=0; 6 | cin>>i; 7 | while(i<=10){ 8 | sum+=i; 9 | i++; 10 | } 11 | cout<<"sum="< 2 | using namespace std; 3 | 4 | int main(){ 5 | int i,sum=0; 6 | cin>>i; 7 | do{ 8 | sum+=i; 9 | i++; 10 | }while(i<=10); 11 | cout<<"sum="< 2 | using namespace std; 3 | 4 | double power(double x,int n){ 5 | double val=1.0; 6 | while(n--) 7 | val*=x; 8 | return val; 9 | } 10 | 11 | int main(){ 12 | cout<<"5 to the power 2 is "< 2 | using namespace std; 3 | 4 | void move(char src,char dest){ 5 | cout<"<>m; 22 | cout<<"the steps to moving "< 2 | using namespace std; 3 | 4 | void swap(int a,int b){ 5 | int t=a; 6 | a=b; 7 | b=t; 8 | } 9 | 10 | int main(){ 11 | int x=5,y=10; 12 | cout<<"x="< 2 | using namespace std; 3 | 4 | const double PI=3.14159265358979; 5 | 6 | inline double calArea(double radius){ 7 | return PI*radius*radius; 8 | } 9 | 10 | int main(){ 11 | double r=3.0; 12 | double area=calArea(r); 13 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int getVolume(int length,int width=2,int height=3); 6 | 7 | int main(){ 8 | const int X=10,Y=12,Z=15; 9 | cout<<"Some box data is "; 10 | cout< 2 | using namespace std; 3 | 4 | int sumOfSquare(int a,int b){ 5 | return a*a+b*b; 6 | } 7 | 8 | double sumOfSquare(double a,double b){ 9 | return a*a+b*b; 10 | } 11 | 12 | int main(){ 13 | int m,n; 14 | cout<<"Enter two integers: "; 15 | cin>>m>>n; 16 | cout<<"Their sum of square: "<>x>>y; 21 | cout<<"Their sum of square: "< 2 | #include 3 | using namespace std; 4 | 5 | const double PI=3.14159265358979; 6 | 7 | int main(){ 8 | double angle; 9 | cout<<"Please enter an angle: "; 10 | cin>>angle; 11 | 12 | double radian=angle*PI/180; 13 | cout<<"sin("<=0;i--){ 11 | char ch; 12 | cin>>ch; 13 | if(ch=='1') 14 | value+=static_cast(power(2,i)); 15 | } 16 | cout<<"Decimal value is "< 2 | using namespace std; 3 | 4 | double arctan(double x){ 5 | double sqr=x*x; 6 | double e=x; 7 | double r=0; 8 | int i=1; 9 | while(e/i>1e-15){ 10 | double f=e/i; 11 | r=(i%4==1)?r+f:r-f; 12 | e=e*sqr; 13 | i+=2; 14 | } 15 | return r; 16 | } 17 | 18 | int main(){ 19 | double a=16.0*arctan(1/5.0); 20 | double b=4.0*arctan(1/239.0); 21 | cout<<"PI="< 2 | using namespace std; 3 | 4 | bool symm(unsigned n){ 5 | unsigned i=n; 6 | unsigned m=0; 7 | while(i>0){ 8 | m=m*10+i%10; 9 | i/=10; 10 | } 11 | return m==n; 12 | } 13 | 14 | int main(){ 15 | for(unsigned m=11;m<1000;m++) 16 | if(symm(m)&&symm(m*m)&&symm(m*m*m)){ 17 | cout<<"m="<>seed; 22 | srand(seed); 23 | 24 | sum=rollDice(); 25 | switch(sum){ 26 | case 7: 27 | case 11: 28 | status=WIN; 29 | break; 30 | case 2: 31 | case 3: 32 | case 13: 33 | status=LOSE; 34 | break; 35 | default: 36 | status=PLAYING; 37 | myPoint=sum; 38 | cout<<"Point is "< 2 | using namespace std; 3 | 4 | int fun2(int m){ 5 | return m*m; 6 | } 7 | 8 | int fun1(int x,int y){ 9 | return fun2(x)+fun2(y); 10 | } 11 | 12 | int main(){ 13 | int a ,b; 14 | cout<<"Please enter two integers(a and b): "; 15 | cin>>a>>b; 16 | cout<<"The sum of square of a and b: "< 2 | using namespace std; 3 | 4 | unsigned fac(unsigned n){ 5 | unsigned f; 6 | if(n==0) 7 | f=1; 8 | else 9 | f=fac(n-1)*n; 10 | return f; 11 | } 12 | 13 | int main(){ 14 | unsigned n; 15 | cout<<"Enter a positive integer: "; 16 | cin>>n; 17 | unsigned y=fac(n); 18 | cout< 2 | using namespace std; 3 | 4 | int comm(int n,int k){ 5 | if(k>n) 6 | return 0; 7 | else if(n==k||k==0) 8 | return 1; 9 | else 10 | return comm(n-1,k)+comm(n-1,k-1); 11 | } 12 | 13 | int main(){ 14 | int n,k; 15 | cout<<"Please enter two integers n and k: "; 16 | cin>>n>>k; 17 | cout<<"C(n,k)="< 4 | using namespace std; 5 | 6 | class Clock{ //时钟类的定义 7 | public: //外部接口,公有成员函数 8 | void setTime(int newH=0,int newM=0,int newS=0); 9 | void showTime(); 10 | private: //私有数据成员 11 | int hour,minute,second; 12 | }; 13 | 14 | //时钟类成员函数的具体实现 15 | void Clock::setTime(int newH,int newM,int newS){ 16 | hour=newH; 17 | minute=newM; 18 | second=newS; 19 | } 20 | 21 | inline void Clock::showTime(){ 22 | cout< 5 | using namespace std; 6 | 7 | enum Level{FRESHMAN,SOPHOMORE,JUNIOR,SENIOR}; 8 | enum Grade{A,B,C,D}; 9 | class Student{ 10 | public: 11 | Student(unsigned number,Level level,Grade grade):number(number),level(level),grade(grade){} 12 | void show(); 13 | private: 14 | unsigned number:27; 15 | Level level:2; 16 | Grade grade:2; 17 | }; 18 | 19 | void Student::show(){ 20 | cout<<"Number: "< 4 | using namespace std; 5 | 6 | class Point{ //Point类的定义 7 | public: //外部接口 8 | Point(int xx=0,int yy=0){ //构造函数 9 | x=xx; 10 | y=yy; 11 | } 12 | Point(Point &p); //复制构造函数 13 | int getX(){ 14 | return x; 15 | } 16 | int getY(){ 17 | return y; 18 | } 19 | private: //私有数据 20 | int x,y; 21 | }; 22 | 23 | //成员函数的实现 24 | Point::Point(Point &p){ 25 | x=p.x; 26 | y=p.y; 27 | cout<<"Calling the copy constructor"< 2 | using namespace std; 3 | 4 | const float PI=3.141593; //给出PI的值 5 | const float FENCE_PRICE=35; //栅栏的单价 6 | const float CONCRETE_PRICE=20; //过道水泥单价 7 | 8 | class Circle{ //声明定义类Circle及其数据和方法 9 | public: //外部接口 10 | Circle(float r); //构造函数 11 | float circumference(); //计算圆的周长 12 | float area(); //计算圆的面积 13 | private: //私有数据成员 14 | float radius; //圆的半径 15 | }; 16 | 17 | 18 | //类的实现 19 | //构造函数初始化数据成员 radius 20 | Circle::Circle(float r){ 21 | radius=r; 22 | } 23 | 24 | //计算圆的周长 25 | float Circle::circumference(){ 26 | return 2*PI*radius; 27 | } 28 | 29 | //计算圆的面积 30 | float Circle::area(){ 31 | return PI*radius*radius; 32 | } 33 | 34 | //主函数实现 35 | int main(){ 36 | float radius; 37 | cout<<"Enter the radius of the pool: "; //提示用户输入半径 38 | cin>>radius; 39 | Circle pool(radius); //游泳池边界对象 40 | Circle poolRim(radius+3); //栅栏对象 41 | 42 | //计算栅栏造价输出 43 | float fenceCost=poolRim.circumference()*FENCE_PRICE; 44 | cout<<"Fencing Cost is $"< 4 | #include 5 | using namespace std; 6 | 7 | class Point{ //Point类定义 8 | public: 9 | Point(int xx=0,int yy=0){ //构造函数 10 | x=xx; 11 | y=yy; 12 | } 13 | Point(Point &p); //复制构造函数 14 | int getX(){return x;} 15 | int getY(){return y;} 16 | private: 17 | int x,y; 18 | }; 19 | 20 | Point::Point(Point &p){ //复制构造函数的实现 21 | x=p.x; 22 | y=p.y; 23 | cout<<"Calling the copy constructor of Point"<(p1.getX()-p2.getX()); 41 | double y=static_cast(p1.getY()-p2.getY()); 42 | len=sqrt(x*x+y*y); 43 | } 44 | 45 | //组合类的复制构造函数 46 | Line::Line(Line &l):p1(l.p1),p2(l.p2){ 47 | cout<<"Calling the copy constructor of Line"< 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | struct Student{ //学生信息结构体 9 | int num; //学号 10 | string name; //姓名 11 | char sex; //性别 12 | int age; //年龄 13 | }; 14 | 15 | int main(){ 16 | Student stu={2014307,"Xu Naonao",'M',23}; 17 | cout<<"Num: "< 2 | #include 3 | using namespace std; 4 | 5 | class ExamInfo{ 6 | public: 7 | //3种构造函数,分别用等级、是否通过和百分制来初始化 8 | ExamInfo(string name,char grade):name(name),mode(GRADE),grade(grade){} 9 | ExamInfo(string name,bool pass):name(name),mode(PASS),pass(pass){} 10 | ExamInfo(string name,int percent):name(name),mode(PERCENTAGE),percent(percent){} 11 | void show(); 12 | 13 | private: 14 | string name; //课程名称 15 | enum{ 16 | GRADE, 17 | PASS, 18 | PERCENTAGE 19 | }mode; //采用何种计分方式 20 | union{ 21 | char grade; //等级制的成绩 22 | bool pass; //是否通过 23 | int percent; //百分制的成绩 24 | }; 25 | }; 26 | 27 | void ExamInfo::show(){ 28 | cout< 4 | #include 5 | using namespace std; 6 | 7 | class SavingsAccount{ //储蓄账户类定义 8 | private: 9 | int id; //账号 10 | double balance; //余额 11 | double rate; //存款的年利率 12 | int lastDate; //上次变更余额的时期 13 | double accumulation; //余额按日累加之和 14 | //记录一笔账,date为日期,amount为金额,desc为说明 15 | void record(int date,double amount); 16 | //获得到指定日期为止的存款金额按日累加值 17 | double accumulate(int date) const{ 18 | return accumulation+balance*(date-lastDate);} 19 | public: 20 | //构造函数 21 | SavingsAccount(int date,int id,double rate); 22 | int getId(){return id;} 23 | double getBalance(){return balance;} 24 | double getRate(){return rate;} 25 | void deposit(int date,double amount); //存入现金 26 | void withdraw(int date,double amount); //取出现金 27 | //结算利息,每年1月1日调用一次该函数 28 | void settle(int date); 29 | //显示账户信息 30 | void show(); 31 | }; 32 | 33 | //SavingsAccount类相关成员函数的实现 34 | SavingsAccount::SavingsAccount(int date,int id,double rate):id(id),balance(0),rate(rate),lastDate(date),accumulation(0){ 35 | cout<getBalance()) 52 | cout<<"Error:not enough money"< 5 | using namespace std; 6 | 7 | int main(){ 8 | cout<<"Entering main..."< 4 | #include "Point.h" 5 | using namespace std; 6 | 7 | Point::Point(){ 8 | x=y=0; 9 | cout<<"Default Constructor called."< 4 | #include 5 | #include "Rectangle.h" 6 | using namespace std; 7 | 8 | int main(){ 9 | Rectangle rect; //定义Rectangle类的对象 10 | rect.initRectangle(2,3,20,10); //设置矩形的数据 11 | rect.move(3,2); //移动矩形位置 12 | cout<<"The data of rect(x,y,w,h): "<x=x;this->y=y;} 9 | void move(float offX,float offY){x+=offX;y+=offY;} 10 | float getX() const {return x;} 11 | float getY() const {return y;} 12 | private: //私有数据成员 13 | float x,y; 14 | }; 15 | 16 | #endif //_POINT_H 17 | -------------------------------------------------------------------------------- /chapter_07/例7-1 Point类公有继承/Rectangle.h: -------------------------------------------------------------------------------- 1 | //Rectangle.h 2 | 3 | #ifndef _RECTANGLE_H 4 | #define _RECTANGLE_H 5 | #include "Point.h" 6 | 7 | class Rectangle:public Point{ //派生类定义部分 8 | public: //新增公有函数成员 9 | void initRectangle(float x,float y,float w,float h){ 10 | initPoint(x,y); //调用基类公有函数成员 11 | this->w=w; 12 | this->h=h; 13 | } 14 | float getH() const {return h;} 15 | float getW() const {return w;} 16 | private: //新增私有数据成员 17 | float w,h; 18 | }; 19 | 20 | #endif //_RECTANGLE_H 21 | -------------------------------------------------------------------------------- /chapter_07/例7-1 Point类公有继承/例7-1 Point类公有继承.dev: -------------------------------------------------------------------------------- 1 | [Project] 2 | FileName=例7-1 Point类公有继承.dev 3 | Name=项目1 4 | Type=1 5 | Ver=2 6 | ObjFiles= 7 | Includes= 8 | Libs= 9 | PrivateResource= 10 | ResourceIncludes= 11 | MakeIncludes= 12 | Compiler= 13 | CppCompiler= 14 | Linker= 15 | IsCpp=1 16 | Icon= 17 | ExeOutput= 18 | ObjectOutput= 19 | LogOutput= 20 | LogOutputEnabled=0 21 | OverrideOutput=0 22 | OverrideOutputName= 23 | HostApplication= 24 | UseCustomMakefile=0 25 | CustomMakefile= 26 | CommandLine= 27 | Folders= 28 | IncludeVersionInfo=0 29 | SupportXPThemes=0 30 | CompilerSet=4 31 | CompilerSettings=0000000100000000001000000 32 | UnitCount=3 33 | 34 | [VersionInfo] 35 | Major=1 36 | Minor=0 37 | Release=0 38 | Build=0 39 | LanguageID=1033 40 | CharsetID=1252 41 | CompanyName= 42 | FileVersion= 43 | FileDescription=Developed using the Dev-C++ IDE 44 | InternalName= 45 | LegalCopyright= 46 | LegalTrademarks= 47 | OriginalFilename= 48 | ProductName= 49 | ProductVersion= 50 | AutoIncBuildNr=0 51 | SyncProduct=1 52 | 53 | [Unit1] 54 | FileName=Point.h 55 | CompileCpp=1 56 | Folder= 57 | Compile=1 58 | Link=1 59 | Priority=1000 60 | OverrideBuildCmd=0 61 | BuildCmd= 62 | 63 | [Unit2] 64 | FileName=Rectangle.h 65 | CompileCpp=1 66 | Folder= 67 | Compile=1 68 | Link=1 69 | Priority=1000 70 | OverrideBuildCmd=0 71 | BuildCmd= 72 | 73 | [Unit3] 74 | FileName=7_1.cpp 75 | CompileCpp=1 76 | Folder= 77 | Compile=1 78 | Link=1 79 | Priority=1000 80 | OverrideBuildCmd=0 81 | BuildCmd= 82 | 83 | -------------------------------------------------------------------------------- /chapter_07/例7-10 个人银行账户管理程序/7_10.cpp: -------------------------------------------------------------------------------- 1 | //7_10.cpp 主函数文件 2 | 3 | #include "account.h" 4 | #include 5 | using namespace std; 6 | 7 | int main(){ 8 | Date date(2008,11,1); //起始日期 9 | //建立几个账户 10 | SavingsAccount sa1(date,"S3755217",0.015); 11 | SavingsAccount sa2(date,"02342342",0.015); 12 | CreditAccount ca(date,"C5392394",10000,0.0005,50); 13 | //11月份的几笔账目 14 | sa1.deposit(Date(2008,11,5),5000,"salary"); 15 | ca.withdraw(Date(2008,11,15),2000,"buy a cell"); 16 | sa2.deposit(Date(2008,11,25),10000,"sell stock 0323"); 17 | //结算信用卡 18 | ca.settle(Date(2008,12,1)); 19 | //12月份的几笔账目 20 | ca.deposit(Date(2008,12,1),2016,"repay the credit"); 21 | sa1.deposit(Date(2008,12,5),5500,"salary"); 22 | //结算所有账户 23 | sa1.settle(Date(2009,1,1)); 24 | sa2.settle(Date(2009,1,1)); 25 | ca.settle(Date(2009,1,1)); 26 | //输出各个账户的信息 27 | cout< 5 | #include 6 | using namespace std; 7 | 8 | double Account::total=0; 9 | //Account类的实现 10 | Account::Account(const Date &date,const string &id):id(id),balance(0){ 11 | date.show(); 12 | cout<<"\t#"<getBalance()){ 33 | error("not enough money"); 34 | } 35 | else{ 36 | record(date,-amount,desc); 37 | acc.change(date,getBalance()); 38 | } 39 | } 40 | void SavingsAccount::settle(const Date &date){ //计算年息 41 | double interest=acc.getSum(date)*rate/date.distance(Date(date.getYear()-1,1,1)); 42 | if(interest!=0) record(date,interest,"interest"); 43 | acc.reset(date,getBalance()); 44 | } 45 | //CreditAccount类相关成员函数的实现 46 | CreditAccount::CreditAccount(const Date &date,const string &id,double credit,double rate,double fee) 47 | :Account(date,id),credit(credit),rate(rate),fee(fee),acc(date,0){} 48 | void CreditAccount::deposit(const Date &date,double amount,const string &desc){ 49 | record(date,amount,desc); 50 | acc.change(date,getDebt()); 51 | } 52 | void CreditAccount::withdraw(const Date &date,double amount,const string &desc){ 53 | if(amount-getBalance()>credit){ 54 | error("not enough credit"); 55 | } 56 | else{ 57 | record(date,-amount,desc); 58 | acc.change(date,getDebt()); 59 | } 60 | } 61 | void CreditAccount::settle(const Date &date){ 62 | double interest=acc.getSum(date)*rate; 63 | if(interest!=0) record(date,interest,"interest"); 64 | if(date.getMonth()==1) 65 | record(date,-fee,"annual fee"); 66 | acc.reset(date,getDebt()); 67 | } 68 | void CreditAccount::show() const{ 69 | Account::show(); 70 | cout<<"\tAvailable credit:"< 9 | 10 | class Account{ //账户类 11 | private: 12 | std::string id; //账号 13 | double balance; //余额 14 | static double total; //所有账户的总金额 15 | protected: 16 | //供派生类调用的构造函数,id为账户 17 | Account(const Date &date,const std::string &id); 18 | //记录一笔账,date为日期,amount为金额,desc为说明 19 | void record(const Date &date,double amount,const std::string &desc); 20 | //报告错误信息 21 | void error(const std::string &msg) const; 22 | public: 23 | const std::string &getId() const {return id;} 24 | double getBalance() const {return balance;} 25 | static double getTotal() {return total;} 26 | //显示账户信息 27 | void show() const; 28 | }; 29 | 30 | class SavingsAccount:public Account{ //储蓄账户类 31 | private: 32 | Accumulator acc; //辅助计算利息的累加器 33 | double rate; //存款的年利率 34 | public: 35 | //构造函数 36 | SavingsAccount(const Date &date,const std::string &id,double rate); 37 | double getRate() const {return rate;} 38 | //存入现金 39 | void deposit(const Date &date,double amount,const std::string &desc); 40 | //取出现金 41 | void withdraw(const Date &date,double amount,const std::string &desc); 42 | void settle(const Date &date); //结算利息,每年1月1日调用一次该函数 43 | }; 44 | 45 | class CreditAccount:public Account{ //信用账户类 46 | private: 47 | Accumulator acc; //辅助计算利息的累加器 48 | double credit; //信用额度 49 | double rate; //欠款的日利率 50 | double fee; //信用卡年费 51 | double getDebt() const{ //获得欠款额 52 | double balance=getBalance(); 53 | return (balance<0?balance:0); 54 | } 55 | public: 56 | //构造函数 57 | CreditAccount(const Date &date,const std::string &id,double credit,double rate,double fee); 58 | double getCredit() const {return credit;} 59 | double getRate() const {return rate;} 60 | double getFee() const {return fee;} 61 | double getAvailableCredit() const{ //获得可用信用额度 62 | if(getBalance()<0) 63 | return credit+getBalance(); 64 | else 65 | return credit; 66 | } 67 | //存入现金 68 | void deposit(const Date &date,double amount,const std::string &desc); 69 | //取出现金 70 | void withdraw(const Date &date,double amount,const std::string &desc); 71 | void settle(const Date &date); //结算利息和年费,每月1日调用一次该函数 72 | void show() const; 73 | }; 74 | 75 | #endif //__ACCOUNT_H__ 76 | -------------------------------------------------------------------------------- /chapter_07/例7-10 个人银行账户管理程序/accumulator.h: -------------------------------------------------------------------------------- 1 | //accumulator.h 按日将数值累加的Accumulator类的头文件 2 | 3 | #ifndef __ACCUMULATOR_H__ 4 | #define __ACCUMULATOR_H__ 5 | #include "date.h" 6 | 7 | class Accumulator{ //将某个数值按日累加 8 | private: 9 | Date lastDate; //上次变更数值的日期 10 | double value; //数值的当前值 11 | double sum; //数值按日累加之和 12 | public: 13 | //构造函数,date为开始累加的日期,value为初始值 14 | Accumulator(const Date &date,double value):lastDate(date),value(value),sum(0){} 15 | //获得日期date的累加结果 16 | double getSum(const Date &date) const {return sum+value*date.distance(lastDate);} 17 | //在date将数值变更为value 18 | void change(const Date &date,double value){ 19 | sum=getSum(date); 20 | lastDate=date; 21 | this->value=value; 22 | } 23 | //初始化,将日期变更为date,数值变为value,累加器清零 24 | void reset(const Date &date,double value){ 25 | lastDate=date; 26 | this->value=value; 27 | sum=0; 28 | } 29 | }; 30 | 31 | #endif //__ACCUMULATOR_H__ 32 | -------------------------------------------------------------------------------- /chapter_07/例7-10 个人银行账户管理程序/date.cpp: -------------------------------------------------------------------------------- 1 | //date.cpp 日期类实现文件 2 | 3 | #include "date.h" 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | namespace{ //namespace使下面的定义只在当前文件中有效 9 | //存储平年中的某个月1日之前有多少天,为便于getMaxDay函数的实现,该数组多出一项 10 | const int DAYS_BEFORE_MONTH[]={0,31,59,90,120,151,181,212,243,273,304,334,365}; 11 | } 12 | 13 | Date::Date(int year,int month,int day):year(year),month(month),day(day){ 14 | if(day<=0||day>getMaxDay()){ 15 | cout<<"Invalid date: "; 16 | show(); 17 | cout<2) totalDays++; 23 | } 24 | 25 | int Date::getMaxDay() const{ 26 | if(isLeapYear()&&month==2) 27 | return 29; 28 | else 29 | return DAYS_BEFORE_MONTH[month]-DAYS_BEFORE_MONTH[month-1]; 30 | } 31 | 32 | void Date::show() const{ 33 | cout< 4 | #include 5 | #include "Rectangle.h" 6 | using namespace std; 7 | 8 | int main(){ 9 | Rectangle rect; //定义Rectangle类的对象 10 | rect.initRectangle(2,3,20,10); //设置矩形的数据 11 | rect.move(3,2); //移动矩形位置 12 | cout<<"The data of rect(x,y,w,h): "<x=x;this->y=y;} 9 | void move(float offX,float offY){x+=offX;y+=offY;} 10 | float getX() const {return x;} 11 | float getY() const {return y;} 12 | private: //私有数据成员 13 | float x,y; 14 | }; 15 | 16 | #endif //_POINT_H 17 | -------------------------------------------------------------------------------- /chapter_07/例7-2 Point类私有继承/Rectangle.h: -------------------------------------------------------------------------------- 1 | //Rectangle.h 2 | 3 | #ifndef _RECTANGLE_H 4 | #define _RECTANGLE_H 5 | #include "Point.h" 6 | 7 | class Rectangle:private Point{ //派生类定义部分 8 | public: //新增公有函数成员 9 | void initRectangle(float x,float y,float w,float h){ 10 | initPoint(x,y); //调用基类公有函数成员 11 | this->w=w; 12 | this->h=h; 13 | } 14 | void move(float offX,float offY){Point::move(offX,offY);} 15 | float getX() const {return Point::getX();} 16 | float getY() const {return Point::getY();} 17 | float getH() const {return h;} 18 | float getW() const {return w;} 19 | private: //新增私有数据成员 20 | float w,h; 21 | }; 22 | 23 | #endif //_RECTANGLE_H 24 | -------------------------------------------------------------------------------- /chapter_07/例7-2 Point类私有继承/例7-2 Point类私有继承.dev: -------------------------------------------------------------------------------- 1 | [Project] 2 | FileName=例7-2 Point类私有继承.dev 3 | Name=项目1 4 | Type=1 5 | Ver=2 6 | ObjFiles= 7 | Includes= 8 | Libs= 9 | PrivateResource= 10 | ResourceIncludes= 11 | MakeIncludes= 12 | Compiler= 13 | CppCompiler= 14 | Linker= 15 | IsCpp=1 16 | Icon= 17 | ExeOutput= 18 | ObjectOutput= 19 | LogOutput= 20 | LogOutputEnabled=0 21 | OverrideOutput=0 22 | OverrideOutputName= 23 | HostApplication= 24 | UseCustomMakefile=0 25 | CustomMakefile= 26 | CommandLine= 27 | Folders= 28 | IncludeVersionInfo=0 29 | SupportXPThemes=0 30 | CompilerSet=4 31 | CompilerSettings=0000000100000000001000000 32 | UnitCount=3 33 | 34 | [VersionInfo] 35 | Major=1 36 | Minor=0 37 | Release=0 38 | Build=0 39 | LanguageID=1033 40 | CharsetID=1252 41 | CompanyName= 42 | FileVersion= 43 | FileDescription=Developed using the Dev-C++ IDE 44 | InternalName= 45 | LegalCopyright= 46 | LegalTrademarks= 47 | OriginalFilename= 48 | ProductName= 49 | ProductVersion= 50 | AutoIncBuildNr=0 51 | SyncProduct=1 52 | 53 | [Unit1] 54 | FileName=Point.h 55 | CompileCpp=1 56 | Folder= 57 | Compile=1 58 | Link=1 59 | Priority=1000 60 | OverrideBuildCmd=0 61 | BuildCmd= 62 | 63 | [Unit2] 64 | FileName=Rectangle.h 65 | CompileCpp=1 66 | Folder= 67 | Compile=1 68 | Link=1 69 | Priority=1000 70 | OverrideBuildCmd=0 71 | BuildCmd= 72 | 73 | [Unit3] 74 | FileName=7_2.cpp 75 | CompileCpp=1 76 | Folder= 77 | Compile=1 78 | Link=1 79 | Priority=1000 80 | OverrideBuildCmd=0 81 | BuildCmd= 82 | 83 | -------------------------------------------------------------------------------- /chapter_07/例7-3 类型兼容规则实例.cpp: -------------------------------------------------------------------------------- 1 | //7-3.cpp 类型兼容规则实例 2 | 3 | #include 4 | using namespace std; 5 | 6 | class Base1{ //基类Base1定义 7 | public: 8 | void display() const {cout<<"Base1::display()"<display(); //“对象指针->成员名” 23 | } 24 | 25 | int main(){ //主函数 26 | Base1 base1; //声明Base1类对象 27 | Base2 base2; //声明Base2类对象 28 | Derived derived; //声明Derived类对象 29 | 30 | fun(&base1); //用Base1类对象的指针调用fun函数 31 | fun(&base2); //用Base2类对象的指针调用fun函数 32 | fun(&derived); //用Derived类对象的指针调用fun函数 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /chapter_07/例7-4 派生类构造函数举例(多继承、含有内嵌对象).cpp: -------------------------------------------------------------------------------- 1 | //7_4.cpp 派生类构造函数举例(多继承、含有内嵌对象) 2 | 3 | #include 4 | using namespace std; 5 | 6 | class Base1{ //基类Base1,构造函数有参数 7 | public: 8 | Base1(int i){cout<<"Constructing Base1 "< 4 | using namespace std; 5 | 6 | class Base1{ //基类Base1,构造函数有参数 7 | public: 8 | Base1(int i){cout<<"Constructing Base1 "< 4 | using namespace std; 5 | 6 | class Base1{ //定义基类Base1 7 | public: 8 | int var; 9 | void fun(){cout<<"Member of Base1"<Base2::var=3; //作用域分辨符标识 35 | p->Base2::fun(); //访问Base2基类成员 36 | 37 | return 0; 38 | 39 | } 40 | -------------------------------------------------------------------------------- /chapter_07/例7-7 多继承同名隐藏举例(2).cpp: -------------------------------------------------------------------------------- 1 | //例7-7.cpp 多继承同名隐藏举例(2) 2 | 3 | #include 4 | using namespace std; 5 | 6 | class Base0{ //定义基类Base0 7 | public: 8 | int var0; 9 | void fun0(){cout<<"Member of Base0"< 4 | using namespace std; 5 | 6 | class Base0{ //定义基类Base0 7 | public: 8 | int var0; 9 | void fun0(){cout<<"Member of Base0"< 5 | using namespace std; 6 | 7 | class Base0{ //定义基类Base0 8 | public: //外部接口 9 | Base0(int var):var0(var){} 10 | int var0; 11 | void fun0(){cout<<"Member of Base0"< 5 | using namespace std; 6 | 7 | int main(){ //主函数 8 | double a[]={ //方程系数矩阵 9 | 0.2368,0.2471,0.2568,1.2671, //第一行 10 | 0.1968,0.2071,1.2168,0.2271, //第二行 11 | 0.1581,1.1675,0.1768,0.1871, //第三行 12 | 1.1161,0.1254,0.1397,0.1490}; //第四行 13 | double b[]={ 1.8471, 1.7471, 1.6471, 1.5471}; //方程右端项 14 | LinearEqu equ(4); //定义一个四元方程组对象 15 | equ.setLinearEqu(a,b); //设置方程组 16 | equ.printLinearEqu(); //输出方程组 17 | if(equ.solve()) //求解方程组 18 | equ.printSolution(); //输出方程组的解 19 | else 20 | cout<<"Fail"< 5 | #include 6 | using namespace std; 7 | 8 | LinearEqu::LinearEqu(int size/*=2*/):Matrix(size){ //用size调用基类构造函数 9 | sums=new double[size]; //动态内存分配 10 | solution=new double[size]; 11 | } 12 | 13 | LinearEqu::~LinearEqu(){ //派生类LinearEqu的析构函数 14 | delete[] sums; //释放内存 15 | delete[] solution; 16 | //会自动调用基类析构函数 17 | } 18 | 19 | void LinearEqu::setLinearEqu(const double *a,const double *b){ //设置线性方程组 20 | setMatrix(a); //调用基类函数 21 | for(int i=0;i 5 | using namespace std; 6 | 7 | void Matrix::setMatrix(const double *values){ //设置矩阵 8 | for(int i=0;i 5 | #include 6 | using namespace std; 7 | 8 | int main(){ //主函数 9 | MyFunction f; //定义MyFunction类的对象 10 | Trapz trapz(f); //定义Trapz类的对象 11 | //计算并输出积分结果 12 | cout<<"TRAPZ Int: "< 4 | using namespace std; 5 | 6 | template //定义函数模板 7 | void outputArray(const T *array,int count){ 8 | for(int i=0;i 4 | #include 5 | using namespace std; 6 | 7 | struct Student{ //结构体Student 8 | int id; //学号 9 | float gpa; //平均分 10 | }; 11 | 12 | template //类模板 13 | class Store{ 14 | private: 15 | T item; //item用于存放任意类型的数据 16 | bool haveValue; //haveValue标记item是否已被存入内容 17 | public: 18 | Store(); //默认形式(无形参)的构造函数 19 | T &getElem(); //提取数据函数 20 | void putElem(const T &x); //存入数据函数 21 | }; 22 | 23 | //以下实现各成员函数 24 | template //默认构造函数的实现 25 | Store::Store():haveValue(false){} 26 | 27 | template //提取数据函数的实现 28 | T &Store::getElem(){ 29 | if(!haveValue){ //如果试图提取未初始化的数据,则程序终止 30 | cout<<"No item present!"< //存入数据函数的实现 38 | void Store::putElem(const T &x){ 39 | haveValue=true; //将haveValue置为true,表示item中已存入数值 40 | item=x; //将x值存入item 41 | } 42 | 43 | int main(){ 44 | Stores1,s2; //定义两个Store类对象,其中数据成员item为int类型 45 | s1.putElem(3); //向对象s1中存入数据(初始化对象s1) 46 | s2.putElem(-7); //向对象s2中存入数据(初始化对象s2) 47 | cout<s3; //定义Store类对象s3,其中数据成员item为Student类型 51 | s3.putElem(g); //向对象s3中存入数据(初始化对象s3) 52 | cout<<"The student id is "<d; //定义Store类对象d,其中数据成员item为double类型 55 | cout<<"Retrieving object d..."; 56 | cout<