81次课程8 1不含录音.pdfVIP

  • 0
  • 0
  • 约6.9千字
  • 约 17页
  • 2023-10-22 发布于北京
  • 举报
15 Polymorphism Virtual Functions 1. Polymorphism virtual functions 2. How C++ implements late binding 3. Virtual function constructor/destructor 4. Pure virtual functions and classes 5. Summary 15.4 Pure virtual functions and classes Sometimes you want the base class to present only an interface for its derived classes. You don’twant any o actually create an object of the base class. An class at least has one pure virtual function.A pure class has nothing but pure virtual functions. You can not create an object of the class. class Point // class { public: void show() {cout“ class”endl; } virtual void Set()=0; // ! virtual void Set() { }; virtual void Draw()=0; }; // Point obj ; //error 15.4 Pure virtual functions and classes ◼ When an class is inherited, all pure virtual functions should be implemented, or the inherited class es as well. ◼ By making a class , you can ensure that a pointer or reference is always used during upcasting to that class, to prevent object slicing. class Point { // class int x0,y0; public: Point(int i=0,int j=0) {x0=i;y0=j;} virtual void Set()=0; virtual void Draw()=0; }; class Line: public Point { int x1,y1; public: Line(int i=0,int j=0,int m=0,int n=0): Point(i,j) { x1=m; y1=n; } virtual void Set() {coutLine::Set() called. endl;} virtual void Draw() {coutLine::Draw() called. endl;} }; 15.4 Pure virtual functions and classes // class class Ellipse: public Point { int x2,y

您可能关注的文档

文档评论(0)

1亿VIP精品文档

相关文档