欢迎来到天天文库
浏览记录
ID:14155972
大小:59.00 KB
页数:13页
时间:2018-07-26
《面向对象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(){Dog
2、d;d.setAge(3);d.setWeight(30);cout<<"小狗:"<#includeusingnamespacestd;classRectangle{public:Rectangle(intxx1,intyy1,intxx2,i
3、ntyy2){x1=xx1;y1=yy1;x2=xx2;y2=yy2;}floatgetArea(){returnfabs(x2-x1)*fabs(y2-y1);}private:intx1,y1;intx2,y2;};voidmain(){Rectanglerec(0,0,10,20);cout<<"矩形面积:"<usingnamespacestd;c
4、lassRectangle{public:Rectangle(intl,intw){length=l;width=w;}floatgetArea(){returnlength*width;}private:intlength;intwidth;};voidmain(){Rectanglerec(10,20);cout<<"矩形面积:"<5、对象进行测试。#includeusingnamespacestd;constfloatPI=3.1415;classCircle{public:Circle(floatr){radius=r;}floatgetArea(){returnradius*PI*PI;}private:floatradius;};voidmain(){Circlec(5.5);cout<<"圆的面积:"<6、mplexc1(3,5);Complexc2=4.5;c1.add(c2);c1.show() ;//源程序如下:#includeusingnamespacestd;classComplex{public:Complex(floatr=0.0,floati=0.0){real=r;image=i;}voidadd(Complexb){real=real+b.real;image=image+b.image;}voidshow(){cout<7、usingn8、amespacestd;classCat{public:Cat(){numOfCats++;}~Cat(){numOfCats--;}staticintgetNumOfCats(){returnnumOfCats;}private:staticintnumOfCats;};intCat::numOfCats=0;voidmain(){cout<<"现在的Cat数量:"<
5、对象进行测试。#includeusingnamespacestd;constfloatPI=3.1415;classCircle{public:Circle(floatr){radius=r;}floatgetArea(){returnradius*PI*PI;}private:floatradius;};voidmain(){Circlec(5.5);cout<<"圆的面积:"<6、mplexc1(3,5);Complexc2=4.5;c1.add(c2);c1.show() ;//源程序如下:#includeusingnamespacestd;classComplex{public:Complex(floatr=0.0,floati=0.0){real=r;image=i;}voidadd(Complexb){real=real+b.real;image=image+b.image;}voidshow(){cout<7、usingn8、amespacestd;classCat{public:Cat(){numOfCats++;}~Cat(){numOfCats--;}staticintgetNumOfCats(){returnnumOfCats;}private:staticintnumOfCats;};intCat::numOfCats=0;voidmain(){cout<<"现在的Cat数量:"<
6、mplexc1(3,5);Complexc2=4.5;c1.add(c2);c1.show() ;//源程序如下:#includeusingnamespacestd;classComplex{public:Complex(floatr=0.0,floati=0.0){real=r;image=i;}voidadd(Complexb){real=real+b.real;image=image+b.image;}voidshow(){cout<7、usingn8、amespacestd;classCat{public:Cat(){numOfCats++;}~Cat(){numOfCats--;}staticintgetNumOfCats(){returnnumOfCats;}private:staticintnumOfCats;};intCat::numOfCats=0;voidmain(){cout<<"现在的Cat数量:"<
7、usingn
8、amespacestd;classCat{public:Cat(){numOfCats++;}~Cat(){numOfCats--;}staticintgetNumOfCats(){returnnumOfCats;}private:staticintnumOfCats;};intCat::numOfCats=0;voidmain(){cout<<"现在的Cat数量:"<
此文档下载收益归作者所有