C++ Foundation - Class Inheritance

Keywords: C++ C

1. Preface

Well, this series of blogs has become an embarrassing Reading Note for C++ Primer Plus.When using C language, code reuse is often achieved by adding library functions, but one drawback is that the original written code is not fully applicable to the current situation.Class inheritance in OOP design is more flexible than it is. It can add new data members and methods, modify the implementation details of inherited methods, and keep the original code.Get to the point.

Class Inheritance Example

The scenario is as follows: Now you need to record information about the members of table tennis, including their names and whether there are free tables.Some of the members have participated in the competition and their points in the competition need to be presented and recorded separately.Therefore, the class to which a member has participated in a competition is a derived class object that already belongs to the member's class.

Class declaration:

 1 #ifndef TABTENN_H_
 2 #define TABTENN_H_
 3 
 4 #include <string>
 5 
 6 using std::string;
 7 
 8 class TableTennisPlayer
 9 {
10 private:
11     string firstname;
12     string lastname;
13     bool hasTable;
14 
15 public:
16     TableTennisPlayer (const string& fn = "none",
17                        const string& ln = "none",bool ht = false);
18     void Name() const;
19     bool HasTable() const {return hasTable;};
20     void ResetTable(bool v) {hasTable = v;};
21 };
22 
23 //derived class
24 class RatedPlayer:public TableTennisPlayer //TableTennisPlayer Is a base class
25 {
26 private:
27     unsigned int rating;
28 public:
29     RatedPlayer(unsigned int r = 0,const string& fn = "none",const string& ln = "none",
30                 bool ht = false);//Default constructor
31     RatedPlayer(unsigned int r,const TableTennisPlayer&  tp);//Create Derived Class Object Constructor from Base Class Object
32     unsigned int Rating() const {return rating;}
33     void ResetRating (unsigned int r) {rating = r;}
34 };
35 
36 #endif
tabtenn.h

Class method definition:

 1 #include <iostream>
 2 #include "tabtenn.h"
 3 
 4 TableTennisPlayer::TableTennisPlayer (const string& fn,const string& ln,bool ht):
 5     firstname(fn),lastname(ln),hasTable(ht)//Member Initialization List
 6 {}
 7 
 8 void TableTennisPlayer::Name() const
 9 {
10     std::cout << lastname << ", " << firstname;
11 }
12 
13 //RatedPlayer methods
14 //Derived class constructor must call base class constructor
15 RatedPlayer::RatedPlayer(unsigned int r,const string& fn,const string& ln,bool ht):
16     TableTennisPlayer(fn,ln,ht)//Derived class constructor creates base class object first, using initialization list to complete
17 {
18     rating = r;
19 }
20 
21 RatedPlayer::RatedPlayer(unsigned int r,const TableTennisPlayer& tp):
22     TableTennisPlayer(tp),rating(r)
23 {}
tabtenn.cpp

The code above brings together the base class TableTennisPlayer and the derived class RatedPlayer.The RatedPlayer class declaration uses: public name_of_base_class to denote public derivation.Add your own constructors and additional member functions and methods to the declaration of derived classes.Derived class constructors are very interesting here.

A base class object must be created before creating a derived class object, because methods of derived classes cannot directly access private members of the base class.The problem arises: when a new derived class object is created, the derived class constructor is automatically called.How do I create a base class object by calling a base class constructor before calling a derived class constructor?(Good bypass) Here you need to use the constructor's unique syntax, Initialization List.The call to the base class constructor is completed in the initialization list before the program pointer points to the first line in the derived class constructor braces.To facilitate the direct selection of derived class objects from the base class objects (the base class objects contain derived class objects), use the second constructor to add score information directly to the base class objects.

3. Examples of applications

Application code:

 1 #include <iostream>
 2 #include "tabtenn.h"
 3 
 4 using std::endl;
 5 using std::cout;
 6 
 7 int main()
 8 {
 9     TableTennisPlayer player1("Tara","Boomdea",false);//Create base class object
10     RatedPlayer rplayer1(1140,"Mallory","Duck",true);//Create Derived Class Object
11     player1.Name();
12     if(player1.HasTable())
13         cout << ": has a table.\n";
14     else
15         cout << ": hasn't a table.\n";
16     rplayer1.Name();
17     if(rplayer1.HasTable())
18         cout << ": has a table.\n";
19     else
20         cout << ": hasn't a table.\n";
21 
22     //initialize RatedPlayer using TableTennisPlayer object
23     RatedPlayer rplayer2(1212,player1);
24     cout << "Name: ";
25     rplayer2.Name();
26     cout << ";Rating: " << rplayer2.Rating() << endl;
27     return 0;
28 }
usett.cpp

Player and rplayer represent base class objects and derived class objects, respectively.rplayer2 and player1 are actually the same people. Members who originally participated in the competition were selected from all the members.It's easy to apply the program, so there's not much to describe here.

Posted by Michan on Fri, 17 May 2019 21:58:34 -0700