void Show(){ cout<<\}};
int main(){ Point pt(0,0); Circle cl1(100,100,10); Cylinder cy1(100,100,10,8); cout<<\点面积:\ pt.Show();
cout<<\圆面积:\ cl1.Show(); cout<<\圆柱底面积:\ cout<<\圆柱侧面积:\ cout<<\圆柱全面积:\ cout<<\圆柱体积:\ cy1.Show(); return 0; }
实验十七 多重继承与虚基类
1.编程:已知时间类TimeType和日期类DateType,先通过多重继承定义日期时间类DateTimeType1,然后使用聚合方式定义功能完全一样的日期时间类DateTimeType2。
多继承类DateTimeType1:
#include
int hour,minute,second; public: TimeType(int h=0,int m=0,int s=0){ hour=h; minute=m; second=s; } void display(){ cout< class DateType{ int month,day,year; public: DateType(int mo=1,int d=1,int y=2000){ month=mo; day=d; year=y; } void display(){ cout< class DateTimeType:public TimeType,public DateType{ int month,minute,second,hour,day,year; public: DateTimeType (int h,int m,int s,int mo,int d,int y):TimeType(h,m,s),DateType(mo,d,y){ hour=h;minute=m;second=s;month=mo;day=d;year=y;} void display(){ DateType::display(); TimeType::display(); } void SetDateTime (int h,int m,int s,int mo,int d,int y){ DateType::SetDate (mo,d,y); }; } TimeType::SetTime (h,m,s); int main(){ TimeType t1(8,30,30); cout<<\类示例:\ t1.SetTime(8,30,30); t1.display(); DateType d1(6,14,2011); cout<<\类示例:\ d1.SetDate(6,14,2011); d1.display(); cout<<\类示例:\ DateTimeType t2(8,30,30,6,14,2011); t2.SetDateTime(8,30,30,6,14,2011); t2.display(); return 0; } 聚合类DateTimeType2: #include void SetTime(int h,int m,int s){ hour=h; minute=m; second=s; } }; class DateType{ int month,day,year; public: DateType(int mo=1,int d=1,int y=2000){ month=mo; day=d; year=y; } void display(){ cout< 实验小结: 这次实验重点在继承的理解。近段时间上课较快,我们很少时间消化,内容比较多,也感觉越来越难。所以从周一到周三一直在看书调程序。 通过一点点把错误改过,对继承的理解加深了,对多继承的操作 也明白了。其实实验也是一种很好的学习方法,实验迫使我们不断看书理解,在实验中消化了书本知识。
相关推荐: