Role of c ++, virtual function, on receiving an essay

And compared to the previous example, only in the present embodiment display () function is declared before adding a virtualkeyword, the member function declarations for virtual function (Virtual Function), so that you can call the member functions of the class Teacher by pointer p, operating results also proved this point (Zhaohong Jia has been a teacher, is no longer a loafer).


With virtual functions, base class pointer on the use of members of the base class (including the member variables and member functions) point to a base class object, use the pointing member of the derived class derived object. In other words, the base class may be a pointer to work according to the way the base class, or may be derived class way to do things, it has a variety of forms, or that manifested in many ways, we call this phenomenon polymorphism ( Polymorphism) .

The above code, also p->display();this statement, when p point to different objects, it performs the operation is not the same. The same statement can perform different operations, it seems there are different manifestations, this is polymorphism.

Polymorphism is one of the main features of object-oriented programming, C ++ virtual functions only useful configuration is polymorphic.

C ++ provides polymorphic object: can be "full" refers to the access member variables and member functions for all derived classes (including direct and indirect derivative derived) by the base class, in particular member function. If there is no polymorphism, we can only access the member variables.

We have said before, according to the type of pointer (which class defined by pointers) to determine which class member function is called when an ordinary member function calls through pointers, but can be found through the analysis of this section, this argument does not apply to the virtual function, virtual function is a pointer to the call, a pointer to an object of which class virtual function which is called class.

But then again, the memory model of the object is very clean, no information of any member functions, the compiler what member functions in accordance find it?

 

 

 

 

 

 

 

 

 

 

#include <the iostream>
the using namespace STD;
// base class People
class People {
public:
People (char * name, int Age);
Virtual void the display (); // virtual function declared as
protected:
char * m_Name;
int m_age;
};
People :: People (char * name, int Age): m_Name (name), m_age (Age) {}
void People :: Run the display () {
cout << << m_Name "this year" << m_age << "years old a, a vagrant "<< endl;.
}
// derived class Teacher
class Teacher: public People {
public:
Teacher (char * name, int Age, the salary int);
virtual void the display (); // declared virtual
Private:
int m_salary;
};
Teacher :: Teacher (char * name, int Age, the salary int): People (name, Age),m_salary(salary){}
Teacher :: Run the display void () {
cout << << m_Name "this year" << m_age << "years old, was a teacher, there are monthly" << m_salary << << endl "income yuan.";
}
int main () {
People People new new = P * ( "Wang Zhigang", 23 is);
P -> the display ();
P = new new Teacher ( "Zhaohong Jia", 45, 8200);
P -> the display ();
return 0;
}

Guess you like

Origin www.cnblogs.com/qianrushi1/p/11564396.html