基于上篇C++ 多态:Shape类层次结构
https://mp.csdn.net/mp_blog/creation/editor/133175074
验证OOP的多态性,包括函数重载、虚函数;掌握采用OOP的多态特性解决问题的方法。
- //Shape类
- class Shape
- {
- public:
- virtual double getArea(){}//计算面积
- virtual double getVolume(){}//计算体积
- virtual void print(){}//打印
- virtual void draw(int x,int y,char c){}//绘制形状
- Shape(){}//构造
- ~Shape(){}//析构
- };
- class TwoDimensionalShape : public Shape//二维
- {
- public:
- TwoDimensionalShape(){}
- virtual double getArea(){}
- virtual void print(){}
- virtual void draw(int x,int y,char c){}
- };
- class Square : public TwoDimensionalShape//二维的正方形
- {
- public:
- Square(){}
- Square(double side)
- {
- side_m=side;
- }
- double getArea()
- {
- return side_m*side_m;
- }
- void print()
- {
- cout<<"二维的正方形的面积为:"<<getArea()<
-
- }
- void draw(int x,int y,char c)
- {
- int a[50][50];
- for(int i=0;i<50;i++)
- {
- for(int j=0;j<50;j++)
- {
- a[i][j]=0;
- }
- }
-
- for(int i=0;i<50;i++)
- {
- for(int j=0;j<50;j++)
- {
- if(j>=x-1 && j
-1 && i>=y-1 && i-1) - {
- a[i][j]=1;
- }
- if(j>=x-2 && j
=y-2 && i1) - {
- a[i][j]=2;
- }
- }
- }
- for(int i=0;i<50;i++)
- {
- cout<
- for(int j=0;j<50;j++)
- {
- if(a[i][j]==1)
- cout<
" "; - else
- cout<<" ";
- }
- }
-
- }
- private:
- double side_m;//边长
- };
4.长方形类Rectangle,继承于二维类TwoDimensionalShape。draw()所用的方法与正方形的思想一样。
- class Rectangle_ : public TwoDimensionalShape//二维的长方形
- {
- public:
- Rectangle_(){}
- Rectangle_(double length,double width)
- {
- length_m=length;
- width_m=width;
- }
- double getArea()
- {
- return length_m*width_m;
- }
- void draw(int x,int y,char c)
- {
- int a[50][50];
- for(int i=0;i<50;i++)
- {
- for(int j=0;j<50;j++)
- {
- a[i][j]=0;
- }
- }
- for(int i=0;i<50;i++)
- {
- for(int j=0;j<50;j++)
- {
- if(j>=x-1 && j
-1 && i>=y-1 && i-1) - {
- a[i][j]=1;
- }
- if(j>=x-2 && j
=y-2 && i1) - {
- a[i][j]=2;
- }
- }
- }
- for(int i=0;i<50;i++)
- {
- cout<
- for(int j=0;j<50;j++)
- {
- if(a[i][j]==1)
- cout<
" "; - else
- cout<<" ";
- }
- }
- }
- void print()
- {
- cout<<"二维的长方形的面积为:"<<getArea()<
- }
- private:
- double length_m;//长
- double width_m;//宽
- };
5.三角形类Triangle,继承于二维类TwoDimensionalShape。规定的是等腰直角三角形(好吧就是因为这个好画),所用的方法与前面两个的思想一样。
-
- class Triangle : public TwoDimensionalShape//二维的三角形 (这里定义为等腰直角三角形)
- {
- public:
- Triangle(){}
- Triangle(double side)
- {
- side_m=side;
- }
- double getArea()
- {
- return 0.5*side_m*side_m;
- }
- void draw(int x,int y,char c)
- {
- int a[50][50];
- for(int i=0;i<50;i++)
- {
- for(int j=0;j<50;j++)
- {
- a[i][j]=0;
- }
- }
- for(int i=0,k=-y+2;i<50;i++,k++)
- {
- for(int j=0;j<50;j++)
- {
- if(j>=x-1 && j
-1 && i>=y-1 && i-1) - {
- a[i][j]=1;
- }
- if(j>=x-2 && j
=y-3 && i1) - {
- a[i][j]=2;
- }
- }
- }
- for(int i=0;i<50;i++)
- {
- cout<
- for(int j=0;j<50;j++)
- {
- if(a[i][j]==1)
- cout<
" "; - else
- cout<<" ";
- }
- }
- }
- void print()
- {
- cout<<"二维的三角形的面积为:"<<getArea()<
- }
- private:
- double side_m;//等腰直角三角形的边长
- };
6.圆形类Circle,继承于二维类TwoDimensionalShape (在这里我想说一下,关于画圆以及如何填充圆,推荐参考文章 Bresenham 画圆算法原理)。因为我们事先固定了画布的范围,所以需要注意的是圆的半径是有规定输入范围的。还有,因为对Bresenham画圆算法学的不好,就学会了画圆的轮廓,还不会填充T-T。
- class Circle : public TwoDimensionalShape//二维的圆形
- {
- public:
- Circle(){}
- Circle(double radius)
- {
- radius_m=radius;
- }
- double getArea()
- {
- return E*radius_m*radius_m;
- }
- void draw(int x,int y,char c)
- {
- //1=
- int a[50][50];
- for(int i=0;i<50;i++)
- {
- for(int j=0;j<50;j++)
- {
- a[i][j]=0;
- }
- }
- int r=x;
- int p;
- x=0;
- y=r;
- p=3-2*r;
- for(;x<=y;x++)
- {
- a[ x+r][ y+r]=1;
- a[ x+r][-y+r]=1;
- a[-x+r][ y+r]=1;
- a[-x+r][-y+r]=1;
- a[ y+r][ x+r]=1;
- a[ y+r][-x+r]=1;
- a[-y+r][ x+r]=1;
- a[-y+r][-x+r]=1;
- if(p>=0)
- {
- p+=4*(x-y)+10;
- y--;
- }
- else
- {
- p+=4*x+6;
- }
-
- }
- for(int i=0;i<50;i++)
- {
- cout<
- for(int j=0;j<50;j++)
- {
- if(a[i][j]==1)
- cout<
" "; - else
- cout<<" ";
- }
- }
- }
- void print()
- {
- cout<<"二维的圆形的面积为:"<<getArea()<
- }
- private:
- double radius_m;//圆的半径
- };
-
7.绘制图形。设计交互,让用户选择填充字符。
- char cinColor()
- {
- char c;
- int color;
- cout<<"填充字符有:1.'*' 2.'#' 3.'@' 4.'&'"<
- cout<<"请输入你想绘制该形状的填充字符(编号),若超出范围则默认为'&':"<
- cin>>color;
- if(color==1)
- c='*';
- else if(color==2)
- c='#';
- else if(color==3)
- c='@';
- else
- c='&';
- return c;
- }
8.测试程序功能。
- int main()
- {
- char cinColor();
- vector
shapeclass; - vector
::iterator iter; - int a;
- int x,y;
- a=-1;
- while(a!=0)
- {
- cout<<"******二维形状图形********"<
- <<"********1.正方形**********"<
- <<"********2.长方形**********"<
- <<"********3.三角形**********"<
- <<"********4.圆形************"<
- <<"********0.退出************"<
- cout<<"请输入你所选择的形状(编号):"<
- cin>>a;
- switch(a)
- {
- case 1:
- {
- double side;
- cout<<"Please enter the side of square:"<
- cin>>side;
- Shape *ptr_Square=new Square(side);
- shapeclass.push_back(ptr_Square);
- iter = shapeclass.begin();
- (*iter)->print();
- char ch=cinColor();
- cout<<"请输入图形的起始坐标:"<
- cin>>x>>y;
- (*iter)->draw(x,y,ch);
- delete ptr_Square;
- }
- break;
- case 2:
- {
- double length,width;
- cout<<"Please enter the length and width of the rectangle:"<
- cin>>length>>width;
- Shape *ptr_Rectangle_=new Rectangle_(length,width);
- shapeclass.push_back(ptr_Rectangle_);
- iter = shapeclass.begin();
- (*iter)->print();
- char ch=cinColor();
- cout<<"请输入图形的起始坐标:"<
- cin>>x>>y;
- (*iter)->draw(x,y,ch);
- delete ptr_Rectangle_;
- }
- break;
- case 3:
- {
- double side;
- cout<<"Please enter the side of the triangle: "<
- cin>>side;
- Shape *ptr_Triangle=new Triangle(side);
- shapeclass.push_back(ptr_Triangle);
- iter = shapeclass.begin();
- (*iter)->print();
- char ch=cinColor();
- cout<<"请输入图形的起始坐标:"<
- cin>>x>>y;
- (*iter)->draw(x,y,ch);
- delete ptr_Triangle;
- }
- break;
- case 4:
- {
- double radius;
- cout<<"Please enter the radius of Circle(0
< - cin>>radius;
- Shape *ptr_Circle=new Circle(radius);
- shapeclass.push_back(ptr_Circle);
- iter = shapeclass.begin();
- (*iter)->print();
- char ch=cinColor();
- x=y=radius;
- (*iter)->draw(x,y,ch);
- delete ptr_Circle;
- }
- break;
- case 0:
- {
- a=0;
- }
- break;
- }
- system("pause");
- system("cls");
- cout<
- }
-
- return 0;
- }
四、【完整代码】
- //
- //(使用Shape类层次结构的多态性的屏幕管理器)开发一个基本图形软件包
- /
- #include
- #include
- #include
- #include
- using namespace std;
- #define E 3.14
- //Shape类
- class Shape
- {
- public:
- virtual double getArea(){}//计算面积
- virtual double getVolume(){}//计算体积
- virtual void print(){}//打印
- virtual void draw(int x,int y,char c){}//绘制形状
- Shape(){}//构造
- ~Shape(){}//析构
- };
-
- class TwoDimensionalShape : public Shape//二维
- {
- public:
- TwoDimensionalShape(){}
- virtual double getArea(){}
- virtual void print(){}
- virtual void draw(int x,int y,char c){}
- };
- class Square : public TwoDimensionalShape//二维的正方形
- {
- public:
- Square(){}
- Square(double side)
- {
- side_m=side;
- }
- double getArea()
- {
- return side_m*side_m;
- }
- void print()
- {
- cout<<"二维的正方形的面积为:"<<getArea()<
-
- }
- void draw(int x,int y,char c)
- {
- int a[50][50];
- for(int i=0;i<50;i++)
- {
- for(int j=0;j<50;j++)
- {
- a[i][j]=0;
- }
- }
-
- for(int i=0;i<50;i++)
- {
- for(int j=0;j<50;j++)
- {
- if(j>=x-1 && j
-1 && i>=y-1 && i-1) - {
- a[i][j]=1;
- }
- if(j>=x-2 && j
=y-2 && i1) - {
- a[i][j]=2;
- }
- }
- }
- for(int i=0;i<50;i++)
- {
- cout<
- for(int j=0;j<50;j++)
- {
- if(a[i][j]==1)
- cout<
" "; - else
- cout<<" ";
- }
- }
-
- }
- private:
- double side_m;//边长
- };
-
- class Rectangle_ : public TwoDimensionalShape//二维的长方形
- {
- public:
- Rectangle_(){}
- Rectangle_(double length,double width)
- {
- length_m=length;
- width_m=width;
- }
- double getArea()
- {
- return length_m*width_m;
- }
- void draw(int x,int y,char c)
- {
- int a[50][50];
- for(int i=0;i<50;i++)
- {
- for(int j=0;j<50;j++)
- {
- a[i][j]=0;
- }
- }
- for(int i=0;i<50;i++)
- {
- for(int j=0;j<50;j++)
- {
- if(j>=x-1 && j
-1 && i>=y-1 && i-1) - {
- a[i][j]=1;
- }
- if(j>=x-2 && j
=y-2 && i1) - {
- a[i][j]=2;
- }
- }
- }
- for(int i=0;i<50;i++)
- {
- cout<
- for(int j=0;j<50;j++)
- {
- if(a[i][j]==1)
- cout<
" "; - else
- cout<<" ";
- }
- }
- }
- void print()
- {
- cout<<"二维的长方形的面积为:"<<getArea()<
- }
- private:
- double length_m;//长
- double width_m;//宽
- };
-
- class Triangle : public TwoDimensionalShape//二维的三角形 (这里定义为等腰直角三角形)
- {
- public:
- Triangle(){}
- Triangle(double side)
- {
- side_m=side;
- }
- double getArea()
- {
- return 0.5*side_m*side_m;
- }
- void draw(int x,int y,char c)
- {
- int a[50][50];
- for(int i=0;i<50;i++)
- {
- for(int j=0;j<50;j++)
- {
- a[i][j]=0;
- }
- }
- for(int i=0,k=-y+2;i<50;i++,k++)
- {
- for(int j=0;j<50;j++)
- {
- if(j>=x-1 && j
-1 && i>=y-1 && i-1) - {
- a[i][j]=1;
- }
- if(j>=x-2 && j
=y-3 && i1) - {
- a[i][j]=2;
- }
- }
- }
- for(int i=0;i<50;i++)
- {
- cout<
- for(int j=0;j<50;j++)
- {
- if(a[i][j]==1)
- cout<
" "; - else
- cout<<" ";
- }
- }
- }
- void print()
- {
- cout<<"二维的三角形的面积为:"<<getArea()<
- }
- private:
- double side_m;//等腰直角三角形的边长
- };
-
- class Circle : public TwoDimensionalShape//二维的圆形
- {
- public:
- Circle(){}
- Circle(double radius)
- {
- radius_m=radius;
- }
- double getArea()
- {
- return E*radius_m*radius_m;
- }
- void draw(int x,int y,char c)
- {
- //1=
- int a[50][50];
- for(int i=0;i<50;i++)
- {
- for(int j=0;j<50;j++)
- {
- a[i][j]=0;
- }
- }
- int r=x;
- int p;
- x=0;
- y=r;
- p=3-2*r;
- for(;x<=y;x++)
- {
- a[ x+r][ y+r]=1;
- a[ x+r][-y+r]=1;
- a[-x+r][ y+r]=1;
- a[-x+r][-y+r]=1;
- a[ y+r][ x+r]=1;
- a[ y+r][-x+r]=1;
- a[-y+r][ x+r]=1;
- a[-y+r][-x+r]=1;
- if(p>=0)
- {
- p+=4*(x-y)+10;
- y--;
- }
- else
- {
- p+=4*x+6;
- }
-
- }
- for(int i=0;i<50;i++)
- {
- cout<
- for(int j=0;j<50;j++)
- {
- if(a[i][j]==1)
- cout<
" "; - else
- cout<<" ";
- }
- }
- }
- void print()
- {
- cout<<"二维的圆形的面积为:"<<getArea()<
- }
- private:
- double radius_m;//圆的半径
- };
-
- char cinColor()
- {
- char c;
- int color;
- cout<<"填充字符有:1.'*' 2.'#' 3.'@' 4.'&'"<
- cout<<"请输入你想绘制该形状的填充字符(编号),若超出范围则默认为'&':"<
- cin>>color;
- if(color==1)
- c='*';
- else if(color==2)
- c='#';
- else if(color==3)
- c='@';
- else
- c='&';
- return c;
- }
-
- int main()
- {
- char cinColor();
- vector
shapeclass; - vector
::iterator iter; - int a;
- int x,y;
- a=-1;
- while(a!=0)
- {
- cout<<"******二维形状图形********"<
- <<"********1.正方形**********"<
- <<"********2.长方形**********"<
- <<"********3.三角形**********"<
- <<"********4.圆形************"<
- <<"********0.退出************"<
- cout<<"请输入你所选择的形状(编号):"<
- cin>>a;
- switch(a)
- {
- case 1:
- {
- double side;
- cout<<"Please enter the side of square:"<
- cin>>side;
- Shape *ptr_Square=new Square(side);
- shapeclass.push_back(ptr_Square);
- iter = shapeclass.begin();
- (*iter)->print();
- char ch=cinColor();
- cout<<"请输入图形的起始坐标:"<
- cin>>x>>y;
- (*iter)->draw(x,y,ch);
- delete ptr_Square;
- }
- break;
- case 2:
- {
- double length,width;
- cout<<"Please enter the length and width of the rectangle:"<
- cin>>length>>width;
- Shape *ptr_Rectangle_=new Rectangle_(length,width);
- shapeclass.push_back(ptr_Rectangle_);
- iter = shapeclass.begin();
- (*iter)->print();
- char ch=cinColor();
- cout<<"请输入图形的起始坐标:"<
- cin>>x>>y;
- (*iter)->draw(x,y,ch);
- delete ptr_Rectangle_;
- }
- break;
- case 3:
- {
- double side;
- cout<<"Please enter the side of the triangle: "<
- cin>>side;
- Shape *ptr_Triangle=new Triangle(side);
- shapeclass.push_back(ptr_Triangle);
- iter = shapeclass.begin();
- (*iter)->print();
- char ch=cinColor();
- cout<<"请输入图形的起始坐标:"<
- cin>>x>>y;
- (*iter)->draw(x,y,ch);
- delete ptr_Triangle;
- }
- break;
- case 4:
- {
- double radius;
- cout<<"Please enter the radius of Circle(0
< - cin>>radius;
- Shape *ptr_Circle=new Circle(radius);
- shapeclass.push_back(ptr_Circle);
- iter = shapeclass.begin();
- (*iter)->print();
- char ch=cinColor();
- x=y=radius;
- (*iter)->draw(x,y,ch);
- delete ptr_Circle;
- }
- break;
- case 0:
- {
- a=0;
- }
- break;
- }
- system("pause");
- system("cls");
- cout<
- }
-
- return 0;
- }
-
五、【运行结果展示】




六、【绘制UML图】

-
相关阅读:
Python:if判断--综合案例练习:石头剪刀布
RocketMQ之MappedFileQueue详解
vtk for java 打包部署和运行
软件测试之如何提升产品的用户体验度
luffy-(8)
linux部署nacos记录
阿里云云原生一体化数仓 — 湖仓一体新能力解读
redis主从复制玩法全过程笔记(redis7+版本)
金仓数据库KingbaseES运维工具参考手册(6. 预防删除工具指南)
平安大视野解读宏观经济:看好中国经济长期潜力,资产配置价值凸显
-
原文地址:https://blog.csdn.net/weixin_74287172/article/details/133175333
-
最新文章
-
沪漂五周年了:我越来越迷茫了
Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
MySQL-Seconds_behind_master的精度误差
[MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
Agent OS :五种驯服不确定性的范式
PortSwigger SQL注入LAB11
数据库即时编译JIT
[Begin]AI Learn Data Day 0
深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU