欢迎来到天天文库
浏览记录
ID:8855719
大小:38.65 KB
页数:2页
时间:2018-04-09
《c抽象类纯虚数学习代码》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、写一个程序,定义抽象类型Shape,由他派生三个类:Circle(圆形),Rectangle(矩形),Trapezoid(梯形),用一个函数printArea分别输出三者的面积,3个图形的数据在定义对象是给定。#includeclassShape{public:virtualdoublearea()const=0;};classCircle:publicShape{private:doubler;public:Circle(doublea):r(a){}virtualdoublearea()const{return3*r*r;}};classRectangle:publi
2、cShape{private:doublew;doubleh;public:Rectangle(doublea,doubleb):w(a),h(b){}virtualdoublearea()const{returnw*h;}};classTrapezoid:publicShape{private:doublew;doubleh;public:Trapezoid(inta,intb):w(a),h(b){}virtualdoublearea()const{returnw*h*0.5;}};voidprintfArea(Shape&a){std::cout<3、l;}voidmain(){Circle*p(newCircle(2));printfArea(*p);Rectangle*pp(newRectangle(2,3));printfArea(*pp);Trapezoid*ppp(newTrapezoid(4,4));printfArea(*ppp);system("pause");}
3、l;}voidmain(){Circle*p(newCircle(2));printfArea(*p);Rectangle*pp(newRectangle(2,3));printfArea(*pp);Trapezoid*ppp(newTrapezoid(4,4));printfArea(*ppp);system("pause");}
此文档下载收益归作者所有