继承与派生的概念

定义基类shape,它有默认的构造函数和析构函数;
把点point类声明为shape的派生类,它包含两个数据成员x和y 和输出函数display();
把圆circle定义为点point类的派生类,它在点point的基础上增加一个数据成员radius;并增加输出成员函数 display ()和求面积成员函数area()。

#include <iostream>
using namespace std;
class shape
{
public:
    shape()
    {
        cout<<endl<<"constructor"<<endl;
    }
    ~shape()
    {
        cout<<endl<<"destructor"<<endl;
    }
};
class point:public shape
{
private:
    double x,y;
public:
    point(){}

    point(double a,double b)
    {
        x=a;
        y=b;
    }
    void display()
    {
        cout<<"x="<<x<<",y="<<y<<endl;
    }
};
class circle:public point
{
private:
    double radius;
public:
    circle(double c)
> 如果circle和point有必要联系,
> 就写成circle(int c,int a,int b ):point(a,b)> 如果没有,在point类中要确保point()能工作;
    {
        radius=c;
    }
    void display()
    {
        cout<<"radius="<<radius<<endl;
    }
    void area()
    {
        cout<<"area="<<radius*radius*3.14<<endl;
    }

};
int main()
{
    point p(3,4);
    p.display();
    circle c(3);
    c.display();
    c.area();
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值