请结合如图所示的继承关系设计Shape、Circle以及Rectangle类,使得下述代码可以正确计算并输出矩形和圆的面积。 提示:Shape的析构以及area()函数都应为虚函数。//Project - Shapes #include <iostream> using namespace std; //在此处定义Shape, Cirlce及Rectangle类 int main(){ Shape* shapes[2]; int w, h; cin >> w >> h; //输入矩形的长宽 shapes[0] = new Rectangle(w,h); float r; //输入圆的半径 cin >> r; shapes[1] = new Circle(0,0,2); //圆心在(0,0),半径为r的圆 printf("Area of rectangle:%.2f\n",shapes[0]->area()); printf("Area of circle:%.2f\n",shapes[1]->area()); for (auto i=0;i<2;i++) delete shapes[i]; return 0; }
时间: 2023-06-08 14:04:54 浏览: 177
Shape类是一个抽象类,包含一个虚析构函数和一个纯虚函数area()。其下有两个派生类Circle类和Rectangle类,它们分别继承自Shape类。Circle类和Rectangle类都需要重新实现area()方法来计算各自的面积。 在代码中声明一个指向Shape对象的指针,然后依次创建一个Circle对象和一个Rectangle对象,并使用area()方法计算它们的面积并输出。
相关问题
image.png 请结合如图所示的继承关系设计Shape、Circle以及Rectangle类,使得下述代码可以正确计算并输出矩形和圆的面积。 提示:Shape的析构以及area()函数都应为虚函数。
好的,这是一个关于面向对象设计的问题。根据如图所示的继承关系,可以设计如下的类:
```
class Shape {
public:
virtual ~Shape() {}
virtual double area() const = 0;
};
class Circle : public Shape {
public:
Circle(double r) : radius(r) {}
virtual double area() const {
return 3.14 * radius * radius;
}
private:
double radius;
};
class Rectangle : public Shape {
public:
Rectangle(double w, double h) : width(w), height(h) {}
virtual double area() const {
return width * height;
}
private:
double width, height;
};
```
然后可以使用如下代码进行测试:
```
Shape* s1 = new Circle(2.0);
Shape* s2 = new Rectangle(4.0, 6.0);
std::cout << "Area of circle: " << s1->area() << std::endl;
std::cout << "Area of rectangle: " << s2->area() << std::endl;
delete s1;
delete s2;
```
输出结果应该为:
```
Area of circle: 12.56
Area of rectangle: 24
```
希望能够帮助您解决问题。
设计图形Shape接口及其子类圆Circle和矩形Rectangle类。接口中声明求图形面积的方法getArea,并两个子类中实现。再定义测试类Test,实现圆和矩形面积的输出。(接口)
下面是Java代码实现:
```
// Shape接口
public interface Shape {
double getArea();
}
// 圆类
public class Circle implements Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
@Override
public double getArea() {
return Math.PI * radius * radius;
}
}
// 矩形类
public class Rectangle implements Shape {
private double length;
private double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
@Override
public double getArea() {
return length * width;
}
}
// 测试类
public class Test {
public static void main(String[] args) {
Circle c = new Circle(5);
System.out.println("圆的面积为:" + c.getArea());
Rectangle r = new Rectangle(3, 4);
System.out.println("矩形的面积为:" + r.getArea());
}
}
```
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)