Question: virtual functions of the same name are the same parameter table do?
The difference between virtual functions and ordinary member functions
Virtual functions can be polymorphic, the other not
Virtual function is called in the constructor and destructor are not polymorphic
Derived class has not been initialized good
MyCompare()
qsort how indicates the sort relations
Virtual function table address !!
Virtual function
in the definition of class, in front of a virtual key member
function is virtual function.
class base {
virtual int get() };
Base :: GET int ()
{}
function declaration with the virtual keyword in the class definition only in the middle ,
do not write the function body.
Polymorphic form of a
pointer to the derived class can be assigned to the base class pointer .
by a base pointer calls the base class and a derived class of the same name virtual function when
1) If the pointer to a target base class , it is called
a base class virtual function of
2 a) If the pointer to the object of a derived class , It was then transferred
using a virtual function of the derived class .
This mechanism is called "
polymorphism."
class CBase { public : Virtual void SomeVirtualFunction () {} }; class the CDerived: public CBase { public : Virtual void SomeVirtualFunction () {} }; int main () { the CDerived ODerived; CBase * P = & ODerived; P -> SomeVirtualFunction (); // virtual function call which depends on what type of object p points CDerived class virtual function call return 0 ; }
Two polymorphic forms
object of a derived class can be assigned to the base class reference
reference to the base class of the same name by calling the base class and a derived class virtual function
1) If the reference is an object reference of a base class, it is
call is the base class virtual function
2) If the reference is an object references a derived class, it
is called a derived class virtual function.
This mechanism is also called "
polymorphism."
class CBase { public : Virtual void SomeVirtualFunction () {} }; class the CDerived: public CBase { public : Virtual void SomeVirtualFunction () {} }; int main () { the CDerived ODerived; CBase & = R & lt ODerived; r.SomeVirtualFunction () ; // which virtual function call depends on which type of the object referenced r return 0 ; }
Polymorphic role
Use polymorphism in object-oriented programming, it is possible to enhance
the program scalability , i.e., the program needs to be modified or added function
of time, and less need to change the added code.
/ * Geometry handler the Sample the Input: . 3 R & lt. 3. 5 C. 9 T. 5. 4. 3 the Sample the Output Triangle:. 6 the Rectangle: 15 Circle: 254.34 39 * / #include <the iostream> #include <stdlib.h> #include <Math. H> the using namespace STD; class CShape { public : virtual Double Area () = 0 ; // pure virtual function virtual void the PrintInfo () = 0 ; }; class that CRectangle: public CShape { public: int w, h; virtual double Area(); virtual void PrintInfo(); }; class CCircle :public CShape { public: int r; virtual double Area(); virtual void PrintInfo(); }; class CTriangle :public CShape { public: int a, b, c; virtual double Area(); virtual void PrintInfo(); }; double CRectangle::Area() { return w*h; } void CRectangle::PrintInfo() { cout << "rectanlgle:" << Area() << endl; } double CCircle::Area() { return 3.14*r*r; } void CCircle::PrintInfo() { cout << "circle:" << Area() << endl; } double CTriangle::Area() { double p = (a + b + c) / 2.0; return sqrt(p*(p - a)*(p - b)*(p - c)); } void CTriangle::PrintInfo() { cout << "triangle:" << Area() << endl; } CShape * pShapes[100]; // 基类 指针数组 int MyCompare(const void *s1, const void *s2); int MyCompare(const void* s1, const void *s2) { doubleA1, A2; CShape ** P1; // s1, S2 is void *, unwritable "" * s1 "to get the contents of both s1 CShape ** P2; P1 = (CShape **) s1; // s1, S2 pShapes point array element, the array element type is * CShape P2 = (CShape **) S2; // so p1, p2 are pointers to pointer type ** CShape A1 = (P1 *) -> Area (); // * P1 type is Cshape *, is the base class pointer therefore sentence polymorphic A2 = (P2 *) -> Area (); IF (A1 < A2) return - . 1 ; the else IF (A2 < A1) return . 1 ; the else return 0; } int main() { int i, n; CRectangle *pr; CCircle *pc; CTriangle *pt; cin >> n; for (i = 0; i < n; i++) { char c; cin >> c; switch (c) { case 'R': pr = new CRectangle(); cin >> pr->w >> pr->h; pShapes [I] = PR; // derived class, the base class from pointer BREAK ; Case ' C ' : PC = new new CCircle; CIN >> PC-> R & lt; pShapes [I] = PC; BREAK ; Case ' T ' : Pt = new new CTriangle (); CIN >> Pt-> Pt- >> A> B >> Pt-> C; pShapes [I] = Pt; BREAK ; } } qsort(pShapes, n, sizeof(CShape*), MyCompare); for (i = 0; i < n; i++) pShapes[i]->PrintInfo(); while (1); return 0; }
Storing various points derived object refers to an array of base class pointer
needle, and then through the array, each derived class objects can
do various operations, it is common practice
The constructor and destructor virtual function call
Virtual function is called in the constructor and destructor, not polymorphic. Code
can be determined when translated, is a function call their own class or base class defined
functions, and will not wait until run time decided to call their own or derived
function class.
Base and derived class virtual functions in the class with the same name as the parameter table, without
virtual virtual function automatically becomes
class myclass public: virtual void hello(){cout<<"hello from myclass"<<endl; }; virtual void bye(){cout<<"bye from myclass"<<endl;} }; class son:public myclass { void hello(){ cout<<"hello from son"<<endl;}; son(){ hello();} ~son(){bye(); }; class grandson:public son { void hello(){cout<<"hello from grandson"<<endl;}; void bye() { cout << "bye from grandson"<<endl;} grandson(){cout<<"constructing grandson"<<endl;}; ~grandson(){cout<<"destructing grandson"<<endl;}; }; int main(){ grandson gson; son *pson; pson=&gson; pson->hello(); 多态 return 0; Constructing Grandson Results:/ * } Hello from Son hello from grandson destructing grandson bye from myclass */
Access to virtual functions
class Base { private: virtual void fun2() { cout << "Base::fun2()" << endl; } }; class Derived:public Base { public: virtual void fun2() { cout << "Derived:fun2()" << endl; } }; Derived d; Base * pBase = & d; pBase-> fun2(); // 编译出错
Compile error because fun2 () is a private member's Base. Even at this time to actually run the call should be
Derived public member fun2 () does not work, because the grammar checker is not considered operational results.
If the Base in the private into public, even if fun2 Derived in () is private, the compiler can still pass
through, can also call the right Derived :: fun2 ().
#include<iostream> using namespace std; class Base { public: virtual void fun2() { cout << "base::fun2()" << endl; } }; class Derived :public Base { private: virtual void fun2() { cout << "Derived:fun2()" << endl; } }; int main() { D Derived; Base * pBase = & d; pBase -> fun2 (); // magic of, private can access! ! ! the while ( . 1 ); return 0 ; }