7-3 单继承中的构造函数与析构函数

编写代码实现一个表示点的父类Dot和一个表示圆的子类Cir,求圆的面积。

Dot类有两个private数据成员 float x,y;

Cir类新增一个private的数据成员半径float r 和一个public的求面积的函数getArea( );

主函数已经给出,请编写Dot和Cir类。

编写这个的总体的大致思路是什么,怎么去思考和进行编写

#include <iostream>
#include<iomanip>
using namespace std;
const double PI=3.14;
//请编写你的代码
class Dot{
    float x,y;
    public:
        Dot(int x,int y)
        {
            printf("Dot constructor called\n") ;
        }
        virtual ~Dot(){
            printf("Dot destructor called\n");
        }
};
class Cir:public Dot{
    private :
        float r;
    public :
    Cir(int a,int b,int c):Dot(a,b),r(c){
        printf("Cir constructor called\n") ;
    }
     ~Cir(){
            printf("Cir destructor called\n");
        }
    float getArea(){
        return r*r*PI;
    }
};


int main(){
    float x,y,r;
    cin>>x>>y>>r;
    Cir c(x,y,r);
    cout<<fixed<<setprecision(2)<<c.getArea()<<endl;
    return 0;
}



#include <iostream>
using namespace std;
 
class Base
{
public:
    Base()
    {
        cout<<"Base中的默认构造函数调用"<<endl;
    }
    ~Base()
    {
        cout<<"Base中的析构函数调用"<<endl;
    }
};
 
class Other
{
public:
    Other()
    {
        cout<<"Other中的默认构造函数调用"<<endl;
    }
 
    ~Other()
    {
        cout<<"Other中的析构函数调用"<<endl;
    }
};
 
class Son : Base
{
public:
    Son()
    {
        cout<<"Son中的默认构造函数调用"<<endl;
    }
    
    ~Son()
    {
        cout<<"Son中的析构函数调用"<<endl;
    }
    
    Other o;
};
 
int main()
{
    Son s;
    return 0;
}