打卡第十八天
定义基类Point和派生类Circle,求圆的周长
一、
1.定义基类Point(点)和派生类Circle(圆),求圆的周长。
2.Point类有两个私有的数据成员float x,y;
3.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;
}