⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 计算几何形状的面积.cpp

📁 计算几何形状的面积. 有10个几何形状
💻 CPP
字号:
/*有10个几何形状,其中有3个矩形,4个三角形,3个圆形。
编写程序用一个函数area(),能够计算每个形状的面积。
在主程序中构造这些形状并输出总面积。
1)必须使用类的继承和派生。即基类为“形状”,矩形、三角形、圆形都从基类派生;
2)必须使用虚函数和多态性;
(布置时间:2008-5-22;上交时间:2008-5-29)*/

#include<iostream>
using namespace std;
class shape
{
private:
	float X;
public:
	shape(float x=0){X=x;}
	virtual void show(){cout<<"Let's calculate the area of the shape!"<<endl;}
	virtual float area(){return X*X;}
};

class round:public shape
{
private:
	float R;
public:
	round(float r){R=r;}
	void show(){cout<<"Let's calculate the area of the round!"<<endl;}
	float area(){return 3.14*R*R;}
};

class rectangle:public shape
{
private:
	float W,H;
public:
	rectangle(float w,float h){W=w;H=h;}
	void show(){cout<<"Let's calculate the area of the rectangle!"<<endl;}
	float area(){return W*H;}
};

class triangle:public shape
{
private:
	float A,B;
public:
	triangle(float a,float b){A=a;B=b;}
	void show(){cout<<"Let's calculate the area of the triangle!"<<endl;}
	float area(){return 0.5*A*B;}
};


int main()
{
	float all=0;
	shape s(0),*p;
	s.show();
	//定义好的,没劲~
	/*round ro1(1),ro2(2),ro3(3);
	rectangle re1(1,1.5),re2(2,2.1),re3(3,4);
	triangle tr1(3,5),tr2(4,2.2),tr3(4,5.2),tr4(2,7);*/
    int i;
	for(i=1;i<=3;i++)//round
	{
		cout<<"输入圆的半径:"<<endl;
		float r;
		cin>>r;
		round ron(r);
		p=&ron;
		p->show();
		all=all+p->area();
	}
		for(i=1;i<=3;i++)//rectangle
	{
		cout<<"输入正方形的长和宽:"<<endl;
		float w,h;
		cin>>h>>w;
		rectangle rec(w,h);
		p=&rec;
		p->show();
		all+=p->area();
	}
		for(i=1;i<=4;i++)//triangle
	{
		cout<<"输入三角形的底和高:"<<endl;
		float a,b;
		cin>>a>>b;
	    triangle tri(a,b);
		p=&tri;
		p->show();
		all+=p->area();
	}

	cout<<"所有图形的总面积是:"<<all<<endl;

	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -