c++定义一个类base,其中包含一个虚函数area(),用来计算面积。正方形square是其派生类,使用area()函数计算其面积。编写程序,并完成计算测试
时间: 2024-03-22 22:41:11 浏览: 27
好的,下面是代码实现:
```c++
#include <iostream>
using namespace std;
class base{
public:
virtual double area() = 0;//定义纯虚函数area
};
class square:public base{
public:
square(double a){
side = a;
}
double area(){
return side * side; //计算正方形面积
}
private:
double side;
};
int main(){
base *b;
square s(5);
b = &s;
cout << "正方形面积为:" << b->area() << endl;
return 0;
}
```
程序运行结果:
```
正方形面积为:25
```
程序说明:
1. 首先定义一个基类 `base` ,其中包含一个纯虚函数 `area()` ,用来计算面积。
2. 然后定义一个派生类 `square` ,它继承自 `base` ,并重写了 `area()` 函数,用来计算正方形的面积。
3. 在主函数中,定义一个指向 `base` 类对象的指针 `b` ,并将 `square` 类对象 `s` 的地址赋给它。
4. 最后调用 `b` 指针的 `area()` 函数,输出正方形的面积。
相关问题
编写一个C++程序,用虚函数和定义类来实现,用于计算正方形,三角形和圆的面积以及计算各类形状的总面积
好的,我可以为您提供一个使用虚函数和定义类的C++程序,用于计算正方形、三角形和圆的面积,并计算各类形状的总面积。代码如下:
```C++
#include <iostream>
#include <cmath>
using namespace std;
class Shape {
public:
virtual double area() = 0;
};
class Square : public Shape {
private:
double side;
public:
Square(double s) : side(s) {}
virtual double area() {
return side * side;
}
};
class Triangle : public Shape {
private:
double base, height;
public:
Triangle(double b, double h) : base(b), height(h) {}
virtual double area() {
return 0.5 * base * height;
}
};
class Circle : public Shape {
private:
double radius;
public:
Circle(double r) : radius(r) {}
virtual double area() {
return M_PI * radius * radius;
}
};
double polygonArea(Shape **shapes, int n) {
double totalArea = 0;
for(int i=0; i<n; i++) {
totalArea += shapes[i]->area();
}
return totalArea;
}
int main() {
double side, base, height, radius;
Shape *shapes[3];
cout << "请输入正方形的边长:";
cin >> side;
Square square(side);
shapes[0] = □
cout << "正方形的面积为:" << square.area() << endl;
cout << "请输入三角形的底边长和高:";
cin >> base >> height;
Triangle triangle(base, height);
shapes[1] = ▵
cout << "三角形的面积为:" << triangle.area() << endl;
cout << "请输入圆的半径:";
cin >> radius;
Circle circle(radius);
shapes[2] = &circle;
cout << "圆的面积为:" << circle.area() << endl;
cout << "各类形状的总面积为:" << polygonArea(shapes, 3) << endl;
return 0;
}
```
在这个程序中,我们定义了一个Shape类作为基类,它包含一个纯虚函数area(),用于计算形状的面积。然后,我们定义了三个派生类Square、Triangle和Circle,它们分别表示正方形、三角形和圆形。这些派生类覆盖了基类的area()函数,以实现各自特定的计算面积的算法。
在主函数中,我们使用用户输入来获取各种形状的参数,并创建相应的对象。然后,我们使用一个Shape类型的指针数组将这些对象存储起来,并通过调用PolygonArea函数来计算各类形状的总面积。注意,在PolygonArea函数中,我们使用了Shape类的指针来遍历所有形状,并调用它们的area()函数来计算面积,这就是虚函数的应用场景之一。
3、编写程序,计算圆形、三角形、正方形和长方形四种图形的面积 提示,定义抽象类shape,在其中说明一个纯虚函数area()作为接口。在派生类中定义具体的函数实现。
以下是一个使用抽象类和多态实现计算四种图形面积的示例程序:
```c++
#include <iostream>
using namespace std;
class Shape {
public:
virtual double area() = 0;
};
class Circle : public Shape {
private:
double radius;
public:
Circle(double r) {
radius = r;
}
double area() {
return 3.14 * radius * radius;
}
};
class Triangle : public Shape {
private:
double base;
double height;
public:
Triangle(double b, double h) {
base = b;
height = h;
}
double area() {
return 0.5 * base * height;
}
};
class Square : public Shape {
private:
double side;
public:
Square(double s) {
side = s;
}
double area() {
return side * side;
}
};
class Rectangle : public Shape {
private:
double width;
double height;
public:
Rectangle(double w, double h) {
width = w;
height = h;
}
double area() {
return width * height;
}
};
int main() {
Shape* shapes[4];
shapes[0] = new Circle(5);
shapes[1] = new Triangle(7, 4);
shapes[2] = new Square(3);
shapes[3] = new Rectangle(6, 2);
for (int i = 0; i < 4; i++) {
cout << "Area of shape " << i+1 << ": " << shapes[i]->area() << endl;
}
return 0;
}
```
输出:
```
Area of shape 1: 78.5
Area of shape 2: 14
Area of shape 3: 9
Area of shape 4: 12
```
在这个示例中,我们定义了一个抽象类 `Shape`,其中包含一个纯虚函数 `area()`。接着,我们定义了四个派生类 `Circle`、`Triangle`、`Square` 和 `Rectangle`,每个类都必须实现 `Shape` 中的纯虚函数 `area()`。在 `main()` 函数中,我们创建了一个 `Shape` 类型的指针数组 `shapes`,并使用派生类对象初始化它的元素。然后,我们通过循环遍历指针数组,调用每个对象的 `area()` 函数,输出它们的面积值。由于 `Shape` 类型的指针可以指向它的任何派生类对象,所以我们可以通过多态性实现灵活的计算不同形状的面积。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)