C + + polymorphic

Source: Internet
Author: User


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.