第4章 继承与派生习题 参考答案
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
void show()
{cout<<"以点";Point::show();
cout<<"为圆心,"<<"半径为"<<radius
<<"的圆面积为"<<area()<<endl;}
private:
double radius;
};
int main(void)
{
Rectangle R(1.2,2.2,6.5,2.5);
}
void show1()
{
cout<<"country"<<"\t"<<"captal"<<"\t"<<"personCount"<<endl;
cout<<countryName<<"\t"<<captal<<"\t"<<personCount<<endl;
}
private:
char countryName[20];
{ dormitories =d; studcount =sc; }
void show()
{
cout<<"floors="<<floors<<" rooms="<<rooms<<" area="<<area
<<" dormitories="<<dormitories<<" studcount="
<<studcount<<endl<<endl;
Circle C(0.5,0.6,4);
R.show();
C.show();
return 0;
}
double width;
};
//以点Point为圆心构造圆
class Circle : public Point
{
public:
Circle(double x,double y,double r):Point(x,y),radius(r){}
double area()
{return 3.1415926*radius*radius;}
1)将Rectangle的继承方式改为公有继承class Rectangle:publicPoint;
2)在Rectangle类中重定义move(),getx()和gety()函数,覆盖基类的同名函数。
void Rectangle::move(intxoffset,intyoffset){Point::move(xoffset,yoffset);}
<<" classrooms="<<classrooms<<endl<<endl;
}
private:
int classrooms;
};
class DormBuilding:public Building{
public:
DormBuilding (int f,int r,double a,int d,int sc) :Building(f,r,a)
cout<<"The value is:"<<obj->getValue()<<endl;
return 0;
}
四、看程序写结果(共24分,每题3分)
1.
2.
3.
4.
5.
6.输出:
21
7.输出:
ABA~B~A~A
8.输出:
PDS
五、编程题(共23分)
1.定义一个国家基类Country,包含国名、首都、人口等属性,派生出省类Province,增加省会城市、人口数量属性。
#include<iostream>
using namespace std;
class Country
{
public:
Country(char cn[],char ct[],int pc)
{ strcpy(countryName,cn);
strcpy(captal,ct);
personCount=pc;
double area()
{return length*width;}
void show()
{cout<<"以点";Point::show();
cout<<"为端点,长为,"<<length<<"宽为"<<width
<<"的矩形面积为"<<area()<<endl;}
private:
double length;
第4章继承与派生习题参考答案
一、选择题(共30分,每题1分)
1
2
3
4
5
6
7
8
9
10
D
C
C
B
D
A
B
B
D
C
11
12
13
14
15
16
17
18
19
20
D
C
B
A
A
A
B
B
A
C
21
22
23
24
25
26
27
28
29
30
D
C
D
C
C
D
B
A
B
C
二、填空题(共17分,每空1分)
1.基类
2.(1)派生类 (2)派生类中子对象类 (3)基类
程序代码:
#include <iostream>
using namespace std;
class Building{
public:
Building(int f,int r,double a) { floors=f; rooms=r; area=a; }
protected:
int floors;
int rooms;
double area;
};
class TeachBuilding:public Building{
public:
TeachBuilding (int f,int r,double a,int cr):Building(f,r,a){ classrooms=cr; }
void show()
{
cout<<"floors="<<floors<<" rooms="<<rooms<<" area="<<area
};
//以点Point为一端点构造矩形
class Rectangle : public Point
{
public:
Rectangle(double x,double y,double length,double width):
Point(x,y),length(length),width(width){}
}
private:
int dormitories;
int studcount;
};
int main()
{
TeachBuilding TB(6, 80,200,35);
TB.show();
DormBuilding DB(6, 80,200,35,300);
DB.show();
return 0;
}
3.定义一个Point类,派生出矩形类Rectangle和圆类Circle,计算各派生类对象的面积Area(),并显示出坐标点和面积。
Member(int val):value(val){ }
};
class MyClass{
Member _m;
public :
MyClass(int val):_m(val){ }
int getValue() const { return _m.value;}
};
int main()
{ MyClass *obj=new MyClass(10);
改正方法,可以用作用域区分符加以限定,如改成:
myc.A::x=10;myc.A::display();
或myc.B::x=10;myc.B::display();
3.
#include<iostream>
using namespace std;
class Member{
public:
int value;
void Rectangle::getx(){return Point::getx();}
void Rectangle::gety(){return Point::gety();}
2.分析:类A、B中有同名公有数据成员x和同名成员函数display(),在主函数中访问对象myc的数据成员x是无法确定是访问从A中继承的还是从B中继承的x;调用成员函数也是如此,无法确认是调用类A中的还是类B中的,产生二义性。
proPersonCount=propc;
}
void show2()
{
show1();
cout<<"city"<<'\t'<<"proPersonCount"<<endl;
cout<<city<<'\t'<<proPersonCount<<endl;
}
private:
char city[20];
int proPersonCount;
};
void main()
{
Province p("美国","华盛顿",3213889,"洛杉矶",30963);
p.show2();
}
2.建立一个基类Building类,用来存储楼房的层数、房间数和总面积,由基类派生出教学楼TeachBuilding类和宿舍楼DormBuilding类,教学楼增加教室数,宿舍楼类增加宿舍数、容纳学生总人数。编写应用程序,建立教学楼对象和宿舍楼对象,并输出它们的有关数据信息。
#include<iostream>
using namespace std;
class Point
{
public:
Point(double x,double y):x(x),y(y){}
void show()
{cout<<"("<<x<<","<<y<<")";}
private:
double x;
double y;
char captal[20];
int personCount;
};
class Province :public Country
{
public:
Province(char cn[],char ct[],int pc,char ci[],int propc):Country(cn,ct,pc)
{
strcpy(city,ci);
3.(1)c1、b2、b3、A2、A3 (2)c2 (3)c3
4.(1)基类 (2)派生类
5.~A(){cout<<"bb";}
6.A(aa),c(aa+1)或c(aa+1),A(aa)
7.(1)公有成员(2)公有和保护成员
8.(1)保护(2)成员函数(3)不可访问
三、改错题(共6ቤተ መጻሕፍቲ ባይዱ,每题2分)
1.保护继承方式使基类的public成员在派生类中的访问属性变为protected,所以派生类Rectangle的对象r不能直接访问基类的成员函数move()、getx()和gety()。其改正方法有两种:
{cout<<"以点";Point::show();
cout<<"为圆心,"<<"半径为"<<radius
<<"的圆面积为"<<area()<<endl;}
private:
double radius;
};
int main(void)
{
Rectangle R(1.2,2.2,6.5,2.5);
}
void show1()
{
cout<<"country"<<"\t"<<"captal"<<"\t"<<"personCount"<<endl;
cout<<countryName<<"\t"<<captal<<"\t"<<personCount<<endl;
}
private:
char countryName[20];
{ dormitories =d; studcount =sc; }
void show()
{
cout<<"floors="<<floors<<" rooms="<<rooms<<" area="<<area
<<" dormitories="<<dormitories<<" studcount="
<<studcount<<endl<<endl;
Circle C(0.5,0.6,4);
R.show();
C.show();
return 0;
}
double width;
};
//以点Point为圆心构造圆
class Circle : public Point
{
public:
Circle(double x,double y,double r):Point(x,y),radius(r){}
double area()
{return 3.1415926*radius*radius;}
1)将Rectangle的继承方式改为公有继承class Rectangle:publicPoint;
2)在Rectangle类中重定义move(),getx()和gety()函数,覆盖基类的同名函数。
void Rectangle::move(intxoffset,intyoffset){Point::move(xoffset,yoffset);}
<<" classrooms="<<classrooms<<endl<<endl;
}
private:
int classrooms;
};
class DormBuilding:public Building{
public:
DormBuilding (int f,int r,double a,int d,int sc) :Building(f,r,a)
cout<<"The value is:"<<obj->getValue()<<endl;
return 0;
}
四、看程序写结果(共24分,每题3分)
1.
2.
3.
4.
5.
6.输出:
21
7.输出:
ABA~B~A~A
8.输出:
PDS
五、编程题(共23分)
1.定义一个国家基类Country,包含国名、首都、人口等属性,派生出省类Province,增加省会城市、人口数量属性。
#include<iostream>
using namespace std;
class Country
{
public:
Country(char cn[],char ct[],int pc)
{ strcpy(countryName,cn);
strcpy(captal,ct);
personCount=pc;
double area()
{return length*width;}
void show()
{cout<<"以点";Point::show();
cout<<"为端点,长为,"<<length<<"宽为"<<width
<<"的矩形面积为"<<area()<<endl;}
private:
double length;
第4章继承与派生习题参考答案
一、选择题(共30分,每题1分)
1
2
3
4
5
6
7
8
9
10
D
C
C
B
D
A
B
B
D
C
11
12
13
14
15
16
17
18
19
20
D
C
B
A
A
A
B
B
A
C
21
22
23
24
25
26
27
28
29
30
D
C
D
C
C
D
B
A
B
C
二、填空题(共17分,每空1分)
1.基类
2.(1)派生类 (2)派生类中子对象类 (3)基类
程序代码:
#include <iostream>
using namespace std;
class Building{
public:
Building(int f,int r,double a) { floors=f; rooms=r; area=a; }
protected:
int floors;
int rooms;
double area;
};
class TeachBuilding:public Building{
public:
TeachBuilding (int f,int r,double a,int cr):Building(f,r,a){ classrooms=cr; }
void show()
{
cout<<"floors="<<floors<<" rooms="<<rooms<<" area="<<area
};
//以点Point为一端点构造矩形
class Rectangle : public Point
{
public:
Rectangle(double x,double y,double length,double width):
Point(x,y),length(length),width(width){}
}
private:
int dormitories;
int studcount;
};
int main()
{
TeachBuilding TB(6, 80,200,35);
TB.show();
DormBuilding DB(6, 80,200,35,300);
DB.show();
return 0;
}
3.定义一个Point类,派生出矩形类Rectangle和圆类Circle,计算各派生类对象的面积Area(),并显示出坐标点和面积。
Member(int val):value(val){ }
};
class MyClass{
Member _m;
public :
MyClass(int val):_m(val){ }
int getValue() const { return _m.value;}
};
int main()
{ MyClass *obj=new MyClass(10);
改正方法,可以用作用域区分符加以限定,如改成:
myc.A::x=10;myc.A::display();
或myc.B::x=10;myc.B::display();
3.
#include<iostream>
using namespace std;
class Member{
public:
int value;
void Rectangle::getx(){return Point::getx();}
void Rectangle::gety(){return Point::gety();}
2.分析:类A、B中有同名公有数据成员x和同名成员函数display(),在主函数中访问对象myc的数据成员x是无法确定是访问从A中继承的还是从B中继承的x;调用成员函数也是如此,无法确认是调用类A中的还是类B中的,产生二义性。
proPersonCount=propc;
}
void show2()
{
show1();
cout<<"city"<<'\t'<<"proPersonCount"<<endl;
cout<<city<<'\t'<<proPersonCount<<endl;
}
private:
char city[20];
int proPersonCount;
};
void main()
{
Province p("美国","华盛顿",3213889,"洛杉矶",30963);
p.show2();
}
2.建立一个基类Building类,用来存储楼房的层数、房间数和总面积,由基类派生出教学楼TeachBuilding类和宿舍楼DormBuilding类,教学楼增加教室数,宿舍楼类增加宿舍数、容纳学生总人数。编写应用程序,建立教学楼对象和宿舍楼对象,并输出它们的有关数据信息。
#include<iostream>
using namespace std;
class Point
{
public:
Point(double x,double y):x(x),y(y){}
void show()
{cout<<"("<<x<<","<<y<<")";}
private:
double x;
double y;
char captal[20];
int personCount;
};
class Province :public Country
{
public:
Province(char cn[],char ct[],int pc,char ci[],int propc):Country(cn,ct,pc)
{
strcpy(city,ci);
3.(1)c1、b2、b3、A2、A3 (2)c2 (3)c3
4.(1)基类 (2)派生类
5.~A(){cout<<"bb";}
6.A(aa),c(aa+1)或c(aa+1),A(aa)
7.(1)公有成员(2)公有和保护成员
8.(1)保护(2)成员函数(3)不可访问
三、改错题(共6ቤተ መጻሕፍቲ ባይዱ,每题2分)
1.保护继承方式使基类的public成员在派生类中的访问属性变为protected,所以派生类Rectangle的对象r不能直接访问基类的成员函数move()、getx()和gety()。其改正方法有两种: