C++ 基类指针数组,虚函数

 实验题目:
定义一个抽象基类shape,由它派生5个类:circle,square,rectangle,trapezoid,triangle.

用虚函数分别计算图形的面积,要求用基类指针数组,使他们的每个元素指向一个派生类对象 。

程序如下:


#include<iostream>
#include<math.h>
using namespace std;
class Shape
{
public:
	virtual void ShapeName(){}                   //虚函数
	virtual float area() { return 0.0; }         //虚函数
};

class Circle:public Shape
{
public:
	Circle(float r){ radius = r; }
	virtual void ShapeName() { cout << "Circle area: "; }        //对虚函数再定义
	virtual float area() { return 3.14*radius*radius; }
protected:
	float radius;
};

class Square :public Shape
{
public:
	Square(float s){ side = s; };
	virtual void ShapeName() { cout << "Square area: "; }
	virtual float area(){ return side*side; }
protected:
	float side;
};
class Rectangle :public Shape{
public:
	Rectangle(float a, float b){ length = a; width = b; };
	virtual void ShapeName() { cout << "Rectangle area: "; }
	virtual float area(){ return length*width; }
protected:
	float length;
	float width;
};
class Trapezoid :public Shape{
public:
	Trapezoid(float top, float bottom, float h){ topside = top; bottomside = bottom; high = h; };
	virtual void ShapeName() { cout << "Trapezoid area: "; }
	virtual float area(){ return (topside + bottomside)*high / 2; }
protected:
	float topside;
	float bottomside;
	float high;
};
class Triangle :public Shape{
public:
	Triangle(float a, float b, float c){ sidea = a; sideb = b; sidec = c; };
	virtual void ShapeName() { cout << "Triangle area: "; }
	virtual float area(){ float s = (sidea + sideb + sidec) / 2; float m = s*(s - sidea)*(s - sideb)*(s - sidec); return sqrt(m); }
protected:
	float sidea;
	float sideb;
	float sidec;
};
int main(){
	Circle circle(5);
	Square square(5);
	Rectangle rectangle(2, 3);
	Trapezoid trapezoid(3, 4, 8);
	Triangle triangle(3,4,5);
	Shape *p[5] = { &circle, &square,&rectangle,&trapezoid,&triangle };
	for (int i = 0; i < 5; i++){
		p[i]->ShapeName();
		cout << p[i]->area() << endl;
	}
	system("pause");
	return 0;
}


好的,我理解了你的问题。下面是这个问题的解答: ```c++ #include<iostream> using namespace std; class Shape { public: virtual double getArea() = 0; // 纯虚函数,求面积 virtual double getPerimeter() = 0; // 纯虚函数,求周长 }; 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; } }; class Square : public Shape { private: double length; // 边长 public: Square(double l) { length = l; } double getArea() { return length * length; } double getPerimeter() { return 4 * length; } }; class Rectangle : public Shape { private: double width, length; // 长和宽 public: Rectangle(double w, double l) { width = w; length = l; } double getArea() { return width * length; } double getPerimeter() { return 2 * (width + length); } }; int main() { Shape* shapes[3]; // 基类指针数组 double radius, length, width; cout << "请输入圆的半径:"; cin >> radius; shapes[0] = new Circle(radius); cout << "请输入正方形的边长:"; cin >> length; shapes[1] = new Square(length); cout << "请输入长方形的长和宽:"; cin >> length >> width; shapes[2] = new Rectangle(width, length); double sum = 0; for (int i = 0; i < 3; i++) { sum += shapes[i]->getArea(); // 计算面积之和 } cout << "所有图形的面积之和为:" << sum << endl; return 0; } ``` 这是一个使用 C++ 实现的面向对象程序,定义了一个 Shape 类作为基类,以及三个派生类 Circle、Square、Rectangle 分别表示圆、正方形和长方形。每个派生类都实现了求面积和周长的纯虚函数。在主函数中,先让用户输入各种图形的参数,然后创建一个基类指针数组,每个指针都指向一个派生类对象。利用基类指针遍历元素对象,求取所有图形面积之和,并输出结果。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值