定义一个抽象类Shape,应用抽象类Shape派生圆类circle、圆的内接正方形类In_Square和圆的外切正方形类Ex_Square。main中利用指向抽象类的指针实现求圆、圆的内接正方形和圆的外切正方形的面积和周长。请实现各个类的成员函数,已知各类的定义及main中的代码如下(不允许修改):
时间: 2023-04-04 15:00:24 浏览: 291
抽象类Shape的定义如下:
```
class Shape {
public:
virtual double getArea() = ;
virtual double getPerimeter() = ;
};
```
圆类Circle的定义如下:
```
class Circle : public Shape {
private:
double radius;
public:
Circle(double r) : radius(r) {}
double getArea() {
return 3.14 * radius * radius;
}
double getPerimeter() {
return 2 * 3.14 * radius;
}
};
```
圆的内接正方形类In_Square的定义如下:
```
class In_Square : public Shape {
private:
double radius;
public:
In_Square(double r) : radius(r) {}
double getArea() {
return 2 * radius * radius;
}
double getPerimeter() {
return 8 * radius;
}
};
```
圆的外切正方形类Ex_Square的定义如下:
```
class Ex_Square : public Shape {
private:
double radius;
public:
Ex_Square(double r) : radius(r) {}
double getArea() {
return 4 * radius * radius;
}
double getPerimeter() {
return 16 * radius / sqrt(2);
}
};
```
main函数中的代码如下:
```
int main() {
Shape* p;
Circle c(5);
In_Square in(5);
Ex_Square ex(5);
p = &c;
cout << "圆的面积:" << p->getArea() << endl;
cout << "圆的周长:" << p->getPerimeter() << endl;
p = ∈
cout << "圆的内接正方形的面积:" << p->getArea() << endl;
cout << "圆的内接正方形的周长:" << p->getPerimeter() << endl;
p = &ex;
cout << "圆的外接正方形的面积:" << p->getArea() << endl;
cout << "圆的外接正方形的周长:" << p->getPerimeter() << endl;
return ;
}
```
以上是我对你的问题的回答,希望能够帮助到你。
阅读全文