First, pure virtual functions and abstract classes in C + +:
1. A class containing one or more pure virtual functions becomes an abstract class, note that this is a pure virtual function, not a virtual function.
2. If a subclass inherits an abstract class, then the pure virtual function in the parent class must be implemented, otherwise the class is also an abstract class.
3. If a class contains virtual functions, the destructor for that class must be declared as a virtual function.
4. Declaration of virtual function and pure virtual function:
virtual void draw ();//virtual function
virtual void draw () = 0;//pure virtual function
5. Two kinds of polymorphism are supported in C + +:
Compile-time polymorphism: implemented by overloaded functions.
Run-time polymorphism: implemented by virtual functions.
Second, Java abstract function and abstract class:
1. Classes containing abstract methods become abstract classes, but abstract classes do not necessarily contain abstract methods. An abstract class cannot create an instance.
2. The abstract method must be decorated with the abstract keyword.
3. If a non-abstract subclass inherits an abstract class, you must override all the abstract methods in the parent class.
C + + interface: In a class, except for constructors and destructors, only pure virtual functions, the class becomes an interface.
Four, the code implementation:
1#include <iostream>2 3 using namespacestd;4 5 //classes that contain one or more pure virtual functions become abstract classes, note that this refers to pure virtual functions instead of virtual functions6 //definition of pure virtual function: virtual void draw () = 0;7 //If you inherit an abstract class, you must implement all of the pure virtual functions in the parent class, or the subclass is also an abstract class8 //If a class contains virtual functions, you must declare the destructor of this class as a virtual function. 9 classShapeTen { One Public: A Shape () { - } - Virtual~Shape () { the } - Virtual DoubleGetarea () =0;//Pure virtual function - Virtual DoubleGETP () =0; - Virtual voidDraw () =0; + }; - + classCircle: PublicShape A { at Public: -Circle (Doubler): Itsradius (r) { - } - Virtual~Circle () { - - } in DoubleGetarea () { - return 3.14* Itsradius *Itsradius; to } + DoubleGetp () { - return 2*3.14*Itsradius; the } * voidDraw () { $cout<<"Draw Circle ..."<<Endl;Panax Notoginseng } - Private: the DoubleItsradius; + } ; A the classRectangle: PublicShape + { - Public: $Rectangle (intWinth): Wid (W), Hei (h) { $ } - Virtual~Rectangle () { - } the DoubleGetarea () - {Wuyi returnWID *Hei; the } - DoubleGetp () Wu { - return 2* (WID +hei); About } $ voidDraw () - { - for(inti =0; I <5; i++) - { A for(intj =0; J <4; j + +) + { thecout<<"X"; - } $cout<<Endl; the } the } the Private: the intwid; - intHei; in }; the intMain () the { About intNumber ; the BOOLFlag =false; the theShape *shape; + while(Flag = =false) - { thecout<<"Please enter a number: (1) Round (2) Rectangle (0) Exit:";BayiCin>>Number ; the Switch(number) the { - Case 1: -Shape =NewCircle (10.0); the Break; the Case 2: theShape =NewRectangle ( -, the); the Break; - Case 0: theFlag =true; the Break; the }94 the if(Flag = =false) the { theShape->Draw ();98 Deleteshape; About } - } 101 return 0;102}
Subsequent updates ....
C + + Learning Basics 12--pure virtual function and abstract class