📄 mainunit.cpp
字号:
#include <iostream>
#include <typeinfo.h>
using namespace std;
class Shape
{
public:
Shape(){};
~Shape(){};
virtual void Draw(){ //基类的Draw函数
cout<<"Draw Shape..."<<endl;
};
};
typedef Shape * pShape; //定义一个新的指针类型
class Rectangle : public Shape
{
int left,right,top,bottom;
public:
Rectangle(int aleft, int aright, int atop, int abottom){
left= aleft;
right= aright;
top= atop;
bottom= abottom;
};
void Draw(){ //继承类Rectangle的Draw函数,绘制一个矩形
cout<<"Draw Rectangle..."<<endl;
};
};
class Circle : public Shape
{
private:
int x,y,r;
public:
Circle(int ax, int ay, int ar){
x=ax;
y=ay;
r=ar;
};
void Draw(){ //继承类Circle的Draw函数,绘制一个圆形
cout<<"Draw Circle..."<<endl;
};
int getR(){ //返回半径的值
return r;
};
};
int main(int argc, char* argv[])
{
const int ShapeNum=30; //定义一个常量使程序更具有可读性
pShape ShapeList[ShapeNum];
for(int i=0;i<ShapeNum;i++)
{
if(i %3==0)
ShapeList[i]= new Shape();
else if(i %3==1)
ShapeList[i]= new Rectangle(0+i, 100-i, 0+i, 100-i);
else if(i%3==2)
ShapeList[i]=new Circle(100,100,50-i);
}
for( int i=0;i<ShapeNum;i++)
{
if(typeid(* ShapeList[i])==typeid(Circle))
cout<<dynamic_cast<Circle *>(ShapeList[i])->getR()<<endl;;
ShapeList[i]->Draw();
delete ShapeList[i];
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -