The dynamic polymorphism and static polymorphism

Source: Internet
Author: User

We all know that object-oriented programming has three features: encapsulation, inheritance, polymorphism. Polymorphism plays an important role in object-oriented programming.

How is this polymorphism achieved? There is usually a base class that contains some specific interfaces that are overloaded by subclasses of the class, using pointers to the base class, or referencing objects that point to subclasses, so that the function of calling the corresponding function of the subclass can be implemented; This phenomenon is called polymorphism.

What are the characteristics of these polymorphic states?

1. It is bound. There is a base class, where some interfaces exist, and subclasses must overload these interfaces, which is bound.

2. It is dynamic. These function invocation mechanisms are dynamic, so that the execution period can be determined.

Polymorphic we can give it a new name, a polymorphic state, with the above two properties. So is there a polymorphic called static polymorphism? It does have a static polymorphism.

I think we must have guessed the characteristics of static polymorphism:

1. It is unbound: because the template mechanism is used, there is no so-called base class, so there is no need to bind.

2. It is static: because the virtual function mechanism is not adopted, all calls can be determined at compile time, so they are static.

How to achieve static polymorphism?

Class Circle
{
Public:
Void Draw() const
{
Cout<<” Circle  Draw”<<endl;
}
}
Class Line
{
Public:
Void Draw() const
{
Cout<<” Line  Draw”<<endl;
}
}
Template<typename T>
Void DrawGeoObj(T const & obj)
{
Obj.Draw();
}
Int main(int *argc, char *argv[])
{
Circle c1;
Line l1;
DrawGeoObj(c1);// A
DrawGeoObj(l1);// B
}

At this point A, b two sentences can be considered as static polymorphism.

At this point you might think that this is nothing polymorphic, just a template function. Well, let me give you one more example.

We know that the bridge pattern in bridge mode (see design pattern) is generally implemented in the following way:

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.