欢迎来到天天文库
浏览记录
ID:22146713
大小:59.01 KB
页数:13页
时间:2018-10-27
《面向对象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);}
3、private: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;}
4、;voidmain(){Rectanglerec(10,20);cout<<"矩形面积:"<usingnamespacestd;constfloatPI=3.1415;classCircle{public:Circle(floatr){radius=r;}floatgetArea(){returnradius*PI*PI;}private:floatradius;};
5、voidmain(){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=imag
6、e+b.image;}voidshow(){cout<7、am>usingnamespacestd;classCat{public:Cat(){numOfCats++;}~Cat(){numOfCats--;}staticintgetNumOfCats(){returnnumOfCats;}private:staticintnumOfCats;};intCat::numOfCats=0;voidmain(){cout<<"现在的Cat数量:"<
7、am>usingnamespacestd;classCat{public:Cat(){numOfCats++;}~Cat(){numOfCats--;}staticintgetNumOfCats(){returnnumOfCats;}private:staticintnumOfCats;};intCat::numOfCats=0;voidmain(){cout<<"现在的Cat数量:"<
此文档下载收益归作者所有