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