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

📄 8_21.cpp

📁 10个比较经典的C++程序。初学者就先多学习学习别人吧。
💻 CPP
字号:
#include <cmath>
#include <iostream>
using namespace std;
const double PI=3.14159265358979;
class Shape					//声明抽象基类
{public:
  	virtual void Disp() = 0;
  	virtual float area() = 0;
};
class TwoDShape : public Shape	//抽象派生类TwoDShape
{public:
  	virtual float perimeter() = 0;
};
class ThreeDShape : public Shape	//抽象派生类ThreeDShape 
{public:
  	virtual float volume() = 0;
};
class Circle : public TwoDShape
{public:
  	Circle(float r) : radius(r) { }
  	void Disp() { cout << "Shape is a circle.\n"; }
  	float perimeter() { return 2*PI*radius; }
  	float area() { return PI*radius*radius; }
private:
  	float radius;
};
class Rectangle : public TwoDShape
{public:
  	Rectangle(float l, float w) : length(l),width(w) { }
  	void Disp() { cout << "Shape is a Rectangle.\n"; }
  	float perimeter() { return 2*(length+width); }
  	float area() { return length*width; }
private:
  	float length,width;
};
class Cone : public ThreeDShape
{public:
  	Cone(float r, float h) : radius(r), height(h) { }
  	void Disp() { cout << "Shape is a Cone.\n"; }
  	float area(){return PI*radius*(radius+sqrt(radius*radius + height*height));}
  	float volume() { return PI*radius*radius*height/3; }
private:
  	float radius, height;
};
class Box : public ThreeDShape
{public:
  	Box(float l, float w,float h) : length(l),width(w), height(h) { }
  	void Disp() { cout << "Shape is a Box.\n"; }
  	float area(){return 2*(length*width+length*height+width*height);}
  	float volume() { return length*width*height; }
private:
  	float length,width, height;
};
int main()
{	Shape *ShapePtr=0;
	Circle c(2);
	ShapePtr=&c;	ShapePtr->Disp();
	//cout<<ShapePtr->perimeter();//语句1:编译错误
	cout<<ShapePtr->area()<<"\n";
	Rectangle r(2,3);
	ShapePtr=&r;	ShapePtr->Disp();
	//cout<<ShapePtr->perimeter();//语句2:编译错误
	cout<<ShapePtr->area()<<"\n";
	Cone co(1, 2);
	ShapePtr=&co;	ShapePtr->Disp();
	//cout<<ShapePtr->perimeter();//语句3:编译错误
	cout<<ShapePtr->area()<<"\n";
	Box b(1, 2,3);
	ShapePtr=&b;	ShapePtr->Disp();
	//cout<<ShapePtr->perimeter();//语句4:编译错误
	cout<<ShapePtr->area()<<"\n";
	return 0;
}

⌨️ 快捷键说明

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