Define a Shape class and its subclasses Circle and Rectangle. The detailed requirements are: (1) Data members: a) Shape's data members include: name, color. b) Circle has an additional data member: radius. c) Rectangle has two additional data members: width, height. (2) Member functions: a) Each class should have a constructor to initialize its data members. b) Define in each class a function show() to display its data members. (3) A global function "showShape(const Shape &shape)" to call a Shape object’s show() function t o display its content. (4) The main() function: a) Define a Circle object circle1 and a Rectangle object rectangle1. b) Define a Shape pointer p_shape to point to circle1, and call showShape(*p_shape) to displ ay the circle's information. c) Change p_shape to point to rectangle1, and call showShape(*p_shape) to display the recta ngle's information.
时间: 2024-03-06 09:46:32 浏览: 62
Here is an implementation in C++:
```
#include <iostream>
#include <string>
using namespace std;
class Shape {
protected:
string name;
string color;
public:
Shape(string name, string color) {
this->name = name;
this->color = color;
}
virtual void show() {
cout << "Shape: " << name << endl;
cout << "Color: " << color << endl;
}
};
class Circle : public Shape {
private:
double radius;
public:
Circle(string name, string color, double radius) : Shape(name, color) {
this->radius = radius;
}
void show() {
Shape::show();
cout << "Radius: " << radius << endl;
}
};
class Rectangle : public Shape {
private:
double width;
double height;
public:
Rectangle(string name, string color, double width, double height) : Shape(name, color) {
this->width = width;
this->height = height;
}
void show() {
Shape::show();
cout << "Width: " << width << endl;
cout << "Height: " << height << endl;
}
};
void showShape(const Shape &shape) {
shape.show();
}
int main() {
Circle circle1("Circle 1", "Red", 5.0);
Rectangle rectangle1("Rectangle 1", "Blue", 4.0, 6.0);
Shape *p_shape = &circle1;
showShape(*p_shape);
p_shape = &rectangle1;
showShape(*p_shape);
return 0;
}
```
In this implementation, we define a `Shape` class and its subclasses `Circle` and `Rectangle`. The `Shape` class has data members `name` and `color`, and a constructor to initialize them. It also has a virtual member function `show()` to display its data members.
The `Circle` class is a subclass of `Shape` and has an additional data member `radius`, and a constructor to initialize it. It overrides the `show()` function to display its own data members as well as the `Shape`'s data members.
The `Rectangle` class is also a subclass of `Shape` and has additional data members `width` and `height`, and a constructor to initialize them. It also overrides the `show()` function to display its own data members as well as the `Shape`'s data members.
In the `main()` function, we create objects of `Circle` and `Rectangle`, and use a `Shape` pointer to point to each object. We then call the `showShape()` function to display the information of the pointed object, which will call the appropriate `show()` function based on the dynamic type of the object.
阅读全文
相关推荐


















