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

Source: Internet
Author: User

To solve the problem that the object type cannot be correctly identified, c ++ providesPolymorphism

(Polymorphism)
To solve the problem. For example program 1, this method can determine which overloaded member function is called during compilation.
CalledFirst
Joint Compilation


(Early binding), while the system can determine which overloaded member function to call based on its type at runtime.
, CalledPolymorphism

, OrLagging Compilation

(Late
Binding). The following figure shows the Example 3: delayed Association. Delayed Association is the solution to the problem of polymorphism.

The Code is as follows:

// Example 3

# Include <iostream
>
Using
Namespace
Std;

Class
Vehicle

{
Public
:
Vehicle (float
Speed, int
Total)
{

Vehicle: speed =
Speed;
Vehicle: total =
Total;
}
Virtual
Void
ShowMember () // virtual function

{

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;
}
Virtual
Void
ShowMember () // virtual function, in the derived class, because
The inherited link. The virtual field can also be left empty.

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

}
Public
:
Int
Aird;
};

Void
Test (Vehicle & temp)

{
Temp. ShowMember ();
}

Int
Main
()
{
Vehicle a (120,4 );
Car B (180,110, 4 );

Test ();
Test (B );
Cin
. Get ();

}

Definition of virtual functions on which polymorphism features work
AddVirtual
Off
The member function becomes a virtual function. From the result of running the code in the preceding example, the system successfully identifies the real type of the object, the respective overload member functions are successfully called.

 
The polymorphism feature saves programmers the consideration of details, improves development efficiency, and greatly simplifies code. Of course, the definition of virtual functions is also flawed, some data storage and execution commands are added due to polymorphism.
Sales, so it is best not to use polymorphism.

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.