我是靠谱客的博主 悲凉蛋挞,这篇文章主要介绍【6.13】 定义基类Base,其数据成员为高h,定义成员函数disp为虚函数。然后再由High派生出长方体类Cuboid与圆柱体类Cylinder。并在两派生类中定义成员函数disp为虚函数。在主函,现在分享给大家,希望可以做个参考。

#include <iostream>
 using namespace std;
 class Base{
 protected:
double h;
 public:

Base(double h1)
{  
h=h1;
}
virtual void disp()=0;
 };
 class cuboid:public Base{
 private:
double wid,len;
 public:
cuboid(double l,double w,double h1):Base(h1)
{    len=l;
    wid=w;
}
    
void disp()
{  cout<<"cuboid:"<<endl;
  cout<<"extent:  "<<len<<endl;
  cout<<"width:  "<<wid<<endl;
  cout<<"height:  "<<h<<endl;
  cout<<"The bulk of cuboid is :  "<<len*wid*h<<endl;
}
 };
 class Cylinder:public Base{
 private:
double r;
 public:
Cylinder(double h1,double r1):Base(h1)
{   r=r1;
}
void disp()
{    cout<<"cylinder:"<<endl;
    cout<<"height:  "<<h<<endl;
    cout<<"radius:  "<<r<<endl;
    cout<<"The bulk of cylinder is :  "<<3.1415*r*r*h<<endl;
}
 };
 int main()
 {   Base *p;

cuboid cu(4,7,5);
Cylinder cy(6,9);
p=&cy;
p->disp();
p=&cu;
p->disp();
system("pause");
return 0;
 }

运行结果:



最后

以上就是悲凉蛋挞最近收集整理的关于【6.13】 定义基类Base,其数据成员为高h,定义成员函数disp为虚函数。然后再由High派生出长方体类Cuboid与圆柱体类Cylinder。并在两派生类中定义成员函数disp为虚函数。在主函的全部内容,更多相关【6.13】内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(58)

评论列表共有 0 条评论

立即
投稿
返回
顶部