资源描述:
《实验5-多态性与虚函数-程序》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、实验5多态性和虚函数二.实验内容5・1.cpp#ineludeusingnamespacestd;〃点类〃构造函数〃析构函数〃虚函数〃虚函数〃坐标点#ineludeclassPoint{public:Point(int=0,int=0);virtual~Point(){}virtualvoidset();virtualvoiddisplay();protected:intx,y;};Point::Point(intx,inty){this->x=x;this->y=y;}voidPoint:
2、:set(){cout«HPleaseentertheinformat!onofpoint(x,y):H;cin>>x»y;cout«,,x=,,«x«"y="«y«endl;}〃圆类〃构造函数〃析构函数〃重新定义熄函数〃重新定义虚函数〃圆半径}voidPoint::display(){classCircle:publicPoint{public:Circle(int=O,int=0,int=0);~Circle(){}virtualvoidset();virtualvoiddisplay();protected:intrad
3、ius;};Circle::Circle(intx,inty,intradius):Point(x,y){this->radius=radius;}voidCircle::set(){cout«,,Pleaseentertheinformat!onofCircle(x,y,radius):cin»x»y»radius;}voidCircle::display!){cout«Hx=,,«x«"y=,,«y«nradius=H«radius«endl;}classCylinder:publicCircle〃圆柱体类{public:
4、Cylinder(int=O,int=0,int=0‘int=0);〃构造函数~Cylinder(){}〃析构函数virtualvoidset();〃重新定义虚函数virtualvoiddisplay));〃重新定义虚函数protected:intheight;〃圆柱体的高};Cylinder::Cylinder(intx,inty,intradius,intheight):Circle(x,y,radius){this->height=height;}voidCylinder::set(){cout«HPleaseenter
5、theinformationofCylinderfx,y,radius,height):n;cin>>x>>y»radius»height;}voidCylinder::display(){cout«l,x=,,«x«Hy=H«y;cout«Hradjus=,,«radius«Hheight=H«height«endl;}intmain(){Point*p;〃定义基类指针Pointpoint;〃定义基类对象p=&point;p->display();P->set();p->display();CirclecirclefO,0,
6、3);p二&circle;p->display();p->set();p->display();〃定义派生类对象Cylindercylinder(O,0,3,10);〃定义派生类对象p=&cylinder;p->display();p->set();p->display();return0;}5・2.cpp#includeusingnamespacestd;〃职工类#ineludeclassEmployee{public:Employee(char*,char*);virtual~Emplo
7、yee(){}voidsetName();char*getNamef);voidsetlD();char*getlD();virtualfloatpay()=O;virtualvoidprint()=O;protected:charname[20];〃构造函数〃析构函数〃修改姓名〃获取姓名〃修改工号〃获取工号〃计算职工的工资〃输出职工的信息〃姓名〃工号charid[20];Employee::Employee(char*name,char*id){strcpy(this->name,name);strcpy(this->id,
8、id);}voidEmployee::setName(){cout«l,Pleaseenteremployee'sname:H«endl;cin»name;}char*Employee::getName(){returnname;}voidEmployee::setlD(){