VC面向对象编程教程第2版清华大学王育坚著 第三章课后答案


3-44

一个名为CPerson 的类有如下属性:姓名、身份证号、性别和年龄,请用 C++语言定义
这个类,并为上述属性定义相应的方法。


#include
#include
class CPerson
{
private:
char Name[10];
char ID[20];
char Sex[4];
int Age;
public:
CPerson(char *na,char *id,char *se,int ag);
void Show();
};
CPerson::CPerson(char *na,char *id,char *se,int ag)
{
strcpy(Name,na);
strcpy(ID,id);
strcpy(Sex,se);
Age=ag;
}
void CPerson::Show()
{
cout<<"姓名:"<cout<<"身份证:"<cout<<"性别:"<cout<<"年龄:"<}
void main()
{
CPerson person("王三","610102************","男",21);
person.Show();
}




3-45 设计一个日期类 Date,该类用于表示日期值(年、月、日)。要求除了能够通过相应得成
员函数设置和获取日期外,还能够实现将日期加一天的操作。


#include
class date{
int year;
int month;
int day;
bool flag;
public:
date()
{
year=0;
month=0;
day=0;
}
date(int yr,int mo,int da);
void setdate();
int getyear();
int getmonth();
int getday();
void addday();
void show();
};
date::date(int yr,int mo,int da)
{
flag=false;
if(mo>=1&&mo<=12&&da>=1&&da<=31)
{
year=yr; month=mo; day=da;
}
else
{
flag=true;
}
}
void date::setdate()
{
cout<<"请输入年分"<cin>>year;
cout<<"请输入月份(1-12)"<cin>>month;
while(month<1||month>12)
{
cout<<"输入有误,请重新输入月份(1-12)"<cin>>month;
}
cout<<"请输入日期(1-31)"<cin>>day;
while(day<1||day>31)
{
cout<<"输入有误,请重新输入日期(1-31)"<cin>>day;
}
flag=false;
}
void date::show()
{
if(!flag)
cout<else
cout<<"日期设置有误,不能输出"<}

int date::getyear()
{
return year;
}
int date::getmonth()
{
return month;
}
int date::getday()
{
return day;
}
void date::addday()
{
day++;
if(month==2)//判断是否是二月
{
bool leapyear;
leapyear=((year%4==0&&year%100!=0)||(year%400==0));//定义闰年
if(leapyear)
{
if(day>29)//若是闰年的二月当 Day 大于 29 时,Day=1,Mon 增加一个月
{
day=1;
month++;
}
}
else
{
if(day>28)//若不是闰年的二月当 Day 大于 28 时,Day=1,Mon 增加一个月

{
day=1;
month++;
}
}
}
else if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
{
if(day>31)//若不是二月月大时,Day=1,Mon 增加一个月
{
day=1;
month++;
}
}
else

{
if(day>30)//若不是二月月小时,Day=1,Mon 增加一个月
{
day=1;
month++;
}
}
if(month>12)//若月大于 12 则 Mon=1,Year 增加一年
{
month=1;
year++;
}
}
void main()
{
date d1(1999,5,30);
d1.show();
d1.setdate();
d1.show();
cout<<"日期增加一天后为:";
d1.addday();
d1.show();
}


3-46

#include
using namespace std;
class CRectangle
{
private:
double X;
double Y;
double length;
double width;
public:
CRectangle(double s, double e, double l, double w)
{
X=s;
Y=e;
length=l;
width=w;
}
~CRectangle(){}
void Move(double , double);
void Size(double ,double);
void Where();
void Area();
};

void CRectangle::Move(double x, double y)
{
cout<<"矩形按向量("<cout<<"现在矩形左上角所在的位置:"<cout<<"("<}

void CRectangle::Size(double cl, double cw)
{
cout<<"要更改的长和宽:"<length=cl;
width=cw;
cout<}

void CRectangle::Where()
{
cout<<"现在矩形左上角所在的位置:"<cout<<"("<}

void CRectangle::Area()
{
double area;
area=length*width;
cout<}

int main()
{
CRectangle cr(2,3,5,4);
cr.Where();
cr.Area();
cr.Move(1,2);
cr.Size(2,3);
return 0;
}



3-47

#include
using namespace std;
class CRectangle
{
private:
double X;
double Y;
double length;
double width;
public:
CRectangle(double s, double e, double l, double w)
{
X=s;
Y=e;
length=l;
width=w;
}
~CRectangle(){}
void Move(double , double);
void Size(double ,double);
void Where();
void Area();
};

void CRectangle::Move(double x, double y)
{
cout<<"矩形按向量("<cout<<"现在矩形左上角所在的位置:"<cout<<"("<}

void CRectangle::Size(double cl, double cw)
{
cout<<"要更改的长和宽:"<length=cl;
width=cw;
cout<}

void CRectangle::Where()
{
cout<<"现在矩形左上角所在的位置:"<cout<<"("<}

void CRectangle::Area()
{
double area;
area=length*width;
cout<}

int main()
{
CRectangle cr(2,3,5,4);
cr.Where();
cr.Area();
cr.Move(1,2);
cr.Size(2,3);
return 0;
}



3-48

#include
#include
class Bank{
private:
char number[20];
int money;
public:
void setnumber(char*num,int mon)
{
strcpy(number,num);


money=mon;
}
void changer(int mon)
{
money+=mon;
}
void show()
{
cout<<"当前的总钱数为:"<}
};
void main()
{
char num[20];
int money;
int addmoney;
cout<<"输入用户名和存入的钱:"<cin>>num>>money;
Bank bank;
bank.setnumber(num,money);
bank.show();
cout<<"输入转账的钱:"<cin>>addmoney;
bank.changer(addmoney);
bank.show();

}



3-49


#include
using namespace std;

class Product
{
private:
char name[20];
double price;
int m_count;
public:
Product(){}
~Product(){}
void setproduct();
void sellproduct();
void show();
};

void Product::setproduct()
{
int n;
cout<<"输入生产产品数量:"<cin>>n;
m_count=n;
}
void Product::sellproduct()
{
int n;
cout<<"输入销售产品数量:"<cin>>n;
m_count-=n;
}
void Product::show()
{
cout<<"输出剩余产品数量:"<cout<}

int main()
{
Product p;
p.setproduct();
p.sellproduct();
p.show();

return 0;
}






3-50 建立一个名为 Student 的类,该类有以下几个私有成员变量:学生姓名、学号、性别和年龄。还有以下两个成员函数:一个用于初始化学生姓名、学号、性别和年龄的构造函数,一个用于输出学生信息的函数。编写一个主函数,声明一个学生对象,然后调用成员函数在屏幕上输出学生信息。

#include
#include
class Student
{
public:
Student(char *name,char *num,char *sex,int age);
~Student();
void show();
private:
char *Name;
char *Num;
char *Sex;
int Age;
};
Student::Student(char *name,char *num,char *sex,int age)
{
Name=new char[strlen(name)+1];//注意字符串的赋值

strcpy(Name,name);
Num=new char[strlen(num)+1];
strcpy(Num,num);
Sex=new char[strlen(sex)+1];
strcpy(Sex,sex);
Age=age;
}
Student::~Student()
{
delete Name;
delete Num;
delete Sex;
}
void Student::show()
{
cout<<"姓名:"<cout<<"学号:"<cout<<"性别:"<cout<<"年龄:"<}
void main()
{
Student student("张三","0401011201","男",18);
student.show();
}


3-51

设计一个CPetrol类,包含以下几个私有成员:90号 93号 98 号汽油加油量和单价,当天的总收入。类还包含以下几个成员函数:设置有关数据成员的构造函数,输入加油量并计算总收入的成员函数。利用类编写主函数:假设加油站某天90 93 98 号汽油单价分别为3.96 4.05 4.38 计算并输出加油站一天的收入。





#include
using namespace std;
class CPetrol
{
public:
CPetrol();
void setamount();
double total;

private:
double am_90;
double am_93;
double am_98;
double price_90;
double price_93;
d

ouble price_98;
};
CPetrol::CPetrol()
{
price_90=3.96;
price_93=4.05;
price_98=4.38;
}
void CPetrol::setamount()
{
cout<<"input three amounts!"<cin>>am_90>>am_93>>am_98;
total=am_90*price_90+am_93+price_93+am_98+price_98;
}
void main()
{
CPetrol c;
c.setamount();
cout<<"The total is"<
}






3-52

修改习题 3-50 中的类 Student,添加一个静态成员变量,用于表示已创建对象的数量;
添加两个静态成员函数,一个用于输出已创建对象的数量,一个用于输出一个学生的姓名和
学号。
#include
#include
class Student
{
public:
Student(char *name,char *num,char *sex,int age);
~Student();
staticvoid show(Student &a);
staticvoid showstudentnum();
private:
char *Name;
char *Num;
char *Sex;
int Age;
static int studentnum;
};
int Student::studentnum=0;
Student::Student(char *name,char *num,char *sex,int age)

{
studentnum++;
Name=new char[strlen(name)+1];
strcpy(Name,name);
Num=new char[strlen(num)+1];
strcpy(Num,num);
Sex=new char[strlen(sex)+1];
strcpy(Sex,sex);
Age=age;
}
Student::~Student()
{
delete Name;
delete Sex;
}
void Student::showstudentnum()
{
cout<<"学生的数量是:"<}
void Student::show(Student &a)
{
cout<<"姓名:"<cout<<"学号:"<cout<<"性别:"<cout<<"年龄:"<}
void main()
{
Student student1("张三","0401011201","男",18);
Student::show(student1);//注意调用方式,静态成员可以通过类名调用
Student::showstudentnum(); //注意调用方式
Student student2("李四","0401011202","男",18);
Student::show(student2);
Student::showstudentnum();
}




3-53

编写程序用静态成员的方法实现对班费的管理,要求定义一个类Student,除了声明一个存放班费的静态成员,还要分别定义一个上交班费的成员函数Contribute(),花费班费的成员函数Spend()和显示班费的静态成员函数Display()

//student.cpp
#include
using namespace std;
class Student
{
private:
static double fee; //fee --班费,静态成员数据
public:
Student(){} //默认构造函数,析构函数
~Student(){}
void Contribute(double n); // n --上缴的班费数额
void Spend(double n); // n --花费班费数量
static void Display(); //静态成员函数
};

double Student::fee=0; //类声明外面对静态数据成员初始化
//类方法
void Student::Contribute(double n)
{
fee=fee+n;
}
void Student::Spend(double n)
{
if(feecout<<"班费不够,请求失败!\n";
else
fee=fee-n;
}

void Student::Display()
{
cout<<"现有班费:"<}

int main()
{
Student stu;
stu.Display();
stu.Contribute(103.4);

//交钱
stu.Display();
stu.Spend(42.3); //花钱
stu.Display();
return 0;
}
放在了一个文件里了,上面是类声明,下面是测试小程序,运行过了,没问题




3-54


定义一个类A,该类除了有两个数据成员x,y外,还有一个对象备份函数copy。
copy函数的功能说明如下:对于类A的对象a1和a2,函数调用a1.copy(a2)表示将对象a2赋值给对象a1.(提示利用this指针防止一个对象对自己赋值)

#include
using namespace std;

class Test
{
private:
char *a,*b;
public:
Test()
{
a = new char[100];
b = new char[100];
}
~Test()
{
delete []a;
delete []b;
}
Test ?(Test &B)
{
if(this == &B) return *this;
int len = strlen(B.a);
delete []a;
a = new char[len+1];
strcpy(a,B.a);
len = strlen(B.b);
delete []b;
b = new char[len+1];
strcpy(b,B.b);
return *this;
}

void mytest(char *str1,char *str2)
{
strcpy(a,str1);
strcpy(b,str2);
}
void myprint()
{
cout << a << " " << b << endl;
}
}A,B;
int main()
{
B.mytest("this is ","B");
B.myprint();
A.copy(B);
A.myprint();
return 0;
}







3-55

将习题 3-45 中类 Date 的“日期加一天”成员函数改为友员函数。

#include
class date{
int year;
int month;
int day;
bool flag;
public:
date()
{

year=0;
month=0;
day=0;
}
date(int yr,int mo,int da);
void setdate();
int getyear();
int getmonth();
int getday();
friend void addday(date &d);
void show();
};
date::date(int yr,int mo,int da)
{
flag=false;
if(mo>=1&&mo<=12&&da>=1&&da<=31)
{
year=yr; month=mo; day=da;
}
else
{
flag=true;
}
}
void date::setdate()
{
cout<<"请输入年分"<cin>>year;
cout<<"请输入月份(1-12)"<cin>>month;
while(month<1||month>12)
{
cout<<"输入有误,请重新输入月份(1-12)"<cin>>month;
}
cout<<"请输入日期(1-31)"<cin>>day;
while(day<1||day>31)
{
cout<<"输入有误,请重新输入日期(1-31)"<cin>>day;
}
flag=false;
}
void date::show()
{

if(!flag)
cout<else
cout<<"日期设置有误,不能输出"<}
int date::getyear()
{
return year;
}
int date::getmonth()
{
return month;
}
int date::getday()
{
return day;
}
void addday(date &d)
{
d.day++;
if(d.month==2)//判断是否是二月
{
bool leapyear;
leapyear=((d.year%4==0&&d.year%100!=0)||(d.year%400==0));//定义闰年
if(leapyear)
{
if(d.day>29)//若是闰年的二月当 Day 大于 29 时,Day=1,Mon 增加一个月

{
d.day=1;
d.month++;
}
}
else
{
if(d.day>28)//若不是闰年的二月当 Day 大于 28 时,Day=1,Mon 增加一个月
{
d.day=1;
d.month++;
}
}
}
else
if(d.month==1||d.month==3||d.month==5||d.month==7||d.month==8||d.month==10||d.month==12)
{

if(d.day>31)//若不是二月月大时,Day=1,Mon 增加一个月
{
d.day=1;
d.month++;
}
}
else
{
if(d.day>30)//若不是二月月小时,Day=1,Mon 增加一个月
{
d.day=1;
d.month++;
}
}
if(d.month>12)//若月大于 12 则 Mon=1,Year 增加一年
{
d.month=1;
d.year++;
}
}
void main()
{
date d1(1999,5,30);
d1.show();
d1.setdate();
d1.show();
cout<<"日期增加一天后为:";
addday(d1);
d1.show();
}




3-56 将习题 3-50 中类 Student 的学生信息输出函数改为友员函数。


#include
#include
class Student
{
public:
Student(char *name,char *num,char *sex,int age);
~Student();
friend void show(Student &);
private:
char *Name;
char *Num;
char *Sex;
int Age;
};
Student::Student(char *name,char *num,char *sex,int age)

{
Name=new char[strlen(name)+1];
strcpy(Name,name);
Num=new char[strlen(num)+1];
strcpy(Num,num);
Sex=new char[strlen(sex)+1];
strcpy(Sex,sex);
Age=age;
//注意字符串的赋值
}
Student::~Student()
{
delete Name;
delete Sex;
}
void show(Student &stu)
{
cout<<"姓名:"<cout<<"学号:"<cout<<"性别:"<cout<<"年龄:"<}
void main()
{
Student student("张三","0401011201","男",18);
show(student);
}





3-57


设计一个直线类Line(设直线方程为ax+bx+c=0),其中,包含三个数据成员a、b、c,一个显示数据成员的disp成员函数和一个求两直线交点的友元函数setpoint,并在main()中给出对象或数据进行测试。请填空完成程序并上机运行验证。

#include
class Line
{
friend void setpoint(Line &A,Line &B);
private:
double a;
double b;
double c;
public:
Line (double a1, double b1, double c1)
{
a=a1;
b=b1;
c=c1;
}
~Line(){};

};

void setpoint(Line &A,Line &B)
{
double x;
if(A.a/B.a!=A.b/B.b)
{
x=-100;
while(1)
{
if(( (-A.c-A.a*x)/A.b - (-B.c-B.a*x)/B.b ) < 0.00001)
break;

x=x+0.00001;
}

cout<}
else
cout<<"error"<}

void main ()
{
Line A(-2,-1,4),B(1,-10,-3);
setpoint(A,B);
}




3-58


#include
#include
class Student
{
private:
char name[10];
char id[20];
char sex[20];
int age;
public:
Student(cha

r pname[10],char pid[20],char psex[5],int page);
~Student(){}

void show()const;

};

Student::Student(char pname[10],char pid[20],char psex[5],int page)
{
strcpy(name,pname);
strcpy(id,pid);
strcpy(sex,psex);
age=page;
}
void Student::show() const
{
cout<<"姓名:"<
}
int main()
{
const Student student1("jane","200931106013","famle",20);
student1.show(); //通过常对象只能调用常函数

Student student2("jane2","200931106013","famle",20);
student2.show(); //通过普通对象也能调用常成员函数

return 0;
}


3-59


#include
class CPoint
{
private:
float x;
float y;
public:
CPoint(float xx=0,float yy=0);
~CPoint();
float Getx();
float Gety();
};
CPoint ::CPoint(float xx,float yy)
{
x=xx;
y=yy;
}
CPoint ::~CPoint()
{
}
float CPoint::Getx()
{
return x;
}
float CPoint::Gety()
{
return y;
}


class CLine:public CPoint
{
private:
float x1;
float y1;
public:
CLine(float a,float b,float c,float d);
~CLine();
float Getxx();
float Getyy();

};
CLine::CLine(float a,float b,float c,float d):CPoint(a,b)
{
x1=c;
y1=d;

}
CLine::~CLine()
{
}
float CLine::Getxx()
{
return x1;
}
float CLine::Getyy()
{
return y1;
}


int main()
{
CLine p(1,2,3,4);
cout<<"输出CPoint的坐标:";
cout<<"("<cout<<"输出CLine的坐标:";
cout<<"("<return 0;
}




3-60

定义一个哺乳动物Mammal类,再由此派生出狗Dog类,定义一个Dog类的对象,编写主函数测试它们并观察基类与派生类的构造函数与析构函数的调用顺序。
提示:Mammal类可以定义属性 Age, Weight等。
Dog类可以再增加自己的属性如color。
注意数据成员的访问属性



#include


class mammal
{
public:

mammal()
{
cout<<"it is a mammal"<}
void age()
{
cout<<"mammal age"<}

void weight()
{
cout<<"mammal weight"<}

~mammal()
{
cout<<"destory mammal"<}
};

class dog:public mammal
{
public:dog()
{
cout<<"it is a dog"<}

void weight()
{
cout<<"dog`s weight"<}

~dog()
{
cout<<"destory dog class"<}
};

void main()
{
dog dg;

}


3-61


#include
#include
class Vehicle{
public:
char xh[20],prod[20],color[20],chspeed[20];
int cweight,speed,oil,peonum,pweight;
public:
Vehicle(char *,char *,char *,char *,int ,int ,int ,int ,int );
~Vehicle(){}
};
Vehicle::Vehicle(char *x,char *p,char *co,char *ch,int cw,int s,int o,int pe,int pw)
{

strcpy(xh,x);
strcpy(prod,p);
strcpy(color,co);
strcpy(chspeed,ch);
cweight=cw;
speed=s;
oil=o;
peonum=pe;
pweight=pw;

}
class Car : public Veh

icle
{
public:
Car(char *x,char *p,char *co,char *ch,int cw,int s,int o,int pe,int pw):Vehicle(x,p,co,ch,cw,s,o,pe,pw){}
void showcar();
};
void Car :: showcar()
{
cout<<"小汽车型号是:"<}
class Truck : public Vehicle
{
public :
Truck(char *x,char *p,char *co,char *ch,int cw,int s,int o,int pe,int pw):Vehicle(x,p,co,ch,cw,s,o,pe,pw){}
void showtruck();
};
void Truck :: showtruck()
{
cout<<"卡车型号是:"<}
void main()
{
char x[20],p[20],co[20],ch[20];
int cw,s,o,pe,pw;
cout<<"请输入小汽车的信息:"<cin>>x>>p>>co>>ch>>cw>>s>>o>>pe>>pw;
Car c(x,p,co,ch,cw,s,o,pe,pw);
c.showcar();
char x1[20],p1[20],co1[20],ch1[20];
int cw1,s1,o1,pe1,pw1;
cout<<"请输入卡车的信息:"<cin>>x1>>p1>>co1>>ch1>>cw1>>s1>>o1>>pe1>>pw1;
Truck B(x1,p1,co1,ch1,cw1,s1,o1,pe1,pw1);
B.showtruck();

}







3-62

#include

using namespace std;

class Book
{
protected:
char book_name[20];
char book_number[20];
char book_author[20];
public:
void InputBookInfo() //输入书的信息
{
cout<<"Please Input The Book's Name:";
cin>>book_name;
cout<<"Please Input The Book's Number:";
cin>>book_number;
cout<<"Please Input The Book's Author:";
cin>>book_author;
}
void DisplayBookInfo() //输出书的信息
{
cout<<"The Book's Name:";
cout<cout<<"The Book's Number:";
cout<cout<<"The Book's Author:";
cout<}
};
class Reader:public Book
{
private:
char name[20];
char ID[20];
int cnt;
public:
Reader();
~Reader();
void InputReaderInfo() //输入读者信息
{
cout<<"Input The number of book(s) reader have borrowed:";
cin>>cnt;
if(cnt>6)
{
cout<<"You can only borrow 6 books!\n";
cnt=6;
}
cout<<"Please Input The Reader's Name:";
cin>>name;
cout<<"Please Input The Reader's ID:";
cin>>ID;
cout<Book *p=new Book [cnt];
Book *q;
int i=0;
for(q=p;q{
i++;
cout<<"**********Please input the "<q->InputBookInfo();
cout<<"-------------------------------------------------\n\n";
}
cout<cout<<"The reader has borrowed "<cout<<"**********Show BookInfo**********************\n"<i=0;
for(q=p;q{

i++;
cout<<"**********The "<q->DisplayBookInfo();
cout<<"-------------------------------------------------\n\n";
}
delete [] p;
p=NULL;
}

};

Reader::Reader()
{

}

Reader::~Reader()
{

}

int main()
{
Reader *r=new Reader;
r->InputReaderInfo();
delete r;
r=NULL;
return 0;
}



3-63


利用习题 3-47 中类 CPerson 派生出类 CEmployee(雇员),派生类 CEmployee 增加了
两个新的数据成员,分别用于表示部门和薪水。要求派生类 CEmployee 的构造函数显示调用
基类 CPerson 的构造函数,可根据需要为派生类增加新的成员函数。


#include
#include
class CPerson
{
private:
char Name[10];
char ID[20];
char Sex[4];
int Age;
public:
CPerson(char *na,char *id,char *se,int ag);
void Show();
};
CPerson::CPerson(char *na,char *id,char *se,int ag)
{

strcpy(Name,na);
strcpy(ID,id);
strcpy(Sex,se);
Age=ag;
}
void CPerson::Show()
{
cout<<"姓名:"<cout<<"身份证:"<cout<<"性别:"<cout<<"年龄:"<}
class CEmployee :public CPerson
{
public:
CEmployee(char*na,char*id,char*se,intag,char*dep,double
wag):CPerson(na,id,se,ag)
{
strcpy(Department,dep);
Wages=wag;
}
void Show();
private:
char Department[50];
double Wages;
};
void CEmployee ::Show()
{
CPerson::Show();
cout<<"部门:"<}
void main()
{
CPerson person("王三","610102************","男",21);
person.Show();
CEmployee Employee ("李四","610102************","男",24,"开发部",5000);
Employee .Show();
}





3-64 修改例 3-11 中类 Point 和类 Circle 的定义,删除设置坐标和设置半径的两个成员函数,
改为利用构造函数设置坐标或半径。

#include
class Point
{
public:
Point(int a,int b);
int getX(){return x;};

int getY(){return y;};
protected:
int x,y;
};
Point:: Point(int a,int b)
{
x=a;
y=b;
}
class Circle :public Point
{
public:
Circle(int a,int b,int r): Point(a,b)
{
radius=r;
}
int getUpperLeftX()
{ return getX()-radius; }
int getUpperLeftY()
{ return getY()+radius; }
int getRudius()
{ return radius; }
private:
int radius;
};
void main()
{
Circle c(200,250,100);
cout<<"X="<cin>>year;
cout<<"请输入月份(1-12)"<cin>>month;
while(month<1||month>12)
{
cout<<"输入有误,请重新输入月份(1-12)"<cin>>month;
}
cout<<"请输入日期(1-31)"<cin>>day;
while(day<1||day>31)
{
cout<<"输入有误,请重新输入日期(1-31)"<cin>>day;
}
flag=false;
}
void date::show()
{
if(!flag)
cout<else
cout<<"日期设置有误,不能输出"<}

int date::getyear()
{
return year;
}
int date::getmonth()
{
return month;
}
int date::getday()
{
return day;
}
class Time
{
public:
Time(int h,int m,int s);
void setTime();
void showtime();
protected:
int Hour;
int Minute;
int Second;
};
Time::Time(int h,int m,int s)
{
Hour=h;
Minute=m;
Second=s;
}
void Time::setTime()
{
cout<<"输入时间"<cin>>Hour;
(Hour>=0&&Hour<24)?Hour:0;
cout<<"输入分钟"<cin>>Minute;
(Minute>=0&&Minute<60)?Minute:0;
cout<<"输入秒"<cin>>Second;
(Second>=0&&Second<60)?Second:0;
}
void Time::showtime()
{
cout<}

class DateTime:public Time,public date
{
public:
DateTime(int Y,int M,int D,int H,int Mi,int S):date(Y,M,D),Time(H,Mi,S){}
void setDateTime()
{
date::setdate();
Time::setTime();
}
void show()
{
date::show();
cout<<" ";
Time::showtime();
}
};
void main()
{
DateTime dt(1999,5,31,12,4,15);
dt.show();
dt.setDateTime();
dt.show();
}



3-66

#include
#include
class Father
{

public:
char Fname[20];
char X[20];
char Fnation[20];
int Fage;
public:
void FSetInfo(char *x,char *na,char *mi,int a);

};
void Father::FSetInfo(char *x,char *na,char *mi,int a)
{
strcpy(X,x);
strcpy(Fname,na);
strcpy(Fnation,mi);
Fage=a;
}

class Mother
{

public:
char Mname[20];
char Mnation[20];
int Mage;
public:
void MSetInfo(char *nb,char *ni,int b);

};
void Mother::MSetInfo(char *nb,char *ni,int b)
{
strcpy(Mname,nb);
strcpy(Mnation,ni);
Mage=b;

}

class Child:public Father,public Mother
{


public:
char firstname[20];
char mainname[20];
int age;
char nation[20];

public:
void CSetInfo(char *str,int a);
void CShow();
void getinfo()
{
strcpy(firstname,X);
strcpy(nation,Mnation);
}

};

void Child::CSetInfo(char *str,int a)
{
strcpy(mainname,str);
age=a;
}

void Child::CShow()
{
cout<<"The information of father is: "<<" name: "<cout<<"The information of mother is: "<<" name: "<cout<<"The information of kid:"<<"name: "<}

int main()
{
Child kid;
char x[20],name1[20],nation1[20],name2[20],nation2[20],mainname[20];
int age1,age2,age3;


cout<<"please input the information of father:"<cout<<"father's family name: ";cin>>x;
cout<<"father's mainname:";cin>>name1;
cout<<"father's nation:";cin>>nation1;
cout<<"father's age:";cin>>age1;


cout<<"please input the information of mother:"<cout<<"mother's name: ";cin>>name2;
cout<<"mother's nation:";cin>>nation2;
cout<<"mother's age:";cin>>age2;

cout<<"please input the information of kid:"<cout<<"child's mainname:";cin>>mainname;
cout<<"child's age";cin>>age3;

kid.FSetInfo(x,name1,nation1,age1);
kid.MSetInfo(name2,nation2,age2);
kid.CSetInfo(mainname,age3);
kid.getinfo();
kid.CShow();


return 0;
}







3-67假设将例 3-14 中类 BaseA 和 BaseB 的成员函数 setA( )和 setB( )的函数名统一为 set( ),
请重写 main( )函数。


#include
class BaseA
{
protected:
int a;
public:
void set(int);
};
class BaseB
{
protected:
int b;
public:
void set(int);
};
class MultiDerived:public BaseA,public BaseB
{
public:
int getAB();
};

void BaseA::set(int x)
{
a=x;
}
void BaseB::set(int x)
{
b=x;
}
int MultiDerived::getAB()
{
return a+b;
}
int main()
{
MultiDerived md;
md.BaseA::set(30);
md.BaseB::set(70);
cout<<"a+b="<return 0;
}




3-68 设类 X 分别派生出类 Y 和类 Z,类 Y 和类 Z 又共同派生出类 W,请用虚基类方式定义
这些类。要求为类简单添加一些成员,并编写 main( )函数进行验证。


#include
class X
{
public:
void show()
{
cout<<"Class X"<}
};
class Y:virtual public X
{
};
class Z:virtual public X
{
};
class W:public Y,public Z
{
};
int main()
{
W w;
w.show();

}



3-69 为了解决例 3-17 中由于多重继承而产生的二义性问题,可以利用一个指向基类 B(或 C)
的指针指向派生类 D 的对象。请按此方法编程。


#include
class A
{
public:
int a;
};
class B:public A
{
public:
int b;
};
class C:public A
{
public:
int c;
};
clas

s D:public B,public C
{
public:
int d;
};
int main()
{
D d1;
B *q;
q=&d1;
q->a=100; //指向从 B 继承来的 a
return 0;
}



3-70 编写一个工资管理程序,将雇员类作为所有类的基类,其派生类包括经理类,销售员类、
计件工类和小时工类。经理享有固定的周薪,销售员的收入是一小部分的基本工资加上销售
额的提成;计件工的收入完全取决于其生产的工件数量;小时工的收入以小时计算,再加上
加班费。


#include
#include
class Employee
{
public:
Employee (char *na);
virtual void compute(){}
virtual void display(){}
protected:

char *Name;
double Wages;
};
Employee ::Employee (char *na)
{
Name=new char[strlen(na)+1];
strcpy(Name,na);
}
class manager:public Employee
{
public:
manager(char *name,double pay):Employee (name)
{
Pay=pay;
}
void compute();
void display();
private:
double Pay; //周薪数
};
void manager::compute()
{
Wages=Pay;
}
void manager::display()
{
cout<<"经理姓名:"<cout<<"周薪:"<}
class salesman:public Employee
{
public:
salesman(char *name,double base,double extra):Employee (name)
{
basepay=base;
extrapay=extra;
}
void compute();
void display();
private:
double basepay; //基本工资
double extrapay; //提成
};
void salesman::compute()
{

Wages=basepay+extrapay;
}
void salesman::display()
{
cout<<"销售员姓名:"<cout<<"基本工资:"<cout<<"加班费:"<cout<<"薪水:"<}
class pieceworker:public Employee
{
public:
pieceworker(char *name,double pi,int num ):Employee (name)
{
piece=pi;
number=num;
}
void compute();
void display();
private:
double piece; //每工件所付钱数
int number; //工件数量
};
void pieceworker::compute()
{
Wages=piece*number;
}
void pieceworker::display()
{
cout<<"记件工姓名:"<cout<<"每件应付钱数:"<cout<<"件数:"<cout<<"薪水:"<}
class hourworker:public Employee
{
public:
hourworker(char *name,double hou, double pa,double cbp):Employee (name)
{
hours=hou;
pay=pa;
call_back_pay=cbp;
}
void compute();
void display();
private:

double hours; //工作小时数
double pay;//每小时应付工资
double call_back_pay; //加班费
};
void hourworker::compute()
{
Wages=hours*pay+call_back_pay;
}
void hourworker::display()
{
cout<<"小时工姓名:"<cout<<"小时工资:"<cout<<"时间:"<cout<<"加班费:"<

ack_pay<cout<<"薪水:"<}
void main()
{
Employee *p;
manager m("张三",2000);
p=&m;
p->compute();
p->display();
cout<salesman s("李四",2000,500);
p=&s;
p->compute();
p->display();
cout<pieceworker u("王五",15,100);
p=&u;
p->compute();
p->display();
cout<hourworker c("赵二",80,20,100);
p=&c;
p->compute();
p->display();
}



3-71







3-71

用函数重载形式编写函数 square( ):求一个 int 型或 double 型参数的平方。
#include
int square(int n)
{
return n*n;
}
float square(float f)
{
return f*f;

}
void main()
{
int n;
cout<<"请输入一个整数:";
cin>>n;
cout<<"它的平方是:"<float f;
cout<<"请输入一个小数:";
cin>>f;
cout<<"它的平方是:"<}



3-72


用函数重载方法求两个整数、两个浮点数、两个字符中的最小者。
#include
int min(int a,int b)
{
return a}
float min(float a,float b)
{
return a}
char min(char a,char b)
{
return a}
void main()
{
int n1,n2;
cout<<"请输入两个整数:";
cin>>n1>>n2;
cout<<"两个数中最小者是:"<float f1,f2;
cout<<"请输入两个小数:";
cin>>f1>>f2;
cout<<"两个数中最小者是:"<char c1,c2;
cout<<"请输入两个字符:";
cin>>c1>>c2;
cout<<"两个数中最小者是:"<}




3-73

修改习题 3-50 中的类 Student,增加以下私有成员变量:高等数学、英语、操作系统、
数据结构等四门课的分数和总成绩。修改或增加以下成员函数:初始化学生姓名、学号、性
别、年龄和 4 门分数的构造函数。输入 4 门课分数的函数,计算学生总成绩的函数,输出学
生信息的函数。编写一个主函数,调用原来的构造函数声明一个学生对象并输入其 4 门课的
分数,再调用新增加的构造函数声明另一个学生对象。最后分别计算两个学生的总分,并在
屏幕输出两个学生的所有信息。

#include
#include
class Student
{
public:
Student(char *name,char *num,char *sex,int age);
Student(char *name,char *num,char *sex,int age,float math, float english, float os,float ds);
~Student();
void getscore(); //输入四门课成绩
void CalScore(); //计算总成绩
void show();
private:
char *Name;
char *Num;
char *Sex;
int Age;
float Mathematics, English, OperatingSystem, DataStructure,TotalScore;
};
Student::Student(char *name,char *num,char *sex,int age)
{
Name=new char[strlen(name)+1

];//注意字符串的赋值
strcpy(Name,name);
Num=new char[strlen(num)+1];
strcpy(Num,num);
Sex=new char[strlen(sex)+1];
strcpy(Sex,sex);
Age=age;
}
Student::Student(char *name,char *num,char *sex,int age,float math, float english, float os,float ds)
{
Name=new char[strlen(name)+1];
strcpy(Name,name);
Num=new char[strlen(num)+1];
strcpy(Num,num);
Sex=new char[strlen(sex)+1];
strcpy(Sex,sex);
Age=age;
Mathematics=math;
English=english;
OperatingSystem=os;
DataStructure=ds;
}
Student::~Student()

{
delete Name;
delete Num;
delete Sex;
}
void Student::getscore()
{
cout<<"请输入高数分数:";
cin>>Mathematics;
cout<<"请输入英语分数:";
cin>> English;
cout<<"请输入操作系统分数:";
cin>>OperatingSystem;
cout<<"请输入数据结构分数:";
cin>>DataStructure;
}
void Student::CalScore()
{
TotalScore=Mathematics+English+OperatingSystem+DataStructure;
}
void Student::show()
{
cout<<"姓名:"<cout<<"学号:"<cout<<"性别:"<cout<<"年龄:"<cout<<"高数分数:"<cout<<"英语分数:"<cout<<"操作系统分数:"<cout<<" 数据结构分数:"<cout<<"总成绩为:"<}
void main()
{
Student student1("张三","0401011201","男",18);
cout<<"请输入第一个学生的分数"<student1.getscore();//输入四门课的成绩
Student student2("李四","0401011202","男",18,80,85,89,76);
student1.CalScore();
student2.CalScore();
student1.show();
cout<student2.show();
}




3-74

采用类继承的方法(不直接修改 Student 类)完成习题 3-73 所要求的功能。

#include
#include
class Student
{
public:
Student(char *name,char *num,char *sex,int age);
Student(char *name,char *num,char *sex,int age,float math, float english, float os,float ds);
~Student();
void show();
private:
char *Name;
char *Num;
char *Sex;
int Age;
};
Student::Student(char *name,char *num,char *sex,int age)
{
Name=new char[strlen(name)+1];//注意字符串的赋值
strcpy(Name,name);
Num=new char[strlen(num)+1];
strcpy(Num,num);
Sex=new char[strlen(sex)+1];
strcpy(Sex,sex);
Age=age;
}
Student::~Student()
{
delete Name;
delete Num;
delete Sex;
}
void Student::show()
{
cout<<"姓名:"<cout<<"学号:"<cout<<"性别:"<cout<<"年龄:"<}
class StudentScore:public

Student{
private:
float Mathematics, English, OperatingSystem, DataStructure,TotalScore;
public:
StudentScore(char *name,char *num,char *sex,int age,float math, float english, float os,float
ds);
void getscore(); //输入四门课成绩
void CalScore(); //计算总成绩

void show();
};
StudentScore::StudentScore(char *name,char *num,char *sex,int age,float math, float english, float
os,float ds)
:Student(name,num,sex,age)
{
Mathematics=math;
English=english;
OperatingSystem=os;
DataStructure=ds;
}
void StudentScore::getscore()
{
cout<<"请输入高数分数:";
cin>>Mathematics;
cout<<"请输入英语分数:";
cin>> English;
cout<<"请输入操作系统分数:";
cin>>OperatingSystem;
cout<<"请输入数据结构分数:";
cin>>DataStructure;
}
void StudentScore::CalScore()
{
TotalScore=Mathematics+English+OperatingSystem+DataStructure;
}
void StudentScore::show()
{
Student::show();
cout<<"高数分数:"<cout<<"英语分数:"<cout<<"操作系统分数:"<cout<<"数据结构分数:"<cout<<"总成绩为:"<}
void main()
{
StudentScore student("李四","0401011202","男",18,80,85,89,76);
student.CalScore();
student.show();
}



3-75

建立一个名为 Teacher 的类,该类有以下成员变量:教师姓名、ID 号、基本工资、奖金、
所得税和实际发放数。还有以下成员函数:两个构造函数,一个用于初始化教师姓名和 ID 号,
一个用于初始化教师姓名、ID 号、基本工资、奖金和所得税;一个输入基本工资、奖金和所
得税的函数;一个计算实际发放数的函数;一个输出教师信息的函数。对协助函数,调用的
一个构造函数声明一个教师对象并输入及工资,再调用第二个构造函数声明一个教师对象。
最后分别计算两个教师的实际发放数,并在屏幕输出。

#include
class Teacher
{
public:
Teacher(char *name,char *id);
Teacher(char *name,char *id,double basepay,double prize,double tax);
~Teacher();
void input();//输入基本工资、奖金和所得税
void compute(); //计算实际发放数
void display(); //显示信息
private:
char *Name;//姓名
char *ID;//ID 号
double Basepay; //基本工资
double Prize;//奖金
double Tax;//个人所得税
double wages;//实际发放数
};
Teacher::Teacher(char *name,char *id)
{
Name=new char[strlen(name)+1];
strcpy(Name,name);
ID=new char[strlen(id)+1];
strcpy(ID,id);
}
Teacher::Teacher(char *name,char *id,double basepay,double prize,double tax)
{
Name=new char[strlen(name)+1];
strcpy(Name,name);

ID=new char[strlen(id)+1];
strcpy(ID,id);
Basepay=basepay;
Prize=prize;
Tax=tax;
}
Teacher::~Teacher()
{
delete Name;
delete ID;
}
void Teacher::input()
{

cout<<"请输入基本工资:";
cin>>Basepay;
cout<<"请输入奖金:";
cin>>Prize;
cout<<"请输入个人所得税:";
cin>>Tax;
}
void Teacher::compute()
{
wages=Basepay+Prize-Tax;
}
void Teacher::display()
{
cout<<"姓名:"<cout<<"ID 号:"<cout<<"基本工资:"<cout<<"奖金:"<cout<<"个人所得税:"<cout<<"实际发放的工资:"<}
void main()
{
Teacher teacher1("张三","001");
cout<<"请输入张三的信息:"<teacher1.input();
https://www.360docs.net/doc/4f6761566.html,pute();
teacher1.display();
Teacher teacher2("李四","002",3000,1000,120);
https://www.360docs.net/doc/4f6761566.html,pute();
teacher2.display();
}



3-76 为习题 3-75 中的类 Teacher 添加两个同名的成员函数 Add( ),一个函数用于将其他教师
的工资加到该教师(如该教师替其他教师代课),一个函数用于将一个数值加到该教师(如提
高该教师的工资)。在主函数中编写代码验证所完成的功能。

#include
#include
class Teacher
{
public:
Teacher(char *name,char *id);
Teacher(char *name,char *id,double basepay,double prize,double tax);
~Teacher();
void input();//输入基本工资、奖金和所得税
void compute();//计算实际发放数

void display();//显示信息
void Add(Teacher t);//将其他教师的工资加到该教师
void Add(double Add); //用于将一个数值加到该教师
private:
char *Name;//姓名
char *ID;//ID 号
double Basepay; //基本工资
double Prize;//奖金
double Tax;//个人所得税
double wages;//实际发放数
};
Teacher::Teacher(char *name,char *id)
{
Name=new char[strlen(name)+1];
strcpy(Name,name);
ID=new char[strlen(id)+1];
strcpy(ID,id);
}
Teacher::Teacher(char *name,char *id,double basepay,double prize,double tax)
{
Name=new char[strlen(name)+1];
strcpy(Name,name);
ID=new char[strlen(id)+1];
strcpy(ID,id);
Basepay=basepay;
Prize=prize;
Tax=tax;
}
Teacher::~Teacher()
{
delete Name;
delete ID;
}
void Teacher::input()
{
cout<<"请输入基本工资:";
cin>>Basepay;
cout<<"请输入奖金:";
cin>>Prize;
cout<<"请输入个人所得税:";
cin>>Tax;
}
void Teacher::compute()
{
wages=Basepay+Prize-Tax;
}

void Teacher::display()
{
cout<<"姓名:"<cout<<"ID 号:"<cout<<"基本工资:"<cout<<"奖金:"<cout<<"个人所得税:"<cout<<"实际发放的工资:"<

es<}
void Teacher::Add(double Add)
{
wages=wages+Add;
}
void Teacher::Add(Teacher t)
{
wages+=t.wages;
}
void main()
{
Teacher teacher1("张三","001");
cout<<"请输入张三的信息:"<teacher1.input();
https://www.360docs.net/doc/4f6761566.html,pute();
teacher1.display();
cout<Teacher teacher2("李四","002",3000,1000,120);
https://www.360docs.net/doc/4f6761566.html,pute();
teacher2.display();
cout<teacher1.Add(teacher2);
teacher1.display();
cout<teacher2.Add(1000);
teacher2.display();
}



3-77
定义矢量类型,给出平面上两个矢量的加法和减法运算。要求利用非成员函数重载运算
符“+”,利用成员函数重载运算符“-”。

#include
class vector
{
public:
vector(double i=0.0,double j=0.0);
friend vector operator+(vector v1,vector v2); //友元函数重载运算符"+"
vector operator-(vector v2);//成员函数重载运算符"-"
void display();
private:

double x,y;
};
vector::vector(double i,double j)
{
x=i;
y=j;
}
vector operator+(vector v1,vector v2)
{
vector temp;
temp.x=v1.x+v2.x;
temp.y=v1.y+v2.y;
return temp;
}
vector vector::operator-(vector v2)
{
vector temp;
temp.x=x-v2.x;
temp.y=y-v2.y;
return temp;
}
void vector::display()
{
cout<}
void main()
{
vector v1(3,6),v2(2,2),v3;
cout<<"v1=";
v1.display();
cout<<"v2=";
v2.display();
v3=v1-v2;
cout<<"v3=v1-v2=";
v3.display();
v3=v1+v2;
cout<<"v3=v1+v2=";
v3.display();
}



3-78


#include
using namespace std;
class Complex
{
friend Complex operator+(Complex c1,Complex c2);
private:
float r;
float i;
public:
Complex(float x=0, float y=0)
{
r=x;
i=y;
}
void show(Complex);
};

Complex operator+(Complex c1,Complex c2)
{
Complex temp;
temp.r=c1.r+c2.r;
temp.i=c1.i+c2.i;
return temp;
}

void Complex::show(Complex temp)
{
cout<}

int main()
{
Complex complex1(3.34f,4.8f),complex2(12.8f,5.2f);
Complex complex;
complex=complex1+complex2;
cout<<"*****输出信息*****"<complex.show(complex);
return 0;
}

3-79

采用重载运算符“++”的方法实现习题 3-40 要求的“日期加一天”操作。

#include
class date{
int year;
int month;
int day;
bool flag;

public:
date()
{
year=0;
month=0;
day=0;
}
date(int yr,int mo,int da);
void setdate();
int getyear();
int getmonth();
int getday();
void operator++(int);//重载运算符++实现日期加一天的操作
void show();
};
date::date(int yr,int mo,int da)
{
flag=fals

e;
if(mo>=1&&mo<=12&&da>=1&&da<=31)
{
year=yr; month=mo; day=da;
}
else
{
flag=true;
}
}
void date::setdate()
{
cout<<"请输入年分"<cin>>year;
cout<<"请输入月份(1-12)"<cin>>month;
while(month<1||month>12)
{
cout<<"输入有误,请重新输入月份(1-12)"<cin>>month;
}
cout<<"请输入日期(1-31)"<cin>>day;
while(day<1||day>31)
{
cout<<"输入有误,请重新输入日期(1-31)"<cin>>day;
}
flag=false;
}
void date::show()
{
if(!flag)
cout<else
cout<<"日期设置有误,不能输出"<}
int date::getyear()
{
return year;
}
int date::getmonth()
{
return month;
}
int date::getday()
{
return day;
}
void date::operator++(int)
{
day++;
if(month==2)//判断是否是二月
{
bool leapyear;
leapyear=((year%4==0&&year%100!=0)||(year%400==0));//定义闰年
if(leapyear)
{
if(day>29) //若是闰年的二月当 Day 大于 29 时,Day=1,Mon 增加一个月
{
day=1;
month++;
}
}
else
{
if(day>28) //若不是闰年的二月当 Day 大于 28 时,Day=1,Mon 增加一个月
{
day=1;
month++;
}
}
}
else if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
{

if(day>31)//若不是二月月大时,Day=1,Mon 增加一个月
{
day=1;
month++;
}
}
else
{
if(day>30) //若不是二月月小时,Day=1,Mon 增加一个月
{
day=1;
month++;
}
}
if(month>12) //若月大于 12 则 Mon=1,Year 增加一年
{
month=1;
year++;
}
}
void main()
{
date d1(1999,5,30);
d1.show();
d1.setdate();
d1.show();
cout<<"日期增加一天后为:";
d1++;
d1.show();
}



3-80
采用重载运算“+”的方法完成习题 3-76 中的两种工资增加功能。

#include
#include
class Teacher
{
public:
Teacher(char *name,char *id);
Teacher(char *name,char *id,double basepay,double prize,double tax);
Teacher(const Teacher &t);//拷贝构造函数
~Teacher();
void input();//输入基本工资、奖金和所得税
void compute();//计算实际发放数
void display();//显示信息
Teacher operator+(Teacher t); //将其他教师的工资加到该教师
friend Teacher operator+(Teacher t,double Add); //用于将一个数值加到该教师
friend Teacher operator+(doubl

e Add,Teacher t); //用于将一个数值加到该教师

private:
char *Name;//姓名
char *ID;//ID 号
double Basepay; //基本工资
double Prize;//奖金
double Tax;//个人所得税
double wages;//实际发放数
};
Teacher::Teacher(char *name,char *id)
{
Name=new char[strlen(name)+1];
strcpy(Name,name);
ID=new char[strlen(id)+1];
strcpy(ID,id);
}
Teacher::Teacher(char *name,char *id,double basepay,double prize,double tax)
{
Name=new char[strlen(name)+1];
strcpy(Name,name);
ID=new char[strlen(id)+1];
strcpy(ID,id);
Basepay=basepay;
Prize=prize;
Tax=tax;
}
Teacher::Teacher(const Teacher &t)
{
Name=new char[strlen(https://www.360docs.net/doc/4f6761566.html,)+1];
strcpy(Name,https://www.360docs.net/doc/4f6761566.html,);
ID=new char[strlen(t.ID)+1];
strcpy(ID,t.ID);
Basepay=t.Basepay;
Prize=t.Prize;
Tax=t.Tax;
wages=t.wages;
}
Teacher::~Teacher()
{
delete Name;
delete ID;
}
void Teacher::input()
{
cout<<"请输入基本工资:";

cin>>Basepay;
cout<<"请输入奖金:";
cin>>Prize;
cout<<"请输入个人所得税:";
cin>>Tax;
}
void Teacher::compute()
{
wages=Basepay+Prize-Tax;
}
void Teacher::display()
{
cout<<"姓名:"<cout<<"ID 号:"<cout<<"基本工资:"<cout<<"奖金:"<cout<<"个人所得税:"<cout<<"实际发放的工资:"<}
Teacher Teacher::operator+(Teacher t)
{
Teacher temp(Name,ID,Basepay,Prize,Tax);
temp.wages=wages+t.wages;
return temp;
}
Teacher operator+(Teacher t,double Add)
{
Teacher temp(t);
temp.wages=temp.wages+Add;
return temp;
}
Teacher operator+(double Add,Teacher t)
{
Teacher temp(t);
temp.wages=temp.wages+Add;
return temp;
}
void main()
{
Teacher teacher1("张三","001");
cout<<"请输入张三的信息:"<teacher1.input();
https://www.360docs.net/doc/4f6761566.html,pute();
teacher1.display();
cout<
Teacher teacher2("李四","002",3000,1000,120);
https://www.360docs.net/doc/4f6761566.html,pute();
teacher2.display();
cout<Teacher teacher3=teacher1+teacher2;
teacher3.display();
cout<Teacher teacher4=(teacher2+1000);
teacher4.display();
}





3-82 修改例 3-13 中的程序,为类 Point、Circle 和 Cylinder 添加计算面积的成员函数 Area( ),
要求函数 Area( )采用虚函数的形式,并通过积累指针调用虚函数 Area( )。


#include
const double pi=3.14;
class Point
{
protected:

int x;
int y;
public:
Point(int a=0,int b=0)
{
x=a; y=b;
cout<<"Point constructor:"<<'['<}
virtual double Area(){
return 0;
};
~Point ()
{
cout<<"Point destructor:"

<<'['<}
};
class Circle:public Point
{
protected:
int radius;
public:
Circle(int a=0,int b=0,int r=0):Point(a,b)
{
radius=r;
cout<<"Circle constructor:"<<'['<< radius<<']'<<'['<}
double Area()
{
return pi*radius*radius;
}
~Circle ()
{
cout<<"Circle destructor:"<<'['<< radius<<']'<<'['<}
};
class Cylinder :public Circle
{
protected:
int height;
public:
Cylinder(int a=0,int b=0,int r=0,int h=0):Circle( a, b, r)
{
height=h;
cout<<" Cylinder constructor:"<<'['<< height<<']'
<<'['<< radius<<']'<<'['<
}
double Area()
{
return (2*pi*radius*radius+2*pi*radius*height);
}
~Cylinder()
{
cout<<" Cylinder destructor:"<<'['<< height<<']'
<< radius<<']'<<'['<}
};
void main()
{
Point *p;
Circle circle(10,10,20);
Cylinder cylinder(20,30,10,40);
p=&circle;
cout<<"The area of circle is "<Area()<p=&cylinder;
cout<<"The area of cylinder is "<Area()<}



3-83 修改例 3-13 中的程序,将类 Point、Circle 和 Cylinder 的析构函数改为虚析构函数,并
编写代码验证完成的功能。

#include
const double pi=3.14;
class Point
{
protected:
int x;
int y;
public:
Point(int a=0,int b=0)
{
x=a; y=b;
cout<<"Point constructor:"<<'['<}
virtual double Area(){
return 0;
};
virtual ~Point ()
{
cout<<"Point destructor:"<<'['<}
};
class Circle:public Point

{
protected:
int radius;
public:
Circle(int a=0,int b=0,int r=0):Point(a,b)
{
radius=r;
cout<<"Circle constructor:"<<'['<< radius<<']'<<'['<}
double Area()
{
return pi*radius*radius;
}
virtual ~Circle ()
{
cout<<"Circle destructor:"<<'['<< radius<<']'<<'['<}
};
class Cylinder :public Circle
{
protected:
int height;
public:
Cylinder(int a=0,int b=0,int r=0,int h=0):Circle( a, b, r)
{
height=h;
cout<<"Cylinder constructor:"<<'['<< height<<']'
<<'['<< radius<<']'<<'['<}
double Area()
{
return (2*pi*radius*radius+2*pi*radius*height);
}
~Cylinder()
{
cout<<"Cylinder destructor:"<<'['<< height<<']'
<< radius<<']'<<'['<}
};
void main()
{
Point *p=new Cylinder(20,30,10,40);
delete p;
}
如果使用基类指针指向其派生类对象,而这个对象是用 new 运算创建的

相关文档
最新文档