C++学习笔记:类的继承和派生--点圆类(公有继承)

以下题为例:

1463: 继承派生(2)-点圆类

Time Limit: 10 Sec Memory Limit: 65535 MB 64bit IO Format: %lld
Description
二维坐标系的点类Point包含私有成员数据横纵坐标值x和y,均为int类型。Point类包含若干成员函数(如下所示)。
class Point
{
private:
int x,y;
public:
Point();
Point(int xx,int yy);
void Show();
};
圆类Circle由Point类公有派生而来,包含私有成员数据半径radius,为int类型。Circle类包含若干成员函数(如下所示)。
class Circle:public Point
{
private:
int radius;
public:
Circle();
Circle(int x,int y,int r);
Circle(Point p,int r);
Circle(int r);
Circle(Point p);
void Show();
};
完成上述各成员函数的设计。main函数已给定(如下所示),提交时只需要提交main函数外的代码部分。
int main()
{
int x,y,r;
int cas=0;
Point p0;
Circle c0;
cout<<"Point 0: "; p0.Show();
cout<<“Circle0: “; c0.Show();
while(cin>>x>>y>>r)
{
cas++;
cout<<“Case #”<<cas<<”:”<<endl;
Point p1(x,y);
Circle c1(x+5,y+5,r);
Circle c2(p1,r);
Circle c3®;
Circle c4(p1);
cout<<"Point 1: "; p1.Show();
cout<<"Circle1: "; c1.Show();
cout<<"Circle2: "; c2.Show();
cout<<"Circle3: "; c3.Show();
cout<<"Circle4: "; c4.Show();
}
return 0;
}

Input
包含多组数据(数据均正确合法)
每组测试数据1行,包含3个整数。

Output
每组测试数据输出具体格式详见Sample Output。

Sample Input 1 2 3

Sample Output
Function #1 is called!
Function #2 is called!
Function #4 is called!
Point 0: (0,0)
Function #3 is called!
Circle0: Radius=0 Center=(1000,1000)
Function #3 is called!
Function #9 is called!
Case #1:
Function #2 is called!
Function #2 is called!
Function #5 is called!
Function #6 is called!
Function #2 is called!
Function #7 is called!
Function #8 is called!
Point 1: (1,2)
Function #3 is called!
Circle1: Radius=3 Center=(6,7)
Function #3 is called!
Function #9 is called!
Circle2: Radius=3 Center=(1,2)
Function #3 is called!
Function #9 is called!
Circle3: Radius=3 Center=(100,100)
Function #3 is called!
Function #9 is called!
Circle4: Radius=10 Center=(1,2)
Function #3 is called!
Function #9 is called!


本题需要标注调用函数步骤,即需要熟练掌握类的继承基类派生类函数的调用。
本题为公有继承,首先先说说什么是公有继承:
1.基类的public和protected成员在派生类中的访问权限不变;但是基类的private成员不可以直接访问。
2.派生类生成的对象只能访问基类public权限的成员。
其次,掌握使用基类函数的方法
eg:point 类为基类,circle类为派生类,则在circle中访问point类函数方式为:

    point::show();

在circle函数中使用point构造函数的方式为:

    Circle::Circle(int x,int y,int r):Point(x,y)//形式为        :+构造函数
    {
   
        radius=r;
        cout<<"Function #5 is called!"
  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值