C + +: Design of classes ———— public inheritance

Source: Internet
Author: User

1. Concept of inheritance

Inheritance is an attribute of C + +, meaning that a new class is derived from an existing class, whereas a derived class inherits some of the private members and member functions of the base class.

Public inheritance is the most common form of inheritance.

2. Characteristics of public succession

The object of the derived class inherits the private member of the base class, but it cannot be accessed directly (derived classes inherit the implementation of the base class)

The object of a derived class can use the member method of the base class (the derived class inherits the interface of the base class).

The use of inheritance must be based on these two characteristics of inheritance, to cite a simple example.

Suppose there is a base class player that describes an athlete's name attribute.

classplayer{Private: std::stringFirstName; STD::stringLastName;  Public: Player (ConstSTD::string& fname ="None",ConstSTD::string& lname ="None"): FirstName (fname), LastName (lname) {}; voidName ()Const; ~Player ();};

We want to design a new class, in addition to the name of the athlete in this class to add new attributes, the athlete's rank.

Since this new analogy has only a few rating properties and some member methods, we can use the player class to derive new classes and improve the reusability of the code.

classRatedplayer: Publicplayer{Private: Unsignedintrating;  Public: Ratedplayer (ConstUnsignedintR =0,ConstSTD::string& fname ="None",ConstSTD::string& lname ="None"); Ratedplayer (unsignedintRConstPlayer &PL); voidReset_rating (unsignedintR =0) {rating =R;};};

The syntax for public inheritance is simple, with a colon behind the derived class (Ratedplayer) (:) followed by the keyword public+ base class name (Player)

3. What to add in the inherited attribute

Derived classes require their own constructors (very important).

Derived classes can add new private members and member functions to suit their needs.

The constructors of derived classes are particularly important, and understanding the constructors of derived classes can be of great help in understanding the concept of inheritance.

The following is a description of the function body of the constructor of the derived class.

Ratedplayer::ratedplayer (constintCONST std::stringconst std::  String & lname): Player (fname, lname), Rating (R) {};

As we can see, when we construct a derived class, we use the constructor of the base class, so what is this?

First, the derived class cannot directly access the private members of the base class, and must pass the method of the base class, so that FirstName, LastName in Ratedplayer can only be initialized by the constructor of the base class.

Second, when you create a derived class object, the program should first create the base class object, because there is no derived class for the base class?

Therefore, the player (Fname,lname) in the constructor in Ratedplayer can be understood to construct the base class object first, and on this basis add the private member initialization of the derived class object.

So what happens if we don't call the constructor of the base class?

That

Ratedplayer::ratedplayer (constintCONST std::stringconst std::  String & lname) {    = r;}

What's going to happen?

Note that the compiler always follows the principle of creating a derived class object after the base class object was first created , so the above code is equivalent to:

Ratedplayer::ratedplayer (constintCONST std::stringconst std::  String & Lanme): Player () {    = r;}  

That is, the default constructor for the base class object is called , and if there is no default constructor, an error occurs.

Unless you want to use the default base class constructor, you should make a display call.

The main points of public succession are as follows:

• When creating a derived class object, you should first create a base class object.

• The derived class constructor should pass the base class information (that is, the private member in the base class) to the constructor of the base class in a way that the member initializes the list.

• Derived class constructors should initialize the new data members of the derived class.

It is also emphasized that this is the special relationship between the derived class and the base class.

There are three important relationships in addition to the way that derived classes can use the base class.

• A base class pointer can point to a derived class object without a display conversion

• A base class reference can point to a derived class reference without a display conversion

• Similarly, you can assign a derived class object to a base class object without casting

#include"Player.h"#include<iostream>intMain () {Ratedplayer player ( -,"Bob","Brown"); Player& pl =player; Player* p = &player;    Pl.name (); P-name ();    GetChar (); return 0;}

These three features are very good, we can only build a pointer array of base class type to store the derived class and base class object batch processing .

In fact, public inheritance establishes a is-a relationship, in which a derived class is a special case of a base class.

C + +: Design of classes ———— public inheritance

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.