Detail C++ foundation Class inheritance

  • 2020-06-15 10:04:48
  • OfStack

1. Introduction

Well, this blog series has become an awkward reading note for C++ Primer Plus. When using C, code reuse is mostly achieved by adding library functions, but one disadvantage is that the original code is not fully applicable to the current situation. Class inheritance in OOP is more flexible, allowing you to add new data members and methods and modify the implementation details of inherited methods, while retaining the original code. Let's get down to business.

2. Example of class inheritance

The scenario is as follows: Information about table tennis players is now needed, including names and availability of tables. There are 1 part of the members who have participated in the competition, and it is necessary to put up this part separately and record their scores in the competition. Therefore, the class to which a member has participated is a derived class object of the class to which the member belongs.

Class declaration:


#ifndef TABTENN_H_
#define TABTENN_H_

#include <string>

using std::string;

class TableTennisPlayer
{
private:
  string firstname;
  string lastname;
  bool hasTable;

public:
  TableTennisPlayer (const string& fn = "none",
            const string& ln = "none",bool ht = false);
  void Name() const;
  bool HasTable() const {return hasTable;};
  void ResetTable(bool v) {hasTable = v;};
};

//derived class
class RatedPlayer:public TableTennisPlayer //TableTennisPlayer Is the base class 
{
private:
  unsigned int rating;
public:
  RatedPlayer(unsigned int r = 0,const string& fn = "none",const string& ln = "none",
        bool ht = false);// Default constructor 
  RatedPlayer(unsigned int r,const TableTennisPlayer& tp);// Create a derived class object constructor from a base class object 
  unsigned int Rating() const {return rating;}
  void ResetRating (unsigned int r) {rating = r;}
};

#endif

tabtenn.h

Class method definition:


#include <iostream>
#include "tabtenn.h"

TableTennisPlayer::TableTennisPlayer (const string& fn,const string& ln,bool ht):
  firstname(fn),lastname(ln),hasTable(ht)// Member initialization list 
{}

void TableTennisPlayer::Name() const
{
  std::cout << lastname << ", " << firstname;
}

//RatedPlayer methods
// The derived class constructor must call the base class constructor 
RatedPlayer::RatedPlayer(unsigned int r,const string& fn,const string& ln,bool ht):
  TableTennisPlayer(fn,ln,ht)// The derived class constructor creates the base class object first, using the initialization list 
{
  rating = r;
}

RatedPlayer::RatedPlayer(unsigned int r,const TableTennisPlayer& tp):
  TableTennisPlayer(tp),rating(r)
{}

tabtenn.cpp

The above code places the base class TableTennisPlayer and the derived class RatedPlayer at 1. The RatedPlayer class declaration USES :public name_of_base_class to indicate public derivation. Add your own constructor and additional member functions and methods to the declaration of a derived class. There is a lot of knowledge in derived class constructors.

The base class object must be created before the derived class object can be created, because the methods of the derived class cannot directly access the private members of the base class. That's the problem: the derived class constructor is automatically called when a new derived class object is created. How do you create a base class object by calling a base class constructor before calling a derived class constructor? You need to use the constructor's syntax to initialize the list. The base class constructor is called in the initialization list before the program pointer points to the first line in the curly braces of the derived class constructor. To make it easier to pick derived class objects directly from the base class object (the base class object contains derived class objects), use the second constructor to add score information directly to the base class object.

3. Sample application

Application code:


#include <iostream>
#include "tabtenn.h"

using std::endl;
using std::cout;

int main()
{
  TableTennisPlayer player1("Tara","Boomdea",false);// Create the base class object 
  RatedPlayer rplayer1(1140,"Mallory","Duck",true);// Create a derived class object 
  player1.Name();
  if(player1.HasTable())
    cout << ": has a table.\n";
  else
    cout << ": hasn't a table.\n";
  rplayer1.Name();
  if(rplayer1.HasTable())
    cout << ": has a table.\n";
  else
    cout << ": hasn't a table.\n";

  //initialize RatedPlayer using TableTennisPlayer object
  RatedPlayer rplayer2(1212,player1);
  cout << "Name: ";
  rplayer2.Name();
  cout << ";Rating: " << rplayer2.Rating() << endl;
  return 0;
}

usett.cpp

player and rplayer represent base class objects and derived class objects, respectively. rplayer2 and player1 are actually the same person, and the members who originally participated in the competition are selected from all the members. The application is relatively simple, but I won't describe it here.


Related articles: