Write a c++ program. 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 11:47:33 浏览: 83
Sure, here's the C++ program that meets your requirements:
```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 << "Name: " << 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() override {
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() override {
Shape::show();
cout << "Width: " << width << endl;
cout << "Height: " << height << endl;
}
};
void showShape(const Shape &shape) {
shape.show();
}
int main() {
Circle circle1("Circle", "Red", 5.0);
Rectangle rectangle1("Rectangle", "Green", 4.0, 6.0);
Shape *p_shape = &circle1;
showShape(*p_shape);
p_shape = &rectangle1;
showShape(*p_shape);
return 0;
}
```
In this program, we define a `Shape` class and its subclasses `Circle` and `Rectangle`. The `Shape` class has two data members: `name` and `color`. The `Circle` class has an additional data member `radius`, and the `Rectangle` class has two additional data members `width` and `height`.
Each class has a constructor to initialize its data members. In addition, each class defines a `show()` function to display its data members. The `Shape` class defines this function as virtual, so that its subclasses can override it.
We also define a global function `showShape(const Shape &shape)` to call a `Shape` object's `show()` function to display its content.
In the `main()` function, we create a `Circle` object `circle1` and a `Rectangle` object `rectangle1`. We then define a `Shape` pointer `p_shape` to point to `circle1`, and call `showShape(*p_shape)` to display the circle's information. We then change `p_shape` to point to `rectangle1`, and call `showShape(*p_shape)` to display the rectangle's information.
阅读全文