Inheritance characteristics of classes in C + +

Source: Internet
Author: User
Tags inheritance

The whole C + + programming is done around the object-oriented way, the inheritance of the class is a very very important mechanism of C + +, the inheritance attribute can make a new class get the operation and data structure of its parent class, the programmer just adds the component in the new class that is not in the original class.

It can be said that the content of this chapter is the key to C + + object-oriented programming.

Let's briefly say the concept of inheritance, first look at the following figure:

The image above is an abstract description of the attribute inheritance table

Transport is a base class (also known as the parent), usually all vehicles have the common characteristics of speed and rated the number of manned, but in accordance with the normal life, we continue to classify the transport, we will think of cars and aircraft, and so on, Cars and flying also have the same speed and rated the number of manned passengers such characteristics, and these characteristics are common to all vehicles, then when building cars and aircraft classes we do not need to define the existing data members of the base class, but only need to describe the car class and aircraft specific characteristics can be, The characteristics of aircraft and automobiles are based on the original characteristics of the vehicle type, then the aircraft class and car class is the vehicle class (also known as subclasses). And so on, as the layers increment, the notion that the seed class obtains the parent attribute is inherited.

Here's what we'll see in the diagram above, like the following code:

#include <iostream>
using namespace Std;
Class Vehicle
{
Public
void Editsc (float speed,int total);
Protected
Float speed;//Speed
int total;//Maximum manned capacity
};
void Vehicle::editsc (float speed,int total)
{
Vehicle::speed = speed;
Vehicle::total = total;
}
Class Car:public Vehicle//car classes inherit the attributes of Vehicle, the car class is a Vehicle derived class
{
Public
Car ()
{
Aird=0;
}
Protected
int aird;//Displacement
};
Class Plane:public Vehicle
{
Protected
Float wingspan;//Wing Exhibition
};
void Main ()
{
Car A;
A.EDITSC (150,4);
Cin.get ();
}

The definition of a derived class can be defined by adding a colon public null Geggaky class name after the class name, such as Class Car:public Vehicle in the code above.

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.