How to inherit: Public private protected
/*
1. Private members cannot be inherited
2. Public inheritance remains unchanged
3. Private inheritance becomes private
4. Protect inheritance into protection
When you create a subclass object, call the constructor of the parent class to tone the constructor of the class first
When destroying an object, the destructor of the subclass is called first to re-tune the destructor of the parent class
Transformation of subclass and parent objects: Subclasses can be converted to parent classes, but parent classes cannot be converted to subclasses
Multiple inheritance: A class has multiple base classes
Two meanings in multiple inheritance (virtual inheritance)
Dynamic binding and static binding are relative to functions
Static binding: Determining the binding of the calling function in the compilation phase is called a static binding
In an object, a normal member function is not a memory, and a member variable is a memory-occupied
Dynamic binding: Determining the binding of the calling function at run time is called dynamic binding
virtual function: When a function is declared, it is added in front of virtual
The virtual function is the memory,
We find that the same type of pointer (or reference) does not perform the same function as the call: polymorphic
Polymorphic: Pointers (or references) of the same custom data type have different patterns
Polymorphic application: Saves the address of the subclass object with the parent pointer variable,
Then call the virtual function with the parent pointer variable
Overwrite (override)
*/
/*
Class Cparent
{
Public
Cparent () {
cout << "cparent::cparent ()" << Endl;
}
cparent (int a,int b) {
cout << "cparent::cparent (int a)" << Endl;
}
~cparent () {}
};
Class Cchild:p ublic cparent
{
Public
Cchild () {}
Cchild (int a): Cparent (a,10) {}
~cchild () {}
};*/
Class of the working person
Class CWorker
{
Public
CWorker () {}
~cworker () {}
virtual void work () {
cout << "Work" << Endl;
}
void Testfun () {
cout << "Cworker::testfun ()" << Endl;
}
int m_age;
};
Programmers
Class Cprogrammer:public CWorker
{
Public
Cprogrammer () {}
~cprogrammer () {}
void work () {
cout << "Write code" << Endl;
}
void Testfun () {
cout << "Cprogrammer::testfun ()" << Endl;
}
};
Personnel Specialist
Class Cpersonal:p ublic cworker
{
Public
Cpersonal () {
}
~cpersonal () {}
void work ()
{
This->a = 10;
cout << "recruitment" << Endl;
}
void Testfun () {
cout << "Cpersonal::testfun ()" << Endl;
}
int A;
};
Homework:
/*
Homework: The realization of animal species of birds dog snakes, each class to achieve a walking method walk (),
If it's a bird output: I'm flying, dog: I'm four feet away snakes: I'm using a slip.
Create three animals for each object in the same array, and then iterate over the arrays to get them to go.
*/
int _tmain (int argc, _tchar* argv[])
{
cpersonal* per = new Cpersonal;
Per->m_age = 100;
cworker* Pworker = per;
cout << "pworker->m_age:" << pworker->m_age << Endl;
cprogrammer* programmer = new Cprogrammer;
A: Output error value B: output 100
Per->work ();
Pworker->work ();
cworker* pWorker2 = programmer;
Pworker2->work ();
Pworker2->testfun ();
Programmer->testfun ();
/*int A;
cpersonal* pworker = NULL;
Pworker->work (); */
A. Error B. Normal operation
return 0;
}
C + + polymorphic