打卡第三十一天

定义基类Point和派生类Circle,求圆的周长

一、

1.Point类定义两个私有的数据成员float x,y

2.Circle类新增一个私有的数据成员半径float r和一个公有的求周长的函数getCircumference()

二、

三、

#include<iostream>
#include<iomanip>
#define PI 3.14
using namespace std;
class Point {
protected:
float X, Y;
public:
Point() {}
Point(float x, float y) :X(x), Y(y) {
cout << "Point constructor called" << endl;
}~Point() {
cout << "Point destructor called" << endl;
}
};
class Circle :public Point {
float R;
public:
float c;
Circle() {}
Circle(float x, float y, float r) :Point(x, y), R(r) {
cout << "Circle constructor called" << endl;
};
~Circle() {
cout << "Circle destructor called" << endl;
}
float getCircumference();
};
float Circle::getCircumference() {
c = PI * R * 2;
return c;
}int main()
{
float x, y, r;
cin >> x >> y >> r;
Circle c(x, y, r);
cout << fixed << setprecision(2) << c.getCircumference() << endl;
return 0;
}

四、

#include<iostream>
#include<iomanip>
#define PI 3.14
using namespace std;
class Point {
protected:
float X, Y;
public:
Point() {}
Point(float x, float y) :X(x), Y(y) {
cout << "Point constructor called" << endl;
}~Point() {
cout << "Point destructor called" << endl;
}
};
class Circle :public Point {
float R;
public:
float c;
Circle() {}
Circle(float x, float y, float r) :Point(x, y), R(r) {
cout << "Circle constructor called" << endl;
};
~Circle() {
cout << "Circle destructor called" << endl;
}
float getCircumference();
};
float Circle::getCircumference() {
c = PI * R * 2;
return c;
}int main()
{
float x, y, r;
cin >> x >> y >> r;
Circle c(x, y, r);
cout << fixed << setprecision(2) << c.getCircumference() << endl;
return 0;
}

posted @ 2023-05-22 18:10  欧吼吼  阅读(15)  评论(0编辑  收藏  举报