📄 p9_9.cpp
字号:
/************************************
* p9_9.cpp *
* 抽象类的使用 *
*************************************/
#include<iostream>
using namespace std;
class Shape //抽象类
{
public:
virtual double area()const=0;
virtual void show()=0;
Shape(){
cout<<"shape cons.."<<endl;
}
};
class Point:public Shape
{
protected:
int X,Y;
public:
Point(int X=0,int Y=0)
{
this->X=X,this->Y=Y;
}
void show()
{
cout<<"("<<X<<","<<Y<<")"<<endl;
}
double area() const //求面积
{
return 0.0;
}
};
const double PI=3.14159;
class Circle :public Point
{
protected:
double radius; //半径
public:
Circle( int X, int Y,double R):Point(X,Y)
{
radius=R;
}
double area() const //求面积
{
return PI*radius*radius;
}
void show()
{
cout<<"Centre:"<<"("<<X<<","<<Y<<")"<<endl;
cout<<"radius:"<<radius<<endl;
}
};
class Cylinder: public Circle
{
private:
double height;
public:
Cylinder(int X, int Y, double R, double H):Circle(X,Y,R)
{
height=H;
}
double area()
{
return 2*Circle::area()+2*PI*radius*height;
}
void show()
{
Circle::show();
cout<<"height of cylinder:"<<height<<endl;
}
};
void main()
{
Cylinder CY(100,200,10,50);
Shape * P;
P=&CY;
P->show();
cout<<"total area:"<<P->area()<<endl;
Circle Cir(5,10,100);
Shape &R=Cir;
R.show();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -