派生

编写代码实现一个表示点的父类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;
//请编写你的代码
int main(){
    float x,y,r;
    cin>>x>>y>>r;
    Cir c(x,y,r);
    cout<<fixed<<setprecision(2)<<c.getArea()<<endl;
    return 0;
}

 

输入格式:

输入圆心和半径,x y r中间用空格分隔。

输出格式:

输出圆的面积,小数点后保留2位有效数字,注意:const double PI=3.14,面积=PI*r*r

输入样例:

在这里给出一组输入。例如圆的中心点为原点(0,0),半径为3:

0 0 4
 

输出样例:

在这里给出相应的输出。例如:

Dot constructor called
Cir constructor called
50.24
Cir destructor called
Dot destructor called
 1 #include <iostream>
 2 #include<iomanip>
 3 using namespace std;
 4 const double PI=3.14;
 5 class Dot
 6 {   private:
 7     float x,y;
 8  public:
 9     Dot(float a,float b)
10     {   x=a;
11         y=b;
12         cout<<"Dot constructor called"<<endl;
13     }
14  Dot(){
15      cout<<"Dot constructor called"<<endl;
16  }
17  ~Dot()
18  {
19      cout<<"Dot destructor called"<<endl;
20  }
21 };
22 class Cir:public Dot
23 {
24     public:
25     Cir(){
26     cout<<"Cir constructor called"<<endl;
27     }
28     Cir(float a,float b,float c):Dot(a,b),r(c)
29     {  
30         cout<<"Cir constructor called"<<endl;
31     }
32     ~Cir()
33     {
34         cout<<"Cir destructor called"<<endl;
35     }
36    float getArea()
37     {
38    return PI*r*r;
39     }
40     private:
41     float r;
42 };
43 
44 int main(){
45     float x,y,r;
46     cin>>x>>y>>r;
47     Cir c(x,y,r);
48     cout<<fixed<<setprecision(2)<<c.getArea()<<endl;
49     return 0;
50 }

 

posted @ 2023-04-26 22:45  刘冰宇  阅读(84)  评论(0)    收藏  举报