Visual C++面向对象编程教程 第2版 王育坚 清华大学出版社 课后答案报告doc.docx
《Visual C++面向对象编程教程 第2版 王育坚 清华大学出版社 课后答案报告doc.docx》由会员分享,可在线阅读,更多相关《Visual C++面向对象编程教程 第2版 王育坚 清华大学出版社 课后答案报告doc.docx(121页珍藏版)》请在冰豆网上搜索。
VisualC++面向对象编程教程第2版王育坚清华大学出版社课后答案报告doc
3-44
一个名为CPerson的类有如下属性:
姓名、身份证号、性别和年龄,请用C++语言定义
这个类,并为上述属性定义相应的方法。
#include
#include
classCPerson
{
private:
charName[10];
charID[20];
charSex[4];
intAge;
public:
CPerson(char*na,char*id,char*se,intag);
voidShow();
};
CPerson:
:
CPerson(char*na,char*id,char*se,intag)
{
strcpy(Name,na);
strcpy(ID,id);
strcpy(Sex,se);
Age=ag;
}
voidCPerson:
:
Show()
{
cout<<"姓名:
"<cout<<"身份证:
"<cout<<"性别:
"<cout<<"年龄:
"<}
voidmain()
{
CPersonperson("王三","610102198506041952","男",21);
person.Show();
}
3-45设计一个日期类Date,该类用于表示日期值(年、月、日)。
要求除了能够通过相应得成
员函数设置和获取日期外,还能够实现将日期加一天的操作。
#include
classdate{
intyear;
intmonth;
intday;
boolflag;
public:
date()
{
year=0;
month=0;
day=0;
}
date(intyr,intmo,intda);
voidsetdate();
intgetyear();
intgetmonth();
intgetday();
voidaddday();
voidshow();
};
date:
:
date(intyr,intmo,intda)
{
flag=false;
if(mo>=1&&mo<=12&&da>=1&&da<=31)
{
year=yr;month=mo;day=da;
}
else
{
flag=true;
}
}
voiddate:
:
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;
}
voiddate:
:
show()
{
if(!
flag)
cout<else
cout<<"日期设置有误,不能输出"<}
intdate:
:
getyear()
{
returnyear;
}
intdate:
:
getmonth()
{
returnmonth;
}
intdate:
:
getday()
{
returnday;
}
voiddate:
:
addday()
{
day++;
if(month==2)//判断是否是二月
{
boolleapyear;
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++;
}
}
}
elseif(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++;
}
}
voidmain()
{
dated1(1999,5,30);
d1.show();
d1.setdate();
d1.show();
cout<<"日期增加一天后为:
";
d1.addday();
d1.show();
}
3-46
#include
usingnamespacestd;
classCRectangle
{
private:
doubleX;
doubleY;
doublelength;
doublewidth;
public:
CRectangle(doubles,doublee,doublel,doublew)
{
X=s;
Y=e;
length=l;
width=w;
}
~CRectangle(){}
voidMove(double,double);
voidSize(double,double);
voidWhere();
voidArea();
};
voidCRectangle:
:
Move(doublex,doubley)
{
cout<<"矩形按向量("<cout<<"现在矩形左上角所在的位置:
"<cout<<"("<}
voidCRectangle:
:
Size(doublecl,doublecw)
{
cout<<"要更改的长和宽:
"<length=cl;
width=cw;
cout<}
voidCRectangle:
:
Where()
{
cout<<"现在矩形左上角所在的位置:
"<cout<<"("<}
voidCRectangle:
:
Area()
{
doublearea;
area=length*width;
cout<
}
intmain()
{
CRectanglecr(2,3,5,4);
cr.Where();
cr.Area();
cr.Move(1,2);
cr.Size(2,3);
return0;
}
3-47
#include
usingnamespacestd;
classCRectangle
{
private:
doubleX;
doubleY;
doublelength;
doublewidth;
public:
CRectangle(doubles,doublee,doublel,doublew)
{
X=s;
Y=e;
length=l;
width=w;
}
~CRectangle(){}
voidMove(double,double);
voidSize(double,double);
voidWhere();
voidArea();
};
voidCRectangle:
:
Move(doublex,doubley)
{
cout<<"矩形按向量("<cout<<"现在矩形左上角所在的位置:
"<cout<<"("<}
voidCRectangle:
:
Size(doublecl,doublecw)
{
cout<<"要更改的长和宽:
"<length=cl;
width=cw;
cout<}
voidCRectangle:
:
Where()
{
cout<<"现在矩形左上角所在的位置:
"<cout<<"("<}
voidCRectangle:
:
Area()
{
doublearea;
area=length*width;
cout<
}
intmain()
{
CRectanglecr(2,3,5,4);
cr.Where();
cr.Area();
cr.Move(1,2);
cr.Size(2,3);
return0;
}
3-48
#include
#include
classBank{
private:
charnumber[20];
intmoney;
public:
voidsetnumber(char*num,intmon)
{
strcpy(number,num);
money=mon;
}
voidchanger(intmon)
{
money+=mon;
}
voidshow()
{
cout<<"当前的总钱数为:
"<}
};
voidmain()
{
charnum[20];
intmoney;
intaddmoney;
cout<<"输入用户名和存入的钱:
"<cin>>num>>money;
Bankbank;
bank.setnumber(num,money);
bank.show();
cout<<"输入转账的钱:
"<cin>>addmoney;
bank.changer(addmoney);
bank.show();
}
3-49
#include
usingnamespacestd;
classProduct
{
private:
charname[20];
doubleprice;
intm_count;
public:
Product(){}
~Product(){}
voidsetproduct();
voidsellproduct();
voidshow();
};
voidProduct:
:
setproduct()
{
intn;
cout<<"输入生产产品数量:
"<cin>>n;
m_count=n;
}
voidProduct:
:
sellproduct()
{
intn;
cout<<"输入销售产品数量:
"<cin>>n;
m_count-=n;
}
voidProduct:
:
show()
{
cout<<"输出剩余产品数量:
"<cout<}
intmain()
{
Productp;
p.setproduct();
p.sellproduct();
p.show();
return0;
}
3-50建立一个名为Student的类,该类有以下几个私有成员变量:
学生姓名、学号、性别和年龄。
还有以下两个成员函数:
一个用于初始化学生姓名、学号、性别和年龄的构造函数,一个用于输出学生信息的函数。
编写一个主函数,声明一个学生对象,然后调用成员函数在屏幕上输出学生信息。
#include
#include
classStudent
{
public:
Student(char*name,char*num,char*sex,intage);
~Student();
voidshow();
private:
char*Name;
char*Num;
char*Sex;
intAge;
};
Student:
:
Student(char*name,char*num,char*sex,intage)
{
Name=newchar[strlen(name)+1];//注意字符串的赋值
strcpy(Name,name);
Num=newchar[strlen(num)+1];
strcpy(Num,num);
Sex=newchar[strlen(sex)+1];
strcpy(Sex,sex);
Age=age;
}
Student:
:
~Student()
{
deleteName;
deleteNum;
deleteSex;
}
voidStudent:
:
show()
{
cout<<"姓名:
"<cout<<"学号:
"<cout<<"性别:
"<cout<<"年龄:
"<}
voidmain()
{
Studentstudent("张三","0401011201","男",18);
student.show();
}
3-51
设计一个CPetrol类,包含以下几个私有成员:
90号93号98号汽油加油量和单价,当天的总收入。
类还包含以下几个成员函数:
设置有关数据成员的构造函数,输入加油量并计算总收入的成员函数。
利用类编写主函数:
假设加油站某天909398号汽油单价分别为3.964.054.38计算并输出加油站一天的收入。
#include
usingnamespacestd;
classCPetrol
{
public:
CPetrol();
voidsetamount();
doubletotal;
private:
doubleam_90;
doubleam_93;
doubleam_98;
doubleprice_90;
doubleprice_93;
doubleprice_98;
};
CPetrol:
:
CPetrol()
{
price_90=3.96;
price_93=4.05;
price_98=4.38;
}
voidCPetrol:
:
setamount()
{
cout<<"inputthreeamounts!
"<cin>>am_90>>am_93>>am_98;
total=am_90*price_90+am_93+price_93+am_98+price_98;
}
voidmain()
{
CPetrolc;
c.setamount();
cout<<"Thetotalis"<}
3-52
修改习题3-50中的类Student,添加一个静态成员变量,用于表示已创建对象的数量;
添加两个静态成员函数,一个用于输出已创建对象的数量,一个用于输出一个学生的姓名和
学号。
#include
#include
classStudent
{
public:
Student(char*name,char*num,char*sex,intage);
~Student();
staticvoidshow(Student&a);
staticvoidshowstudentnum();
private:
char*Name;
char*Num;
char*Sex;
intAge;
staticintstudentnum;
};
intStudent:
:
studentnum=0;
Student:
:
Student(char*name,char*num,char*sex,intage)
{
studentnum++;
Name=newchar[strlen(name)+1];
strcpy(Name,name);
Num=newchar[strlen(num)+1];
strcpy(Num,num);
Sex=newchar[strlen(sex)+1];
strcpy(Sex,sex);
Age=age;
}
Student:
:
~Student()
{
deleteName;
deleteSex;
}
voidStudent:
:
showstudentnum()
{
cout<<"学生的数量是:
"<}
voidStudent:
:
show(Student&a)
{
cout<<"姓名:
"<cout<<"学号:
"<cout<<"性别:
"<cout<<"年龄:
"<}
voidmain()
{
Studentstudent1("张三","0401011201","男",18);
Student:
:
show(student1);//注意调用方式,静态成员可以通过类名调用
Student:
:
showstudentnum();//注意调用方式
Studentstudent2("李四","0401011202","男",18);
Student:
:
show(student2);
Student:
:
showstudentnum();
}
3-53
编写程序用静态成员的方法实现对班费的管理,要求定义一个类Student,除了声明一个存放班费的静态成员,还要分别定义一个上交班费的成员函数Contribute(),花费班费的成员函数Spend()和显示班费的静态成员函数Display()
//student.cpp
#include
usingnamespacestd;
classStudent
{
private:
staticdoublefee;//fee--班费,静态成员数据
public:
Student(){}//默认构造函数,析构函数
~Student(){}
voidContribute(doublen);//n--上缴的班费数额
voidSpend(doublen);//n--花费班费数量
staticvoidDisplay();//静态成员函数
};
doubleStudent:
:
fee=0;//类声明外面对静态数据成员初始化
//类方法
voidStudent:
:
Contribute(doublen)
{
fee=fee+n;
}
voidStudent:
:
Spend(doublen)
{
if(feecout<<"班费不够,请求失败!
\n";
else
fee=fee-n;
}
voidStudent:
:
Display()
{
cout<<"现有班费:
"<}
intmain()
{
Studentstu;
stu.Display();
stu.Contribute(103.4);//交钱
stu.Display();
stu.Spend(42.3);//花钱
stu.Display();
return0;
}
放在了一个文件里了,上面是类声明,下面是测试小程序,运行过了,没问题
3-54
定义一个类A,该类除了有两个数据成员x,y外,还有一个对象备份函数copy。
copy函数的功能说明如下:
对于类A的对象a1和a2,函数调用a1.copy(a2)表示将对象a2赋值给对象a1.(提示利用this指针防止一个对象对自己赋值)
#include
usingnamespacestd;
classTest
{
private:
char*a,*b;
public:
Test()
{
a=newchar[100];
b=newchar[100];
}
~Test()
{
delete[]a;
delete[]b;
}
Test©(Test&B)
{
if(this==&B)return*this;
intlen=strlen(B.a);
delete[]a;
a=newchar[len+1];
strcpy(a,B.a);
len=strlen(B.b);
delete[]b;
b=newchar[len+1];
strcpy(b,B.b);
return*this;
}
voidmytest(char*str1,char*str2)
{
strcpy(a,str1);
strcpy(b,str2);
}
voidmyprint()
{
cout<}
}A,B;
intmain()
{
B.mytest("thisis","B");
B.myprint();
A.copy(B);
A.myprint();
return0;
}
3-55