The decomposition of class, the need of abstract class and pure virtual function

Source: Internet
Author: User
Tags abstract inheritance

In order not to blur the concept here we will simply elaborate on the decomposition of the class, in the previous tutorial we focused on the inheritance of the class, which is characterized by the inheritance of the attributes of the base class, the expansion of the structure, the gradual expansion of the process of decomposing the different characteristics of each other in the derived classes is actually the decomposition of the class.

Decomposition process I do not want to take the code to do too much elaboration analysis, meaning that, for the gradual decomposition of the idea of gradual expansion on the thought of everyone on their own.

Think of the procedure in front of the vehicle class, vehicles derived from vehicles, aircraft, are more specific characteristics of the description of the class, and for the transport of a base class, its characteristics are vague, extensive, if the establishment of a vehicle class objects and has no practical significance, In order to constrain the class which is not necessary to establish the object, C + + introduces the attribute of abstract class, and the constraint control of abstract class comes from the definition of pure virtual function.

Life a member function of a class is a pure virtual function that makes C + + aware that the function is meaningless, and its role is simply to hold the location of the virtual function overload for the derived class.

The definition of a pure virtual function is to add the "= 0" tag after the declaration of the member function of the class, and once the class has the definition of a pure virtual function, it is no longer possible to create objects of this class, and we call this class an abstract class.

The sample code for the abstract class is as follows:

Program Author: Junning
Site: www.cndev-lab.com
All manuscripts have copyright, if you want to reprint, please be sure to indicate the 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 ()//derived class member function overload
{
cout<<speed<< "|" <<total<< "|" <<aird<<endl;
}
Protected
int Aird;
};
int main ()
{
Vehicle A (100,4);//error, abstract class cannot create 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.