Decomposition of C ++ classes, the need for abstract classes and pure virtual functions

Source: Internet
Author: User

In order not to blur the concept, we will briefly describe it hereClass DecompositionIn the previous tutorial, we focused on class inheritance. The Inheritance feature is that the derived class inherits the features of the base class and expands the structure, the process of gradually decomposing different features in each derived class is actually the decomposition of classes.

The decomposition process I don't want to elaborate on the code too much here. I mean, the idea of gradual decomposition and gradual expansion depends on everyone's own thinking.

Taking the procedures of transportation tools for consideration, the vehicle and aircraft derived from transportation tools are described with more specific characteristics, its features are fuzzy and extensive. If it is meaningless to create a Transportation Tool Object, c ++ introducesAbstract classAbstract class constraints are defined by pure virtual functions.

A member function of a class is a pure virtual function. It makes sense for c ++ to know that the function is meaningless. Its function is only to reload and retain the position of the virtual function for the derived class.

The definition of pure virtual functions is to add the "= 0" mark after the declaration of the class member functions, once a pure virtual function is defined in a class, this class can no longer create such objects. We call this class an abstract class.

The sample code of the abstract class is as follows:


// Program Author: Guan Ning
// Site: www.cndev-lab.com
// All the manuscripts are copyrighted. If you want to reprint them, be sure to use the famous source and author.
# Include <iostream>
Using namespace std;

Class Vehicle
{
Public:
Vehicle (float speed, int total)
{
Vehicle: speed = speed;
Vehicle: total = total;
}
Virtual void ShowMember () = 0; // definition of pure virtual function
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 () // member function overload of the derived class
{
Cout <speed <"|" <total <"|" <aird <endl;
}
Protected:
Int aird;
};

Int main ()
{
// Vehicle a (); // error. The abstract class cannot create an object.
Car B (250,150, 4 );
B. ShowMember ();
System ("pause ");
}

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.