Polymorphism of classes and use of virtual functions in C ++ (1)

Source: Internet
Author: User

Class polymorphism is the most important feature that supports object-oriented languages. People who have experience in non-object-oriented language development usually refer
The content may be hard to get used to, because many people mistakenly think that the encapsulated language of the Support class supports object-oriented, but it is not,Visualized BASIC 6.0

It is a typical non-object-oriented development language, but it is indeed a Support class. The support class cannot be described as a language that supports object-oriented and can solve polymorphism problems, is a language that truly supports object-oriented development.
Be sure to remind readers of other non-object-oriented language basics!

The concept of polymorphism is a bit vague. If you want to use clear language descriptions at the beginning
It seems unrealistic for the reader to understand it, so let's look at the following code first:

//
Routine 1

# Include <iostream
>

Using
Namespace
STD;


Class
Vehicle
{
Public
:
Vehicle (float
Speed, int
Total)
{
Vehicle: speed = speed;

Vehicle: total = total;
}
Void
ShowMember ()
{
Cout
<Speed <"|" <total <endl;

}
Protected
:
Float
Speed;
Int
Total;

};
Class
Car: public
Vehicle

{
Public
:
Car (int
Aird, float
Speed, int
Total): Vehicle (speed, total)
{
Car: aird = aird;

}
Void
ShowMember ()
{

Cout
<Speed <"|" <total <"|" <aird <endl;

}
Protected
:
Int
Aird;
};

Void
Main
()
{
Vehicle a (120,4 );
A. ShowMember ();

Car B (180,110, 4 );
B. ShowMember ();
Cin
. Get ();
}

In c ++, the derived class is allowed to overload the base class member function.
Specifically, when calling the member functions of different classes of objects, the system knows how to find members with the same name of the class,. showMember ();, that is, call
Is Vehicle: ShowMember (), B. ShowMember ();, that is, the Car: ShowMemeber (); is called ();.

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.