欢迎来到天天文库
浏览记录
ID:24753427
大小:59.00 KB
页数:13页
时间:2018-11-16
《面向对象c++习题参考解答》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、4-8定义一个Dog类,包含age,weight等属性,以及对这些属性操作的方法。实现并测试这个类。#includeusingnamespacestd;classDog{public:voidsetAge(inta){age=a;}intgetAge(){returnage;}voidsetWeight(floatw){weight=w;}floatgetWeight(){returnweight;}private:intage;floatweight;};voidmain(){Dogd;d.setAge(3);d.setWeight(30);cout<<"小狗:"<
2、#includeusingnamespacestd;classRectangle{public:Rectangle(intxx1,intyy1,intxx2,intyy2){x1=xx1;y1=yy1;x2=xx2;y2=yy2;}floatgetArea(){returnfabs(x2-x1)*fabs(y2-y1);}pr
3、ivate:intx1,y1;intx2,y2;};voidmain(){Rectanglerec(0,0,10,20);cout<<"矩形面积:"<usingnamespacestd;classRectangle{public:Rectangle(intl,intw){length=l;width=w;}floatgetArea(){returnlength*width;}private:intlength;intwidth;};vo
4、idmain(){Rectanglerec(10,20);cout<<"矩形面积:"<usingnamespacestd;constfloatPI=3.1415;classCircle{public:Circle(floatr){radius=r;}floatgetArea(){returnradius*PI*PI;}private:floatradius;};void
5、main(){Circlec(5.5);cout<<"圆的面积:"<usingnamespacestd;classComplex{public:Complex(floatr=0.0,floati=0.0){real=r;image=i;}voidadd(Complexb){real=real+b.real;image=image+b.i
6、mage;}voidshow(){cout<usi
7、ngnamespacestd;classCat{public:Cat(){numOfCats++;}~Cat(){numOfCats--;}staticintgetNumOfCats(){returnnumOfCats;}private:staticintnumOfCats;};intCat::numOfCats=0;voidmain(){cout<<"现在的Cat数量:"<
此文档下载收益归作者所有