[C ++ Primer] class Polymorphism

Source: Internet
Author: User

I. Overview
1) multiple implementation methods of interfaces are polymorphism.
2) polymorphism is a technique that allows you to set a parent object to be equal to one or more other sub-objects, the parent object can operate in different ways based on the features of the sub-objects assigned to it. To put it simply, you can assign a pointer of the subclass type to a pointer of the parent class. Polymorphism is implemented through Virtual functions in C ++.
3) key point: there is also a key to polymorphism: all objects are operated by pointers or references to the base class.

2. Example
1) Common Object operations
[Html]
# Include <iostream>
Using namespace std;
Class {
Public:
Void print ()
{
Cout <"This is A" <endl;
}
};
Class B: public {
Public:
Void print ()
{
Cout <"This is B" <endl;
}
};
Int main () {// for future convenience, the main () code is called main1
A;
B B;
A. print ();
B. print ();
}

Output: This is
This is B
2) perform multi-state object operations: operate subclass objects through parent objects
[Html]
# Include <iostream>
Using namespace std;
Class {
Public:
Void print ()
{
Cout <"This is A" <endl;
}
};
Class B: public {
Public:
Void print ()
{
Cout <"This is B" <endl;
}
};
Int main () {// operate subclass through parent class
A;
B B;
A * p1 = &;
A * p2 = & B;
P1-> print ();
P2-> print ();
}

Output: This is
This is
This is different from what we expected.

3) using polymorphism means adding the virtual
Call corresponding functions based on different class objects. This function is a virtual function.
[Html]
# Include <iostream>
Using namespace std;
Class {
Public:
Virtual void print ()
{
Cout <"This is A" <endl;
}
};
Class B: public {
Public:
Void print ()
{
Cout <"This is B" <endl;
}
};
Int main () {// operate subclass through parent class
A;
B B;
A * p1 = &;
A * p2 = & B;
P1-> print ();
P2-> print ();
}

Iii. virtual functions (advanced tutorial)
Void (A: * fun) (); // defines A function pointer.
A * p = new B;
Fun = & A: fun; // is the address that actually obtains the virtual function? In fact, the address of a piece of code that indirectly obtains the virtual function address
[Html]
# Include <iostream>
Using namespace std;
 
Class {
Public:
Virtual void fun ()
{
Cout <"A: fun" <endl;
}
Virtual void fun2 ()
{
Cout <"A: fun2" <endl;
}
};
Class B: public {
Public:
Void fun ()
{
Cout <"B: fun" <endl;
}
Void fun2 ()
{
Cout <"B: fun2" <endl;
}
};
Void CallVirtualFun (void * pThis, int index = 0)
{
Void (* funptr) (void *);
Long lVptrAddr;
Memcpy (& lVptrAddr, pThis, 4 );
Memcpy (& funptr, reinterpret_cast <long *> (lVptrAddr) + index, 4 );
Funptr (pThis );
}
Int main ()
{
 
Void (A: * fun) (); // defines A function pointer.
A * p = new B;
Fun = & A: fun; // is the address that actually obtains the virtual function? In fact, the address of a piece of code that indirectly obtains the virtual function address
(P-> * fun) (); // B: fun
Fun = & A: fun2;
(P-> * fun) (); // B: fun2

CallVirtualFun (p); // call the virtual function p-> fun ()
CallVirtualFun (p, 1); // call the virtual function p-> fun2 ()
Delete p;
System ("pause ");
Return 0;
}

Author: tianshuai11
 

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.