继承
来源:互联网 发布:万网域名注册流程图 编辑:程序博客网 时间:2024/06/15 18:52
编写C++程序完成以下功能:(1) 声明一个基类Shape(形状),其中包含一个方法来计算面积;(2) 从Shape派生两个类矩形和圆形;(3) 从矩形派生正方形;(4) 分别实现派生类构造函数、析构函数和其他方法;(5) 创建派生类的对象,观察构造函数、析构函数调用次序;(6) 不同对象计算面积。#include<iostream>#define PI 3.1415using namespace std;class Shape{ public: Shape() {cout<<"调用构造函数Shape"<<endl;} ~Shape() {cout<<"调用析构函数Shape"<<endl;} double Girth() {} double Area() {}};class Rectangle: public Shape{private: double len; double wide;public: Rectangle(double l=0,double w=0) {len=l; wide=w; cout<<"调用构造函数Rectangle"<<endl;} void SetRectangle(double l=0,double w=0) {len=l; wide=w;} ~Rectangle(){cout<<"调用析构函数Rectangle"<<endl;} double Girth() {return (len+wide)*2;} double Area() {return len*wide;}};class Circle:public Shape{private: double r;public: Circle(double radius=0) {r=radius; cout<<"调用构造函数Circle"<<endl;} void SetCircle(double radius=0) {r=radius;} ~Circle(){cout<<"调用析构函数Circle"<<endl;} double Girth() {return 2*PI*r;} double Area() {return PI*r*r;}};class Square:public Rectangle{public: Square(double x=0) {Rectangle(x,x); cout<<"调用构造函数Square"<<endl;} void SetSquare(double x=0) {SetRectangle(x,x);} ~Square(){cout<<"调用析构函数Square"<<endl;}};int main(){ Rectangle b; Circle c; Square d; double x,y,m,n; cout<<endl<<"请输入矩形长和宽(用空格隔开):"<<endl; cin>>x>>y; b.SetRectangle(x,y); cout<<"矩形周长为:"<<b.Girth()<<endl; cout<<"矩形面积为:"<<b.Area()<<endl; cout<<endl<<"请输入圆形的半径:"<<endl; cin>>m; c.SetCircle(m); cout<<"圆形周长为:"<<c.Girth()<<endl; cout<<"圆形面积为:"<<c.Area()<<endl; cout<<endl<<"请输入正方形的边长:"<<endl; cin>>n; d.SetSquare(n); cout<<"正方形周长为:"<<d.Girth()<<endl; cout<<"圆形面积为:"<<d.Area()<<endl; system("pause"); cout<<endl; return 0;}
0 0
- 继承
- 继承
- 继承
- 继承
- 继承
- 继承
- 继承
- 继承
- 继承
- 继承
- 继承
- 继承
- 继承
- 继承
- 继承
- 继承
- 继承
- 继承
- opencv提高之cascade分类器训练人脸检测模型
- 文摘:不值得做的事情,就不值得做好
- c++反向迭代器
- 常用的jquery语句
- cmd打war包
- 继承
- Sublime Text 3 常用插件以及安装方法,Package Control组件安装
- DAY6
- Unity游戏开发实现短信验证(SMSSDK)
- vsftp的配置安装
- Effective c# 第二版读书笔记1
- Tomcat9 users and roles, Remote deploy
- Delphi 10 Seattle小票打印控件TQ_Printer
- Shell——文件包含