The example code explains the c++ inheritance feature

  • 2020-10-23 20:15:02
  • OfStack

Derived classes need their own constructors.

Derived classes can add additional data members and member functions as needed, or even give new definitions to inherited original member functions.

A base class pointer or reference can point to a derived object, which in turn can only be cast.

Derived class objects can use non-private members of the base class.

You can use derived objects to initialize base class objects or assign values.

Assignment of base class objects to derived class objects is generally not allowed (rule 3 above), but in special cases.

An existing derived class object initializes the created derived class object.

An existing derived class object assigns a value to another derived class object.

The destructor of the derived class object is automatically called when the destructor of the derived class object is called.

C++11 adds a mechanism that allows constructors to be inherited, but derived classes cannot inherit constructors and destructors by default.


class RatedPlayer : public TableTennisPlayer
 {
 private:
 unsigned int rating; // add a data member
 public:
 RatedPlayer (unsigned int r = 0, const string &fn = "none", const string &ln = "none", bool ht = false);
 RatedPlayer (unsigned int r, const TableTennisPlayer &tp);
 unsigned int Rating() const { return rating; } // add a method
 void ResetRating (unsigned int r) { rating = r; } // add a method
};

Constructors must provide data to new and inherited members.


RatedPlayer::RatedPlayer(unsigned int r, const string &fn, const string &ln, bool ht) : TableTennisPlayer(fn, ln, ht)
{
 rating = r;
}
A derived class constructor must use a base class constructor. When creating a derived class object, the program first creates the base class object (initializing the inherited data member) and then calls the derived class constructor. C++ USES the member initialization list syntax to do this. If the base class constructor is not called, the default constructor of the base class is implicitly called. Unless you are using the default constructor, the correct base class constructor should be displayed for invocation.

When a derived class object expires, the program first calls the derived class destructor and then automatically calls the base class destructor.

To use derived classes, a program must have access to the base class declaration.

Derived class objects can use the methods of the base class as long as the methods are not private (that is, public and protected).

Base class Pointers can point to derived class objects without display cast; Base class references can refer to derived class objects without a display cast


RatedPlayer rplayer(1140, "Mallory", "Duck", true);
TableTennisPlayer &rt = rplayer;
TableTennisPlayer *pt =&rplayer;
rt.Name(); // invoke Name() with reference
pt->Name(); // invoke Name() with pointer

Base class objects and addresses cannot be assigned to derived class references and Pointers unless cast is used. (A friend function is not a member function and therefore cannot be inherited, but you can use this method if you want to use a friend function of a base class, but be careful.)

The function reference or pointer arguments declared by the base class can be used in cases where the values are base or derived class objects and their addresses.


void Show(const TableTennisPlayer &rt)
{
 ...
}

TableTennisPlayer player1("Tara", "Boomdea", false);
RatedPlayer rplayer1(1140, "Mallory", "Duck", true);
Show(player1); // works with TableTennisPlayer argument
Show(rplayer1); // works with RatedPlayer argument

The omitted parameter is const TableTennisPlayer *rt, which is similar to above.

Reference compatibility property: Base class objects can be initialized as derived class objects.


RatedPlayer olaf1(1840, "Olaf", "Loaf", true);
TableTennisPlayer olaf2(olaf1);

The stereotype of the matching constructor:


TableTennisPlayer(const RatedPlayer &); // doesn't exit

There is no such constructor in the class, but there is an implicit copy constructor:


// implicit copy constructor
TableTennisPlayer(const TableTennisPlayer &);

That is, it initializes olaf2 as an TableTennisPlayer object nested within RatedPlayer object olaf1 (initializes the target base class object using nested base class objects in derived classes)

Similarly, derived objects can be assigned to base class objects:


RatedPlayer olaf1(1840, "Olaf", "Loaf", true);
TableTennisPlayer winner;
winner = olaf1; // assign derived to base object

In this case, the program USES the implicit overload assignment operator:


TableTennisPlayer &operator=(const TableTennisPlayer &) const;

Similarly, the target base class object is assigned by member using a nested base class object in a derived class.

A special case of assigning a derived class object to a base class object

A derived class object can be assigned a value to a base class object if it contains a constructor that converts the base class object to a derived class object, or if the derived class defines an assignment operator that assigns the base class object to the derived class object.

Initializes a derived class object created with an existing derived class object

Copying a member of a class or an inherited component of a class is done using the copy constructor of the class and is appropriate for the inherited base class object.

Assign a value to another derived class object with an existing derived class object

By member assignment, the class member assignment operator is called, and the base class component (inherited base class object) is assigned using the base class assignment operator

If a derived class USES dynamic memory allocation, the derived class's destructor, copy constructor, and copy operator must all handle the base class elements using the corresponding base class methods (showing the calling base class constructor and copy operator) :

For destructors, this is done automatically. For copy constructors, this is done by calling the base class's copy constructor from the initialization member list; If you don't, the default constructor for the base class is automatically called. For the copy operator, this is done by explicitly calling the assignment operator of the base class using the scope resolution operator:

RatedPlayer::RatedPlayer(unsigned int r, const string &fn, const string &ln, bool ht) : TableTennisPlayer(fn, ln, ht)
{
 rating = r;
}
0

That's how the example code explains the c++ inheritance feature in detail. For more information on c++ inheritance features, please follow the other articles on this site!


Related articles: