您好,欢迎访问三七文档
上机五类与对象(一)1、运行书P106例4-1实例,体会类的使用。2.作业4-8,定义一个Dog类,包含了age,weight等属性,以及对这些属性操作的方法。实现并测试这个类。3.作业2-9,设计并测试一个名为Rectangle的矩形类,其属性为矩形的左下角与右下角两个点的坐标,根据坐标能计算矩形的面积。4、作业4-13:定义一个Circle类,由数据成员radius(半径),成员函数getArea(),计算圆的面积,构造一个Circle的对象进行测试。上机十类的继承(一)1.运行书P256,例7-1。体会公有继承的使用。2.运行书P259,例7-2。体会私有继承的使用。3.定义一个Document类,有数据成员name,从Document派生出Book类,增加数据成员pageCount。4.定义一个基类Base,有两个公有成员函数fn1(),fn2(),私有派生出Derived类,如何通过Derived类的对象调用基类的函数fn1()?Point.h#ifndefPOINT_H#definePOINT_HclassPoint{public:voidinitPoint(floatx=0,floaty=0){this-x=x;this-y=y;}voidmove(floatoffx,floatoffy){x+=offx;y+=offy;}floatgetX()const{returnx;}floatgetY()const{returny;}private:floatx,y;};#endif//POINT_HRectangle.h#ifndefRECTANGLE_H#defineRECTANGLE_H#includePoint.hclassRectangle:publicPoint{public:voidinitRectangle(floatx,floaty,floatw,floath){initPoint(x,y);this-w=w;this-h=h;}floatgetW()const{returnw;}floatgetH()const{returnh;}private:floatw,h;};#endif//RECTANGLE_HSj10.cpp#includeiostream#includecmath#includeRectangle.husingnamespacestd;intmain(){Rectanglerect;rect.initRectangle(2,3,20,10);rect.move(3,2);coutThedataofrect(x,y,w,h):endl;coutrect.getX(),rect.getY(),rect.getW(),rect.getH()endl;return0;}2.Point.h#ifndefPOINT_H#definePOINT_HclassPoint{public:voidinitPoint(floatx=0,floaty=0){this-x=x;this-y=y;}voidmove(floatoffx,floatoffy){x+=offx;y+=offy;}floatgetX()const{returnx;}floatgetY()const{returny;}private:floatx,y;};#endif//POINT_HRectangle.h#ifndefRECTANGLE_H#defineRECTANGLE_H#includePoint.hclassRectangle:privatePoint{public:voidinitRectangle(floatx,floaty,floatw,floath){initPoint(x,y);this-w=w;this-h=h;}voidmove(floatoffx,floatoffy){Point::move(offx,offy);}floatgetX()const{returnPoint::getX();}floatgetY()const{returnPoint::getY();}floatgetW()const{returnw;}floatgetH()const{returnh;}private:floatw,h;};#endif//RECTANGLE_HSj10_2.cpp#includeiostream#includecmath#includeRectangle.husingnamespacestd;intmain(){Rectanglerect;rect.initRectangle(2,3,20,10);rect.move(3,2);coutThedataofrect(x,y,w,h):endl;coutrect.getX(),rect.getY(),rect.getW(),rect.getH()endl;return0;}3.#includeiostream#includecstringusingnamespacestd;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;};intmain(){Bookbook(计算机网络,430);book.show();}4.#includeiostreamusingnamespacestd;classBaseClass{public:voidfn1(){coutbaseclassfn1endl;}voidfn2(){coutbaseclassfn2endl;}};classDerivedClass:publicBaseClass{public:voidfn1(){coutDerivedClassfn1endl;}public:voidfn2(){coutDerivedClassfn2endl;}};intmain(){BaseClassbase;base.fn1();base.fn2();DerivedClassd;d.fn1();d.fn2();}上机十三虚函数1、三维坐标类对象之间的直接运算。三维坐标类有数据成员x、y、z,对象间运算时要求通过函数成员实现“+”、前置“--”、“==”运算符的重载,通过友元函数实现后置“--”、“+=”、“”和“”运算符的重载,实现三维坐标类对象间的直接运算。main()完成对象的定义和有关运算符重载函数的测试。(提示:1)书中P313例8-3是对二维坐标的重载,首先阅读它,看看对编写三维坐标有何启发。2)插入符函数的一般形式:ostream&operator(ostream&函数的流,类名&对象名){…return函数的流;})2、由二维坐标点类作为基类派生出圆类;再由圆类作为基类派生出圆柱体类(通过虚函数完成输出)1.#includeiostreamusingnamespacestd;classPoint{public:Point(intxx=0,intyy=0,intzz=0):x(xx),y(yy),z(zz){}Pointoperator+(constPoint&p)const{returnPoint(x+p.x,y+p.y,z+p.z);}Pointoperator--(){--x;--y;--z;return*this;}booloperator==(constPoint&p1){if(x==p1.x&&y==p1.y&&z==p1.z)returntrue;elsereturnfalse;}friendPointoperator--(Pointp,int){returnPoint(--p.x,--p.y,--p.z);}friendvoidoperator+=(Point&p1,Point&p2){p1.x+=p2.x;p1.y+=p2.y;p1.z+=p2.z;}friendostream&operator(ostream&out,constPoint&p){out(p.x,p.y,p.z);returnout;}private:intx,y,z;};intmain(){Pointp1(5,3,2);Pointp2(1,5,4);Pointp3;coutp1:p1endl;coutp2:p2endl;p3=p1+p2;coutp3=p1+p2:p3endl;--p1;cout--p1:p1endl;if(p1==p2)coutYESendl;elsecoutNOendl;p1+=p2;coutp1+=p2:p1endl;return0;}2.#includeiostream//#includestring.husingnamespacestd;constdoublePI=3.14;classPoint{private:intx,y;//点的x和y坐标public:Point(intxx=0,intyy=0):x(xx),y(yy){}//构造函数//voidSetPoint(int,int);//设置坐标intGetX(){returnx;}//取x坐标intGetY(){returny;}//取y坐标virtualvoidshow(){cout(x,y)endl;}//输出点的坐标};//Point::Point(inta,intb){SetPoint(a,b);}//voidPoint::SetPoint(inta,intb){x=a;y=b;}//voidPoint::Print(){cout[x,y];}//从点类派生出圆类classCircle:publicPoint{private:intradius;public:Circle(intxx,intyy,intr):Point(xx,yy),radius(r){}//对数据成员赋值,并使用SetRadius和基类的PointintGetR(){returnradius;}doubleGetRadius();//取半径floatGetarea(){floatarea;area=static_castfloat(2*PI*radius);returnarea;}voidshow(){coutendl;cout圆心坐标:;Point::show();cout圆的半径:radiusendl;cout圆的面积:Getarea()endl;}};classCylinder:publicCircle{private:inthigh;public:Cylinder(intxx,intyy,intr,inth):Circle(xx,yy,r),high(h){}intGetH(){returnhigh;}intGetV(){floatv;v=static_castfloat(Circle::Getarea()*high);returnv;}voidshow(){coutendl;Circle::show();cout圆柱的高:highendl;cout圆柱的体积:GetV()endl;}};int
本文标题:c++实验
链接地址:https://www.777doc.com/doc-5462157 .html