C++程序设计实践指导书7(答案).pptxVIP

  • 6
  • 0
  • 约2.2千字
  • 约 9页
  • 2021-01-11 发布于广东
  • 举报
C++程序设计实践上机指导书;2;3;} void show(){ coutID: IDendl; coutname: nameendl;;Course::show(); coutScore: scoreendl; } private: float score; }; void main(){ Student s1(0001,林维,S,21); s1.show(); coutendl; Course c1(1001,高级语言程序设计,64); c1.show(); coutendl; SelCourse sc1(9901,张帅,M,22,1002,面向对象程序设计C++,56,89); sc1.show(); } ;Point(int xx=0, int yy=0){ x=xx; y=yy; } int getX() { return x; } int getY() { return y; } void show() {cout(x,y)endl;} protected: int x,y; }; class Circle:virtual public Point{ public: Circle(int xx=0,int yy=0,float r=1):Point(xx,yy){ radius=r; } int getR() {return radius;} void show(){ cout圆心坐标:; Point::show(); cout圆半径:radiusendl; } protected: float radius; }; class cylinder:public Circle{ public: cylinder(int xx=0,int yy=0,float r=1,float h=2):Point(xx,yy),Circle(r){ height=h; } int getH() {return height;} void show(){ Circle::show(); cout圆柱体高度:heightendl; } private: float height; }; int main(){ Point p1(1,2); p1.show();;coutendl; Circle c1(2,2,3); c1.show(); coutendl; cylinder cy1; cy1.show(); system(pause); return 0; } 不使用虚基类。如果circle类继承point,cylinder继承circle,并且在cylinder类中 Point(xx,yy),Circle(r)这样在构造函数中赋值就会报错“错误 1 error C2614: “cylinder”: 非法的成员初始化:“Point”不是基或成员”。修改办法一,将point 设置为虚基类,修改办法二,在cylinder构造函数中通过Circle(xx,yy,r)传值给point。 #include iostream #include string using namespace std; class Point { public: Point(int xx=0, int yy=0){ x=xx; y=yy; } int getX() { return x; } int getY() { return y; } void show() {cout(x,y)endl;} protected: int x,y; }; class Circle:public Point{ public: ;protected: float radius; }; class cylinder:public Circle{ public: cylinder(int xx=0,int yy=0,float r=1,float h=2):Circle(xx,yy,r){ height=h; } int getH() {return height;} void show(){ Circle::show(); cout圆柱体高度:heightendl; } private: float height; }; int main(){ Point p1(1,2); p1.show(); coutendl; Circle c1(2,2,3); c1.show(); coutendl;

文档评论(0)

1亿VIP精品文档

相关文档