Posts by netizens who have made me understand Polymorphism

Source: Internet
Author: User

Abstract classes are usually defined by defining pure virtual functions in this class.
It is too common to use a private Type constructor. It is not an abstract class. Any desired object must be created in the heap.

Abstract classes are mainly used for interfaces. They can also be understood as a communication and interaction method of classes. The concept of interfaces is widely used in advanced applications such as COM. This is also the second threshold for C ++.
Class interfacehavefun
{
Required Ural havefun () = 0;
}
Interfacehavefun is a pure signed Ural class, and you cannot generate its objects. However, its pointer can point to its subclass.
That is to say, this is wrong.
Interfacehavefun TMP; // definition Error
Interfacehavefun * TMP = new interfacehavefun (); // definition Error
Class A: Public interfacehavefun
{
Havefun () // unknown Ural is not defined. It is also a signed Ural function.
{
STD: cout <"A have fun" <STD: Endl;
};
}

Class B: Public interfacehavefun
{
Havefun ()
{
STD: cout <"B have fun" <STD: Endl;
};
}
You can use the following interface:
Interfacehavefun * TMP = new ()
TMP-> havefun ()
Result:
A have fun

Interfacehavefun * TMP = new B ();
TMP-> havefun ();
B have fun

Don't underestimate this method. You can use it to write beautiful programs.

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.