面向对象C++习题参考解答

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

4-8定义一个Dog类,包含age,weight等属性,以及对这些属性操作的方法。实现并测试这个类。#includeiostreamusingnamespacestd;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小狗:d.getAge()岁,重d.getWeight()斤。endl;}4-9设计并测试一个名为Rectangle的矩形类,其属性为矩形的左下角与右上角两个点的坐标,根据坐标能计算矩形的面积。#includeiostream#includemath.husingnamespacestd;classRectangle{public:Rectangle(intxx1,intyy1,intxx2,intyy2){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矩形面积:rec.getArea()endl;}4-11定义并实现一个矩形类,有长、宽两个属性,由成员函数计算矩形的面积。#includeiostreamusingnamespacestd;classRectangle{public:Rectangle(intl,intw){length=l;width=w;}floatgetArea(){returnlength*width;}private:intlength;intwidth;};voidmain(){Rectanglerec(10,20);cout矩形面积:rec.getArea()endl;}4-13定义一个Circle类,有数据成员radius(半径),成员函数getArea(),计算圆的面积,构造一个Circle的对象进行测试。#includeiostreamusingnamespacestd;constfloatPI=3.1415;classCircle{public:Circle(floatr){radius=r;}floatgetArea(){returnradius*PI*PI;}private:floatradius;};voidmain(){Circlec(5.5);cout圆的面积:c.getArea()endl;}4-20定义一个复数类Complex,使得下面的代码能够工作。Complexc1(3,5);Complexc2=4.5;c1.add(c2);c1.show();//源程序如下:#includeiostreamusingnamespacestd;classComplex{public:Complex(floatr=0.0,floati=0.0){real=r;image=i;}voidadd(Complexb){real=real+b.real;image=image+b.image;}voidshow(){coutreal+imageiendl;}private:floatreal;//实部floatimage;//虚部};voidmain(){Complexc1(3,5);Complexc2=4.5;//相当于Complexc2(4.5);c1.add(c2);c1.show();}5-7定义一个Cat类,拥有静态数据成员numOfCats,记录Cat的个体数目;静态成员函数getNumOfCats(),读取numOfCats。设计程序测试这个类,体会静态数据成员和静态成员函数的用法。#includeiostreamusingnamespacestd;classCat{public:Cat(){numOfCats++;}~Cat(){numOfCats--;}staticintgetNumOfCats(){returnnumOfCats;}private:staticintnumOfCats;};intCat::numOfCats=0;voidmain(){cout现在的Cat数量:Cat::getNumOfCats()endl;Cata;cout现在的Cat数量:a.getNumOfCats()endl;Catb;cout现在的Cat数量:b.getNumOfCats()endl;}5-14定义Boat与Car两个类,二者都有weight属性,定义二者的一个友元函数getTotalWeight(),计算二者的重量和。#includeiostreamusingnamespacestd;classCar;classBoat{public:Boat(floatw){weight=w;}friendfloatgetTotalWeight(Boatb,Carc);private:floatweight;};classCar{public:Car(floatw){weight=w;}friendfloatgetTotalWeight(Boatb,Carc);private:floatweight;};floatgetTotalWeight(Boatb,Carc){returnb.weight+c.weight;}voidmain(){Boatboat(3500);Carcar(1000);cout船和汽车共重getTotalWeight(boat,car)公斤。endl;}7-5定义一个基类Shape,在此基础上派生出Rectangle和Circle,二者都有getArea()函数计算对象的面积。使用Rectangle类创建一个派生类Square。#includeiostreamusingnamespacestd;constfloatPI=3.14;classShape{public:Shape(floata,floatb=0.0){this-a=a;this-b=b;}protected:floata,b;};classRectangle:publicShape{public:Rectangle(floatl,floatw):Shape(l,w){}floatgetArea(){returna*b;}};classCircle:publicShape{public:Circle(floatr):Shape(r){}floatgetArea(){returna*PI*PI;}};classSquare:publicRectangle{public:Square(floatl):Rectangle(l,l){}floatgetArea(){returna*a;}};voidmain(){Rectangler(10,20);Circlec(5);Squares(10);cout矩形的面积:r.getArea()endl;cout圆的面积:c.getArea()endl;cout正方形的面积:s.getArea()endl;}7-6定义一个哺乳动物类Mammal,再由此派生出狗类Dog,定义一个Dog类的对象,观察基类与派生类的构造函数和析构函数的调用顺序。#includeiostreamusingnamespacestd;classMammal{public:Mammal(){coutConstructingMammal.endl;}~Mammal(){coutDesstructingMammal.endl;}};classDog:publicMammal{public:Dog(){coutConstructingDog.endl;}~Dog(){coutDesstructingDog.endl;}};voidmain(){Dogd;}7-8定义一个Document类,有数据成员name,从Document派生出Book类,增加数据成员pageCount。#includeiostreamusingnamespacestd;classDocument{public:Document(char*n){strcpy(name,n);}voidshow(){coutname;}private:charname[50];};classBook:publicDocument{public:Book(char*n,intp):Document(n),pageCount(p){}voidshow(){cout书名:;Document::show();coutendl页数:pageCountendl;}private:intpageCount;};voidmain(){Bookbook(C++语言程序设计,529);book.show();}7-10定义一个Object类,有数据成员weight及相应的操作函数,由此派生出Box类,增加数据成员height和width及相应的操作函数,声明一个Box对象,观察构造函数与析构函数的调用顺序。#includeiostreamusingnamespacestd;classObject{public:Object(){coutConstructingObject.endl;}~Object(){coutDestructingObject.endl;}voidsetWeight(intw){weight=w;}intgetWeight(){returnweight;}private:intweight;};classBox:publicObject{public:Box(){coutConstructingBox.endl;}~Box(){coutDestructingBox.endl;}voidsetHeight(inth){height=h;}intgetHeight(){returnheight;}voidsetWidth(intw){width=w;}intgetWidth(){returnwidth;}private:intheight;intwidth;};voidmain(){Boxbox;box.setHeight(5);box.setWidth(10);box.setWeight(8);cout盒子:高box.getHeight(),宽box.getWidth(),重box.getWeight()endl;}8-4#includeiostreamusingnamespacestd;classCounter{public:Counter(intii=0){i=ii;}voidprint(){couti=iendl;}Counteroperator+(inta){Countertemp;temp.i=i+a;returntemp;}private:inti;};voidmain(){Counterc;c=c+3;c.print();c=c+5;c.print();}8-5#includeiostreamusingnamespacestd;classMammal{public:virtualvoidspeak(){coutMammalSpeak!endl;}};classDog:publicMa

1 / 13
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功