Inheritance of C ++ learning notes

Source: Internet
Author: User

Inheritance of C ++ learning notes
I. In many cases, the objects of one class are also the objects of another class. For example, the rectangle is a quadrilateral. In C ++, the Rectangle class of the Rectangle class can be inherited from the Quad class of the Quadrilateral class. Therefore, the Quad class of the Quadrilateral class is the base class, And the Rectangle class of the Rectangle class is the derived class. However, it is obvious that the quadrilateral is a rectangle. Examples of basic and derived classes: Basic Derived classes include rice, noodles, dumplings, transportation vehicles, trains, airplanes, countries like China, the United States, and Spain, the object of each derived class is an object of the base class, and a base class can have many derived classes. An inheritance relationship forms a tree hierarchy. This hierarchical relationship exists between the base class and the derived class. For example, the following program example shows how to create a TableTennisPlayer class for a table tennis member: copy code 1 # ifndef TABTEN_H _ 2 # define TABTEN_H _ 3 # include <string> 4 using std: string; 5 // a simple base class 6 class TableTennisPlayer 7 {8 private: 9 string firstname; 10 string lastname; 11 bool hasTable; 12 public: 13 TableTennisPlayer (const string & fn = "none", 14 const string & ln = "none", bool ht = false); 15 void Name () const; 16 bool HasTable () Const {return hasTable;} 17 void ResetTable (bool v) {hasTable = v;} 18 }; 19 20 # endif copy code 1 # include <iostream> 2 # include "tabten. h "3 4 TableTennisPlayer: TableTennisPlayer (const string & fn, 5 const string & ln, bool ht): firstname (fn), lastname (ln), hasTable (ht) {} 6 7 void TableTennisPlayer: Name () const 8 {9 std: cout <lastname <"," <firstname; 10} copy the code TableTennisPlayer class to only record the member's name and Whether there is a table tennis. Assume that some members have participated in the championship, such a class is required, which can include the score of members in the competition. Do we need to create a new class? Obviously, you only need to derive a class from the TableTennisPlayer class. Assume It is RatedPlayer class RatedPlayer: public TableTennisPlayer {...}; colon indicates that the base class of the RatedPlayer class is TableTennisPlayer. public indicates that TableTennisPlayer is a public base class, which is called public derivation. When using public derivation, the public Member of the base class will become the public Member of the derived class, and the private part of the base class will also become part of the derived class, but only through the public (public) of the base class) and Protection (protected) method access. The RatedPlayer object has the following features: the derived class object stores the data members of the base class (the derived class inherits the implementation of the base class) the derived class can use the method of the base class (the derived class inherits the interface of the base class). The derived class also needs to do: add your own constructor as needed add additional data members and member functions add the header file of the derived class as follows: Copy code 1 # include <iostream> 2 # include "tabten. h "3 4 TableTennisPlayer: TableTennisPlayer (const string & fn, 5 const string & ln, bool ht): firstname (fn), lastname (ln), hasTable (ht) {} 6 7 void TableTennisPlayer: Name () const 8 {9 std: cout <lastname <"," <firstname; 1 0} 11 12 class RatedPlayer: public TableTennisPlayer13 {14 private: 15 unsigned int rating; // Add data member 16 public: 17 // The constructor of the derived class must provide data 18 RatedPlayer (unsigned int r = 0, const string & fn = "none", 19 const string & ln = "none", bool ht = false); 20 RatedPlayer (unsigned int r, const TableTennisPlayer & tp ); 21 unsigned int Rating () const {return rating;} // Add Method 22 void ResetRating (unsig Ned int r) {rating = r ;}// add Method 23}; copy the code and copy code 1 # include <iostream> 2 # include "tabten. h "3 4 TableTennisPlayer: TableTennisPlayer (const string & fn, 5 const string & ln, bool ht): firstname (fn), lastname (ln), hasTable (ht) {} 6 7 void TableTennisPlayer: Name () const 8 {9 std: cout <lastname <"," <firstname; 10} 11 12 RatedPlayer :: ratedPlayer (unsigned int r, const string & fn, 13 const string & Ln, bool ht): TableTennisPlayer (fn, ln, ht) 14 {15 rating = r; 16} 17 RatedPlayer: RatedPlayer (unsigned int r, const TableTennisPlayer & tp) 18: TableTennisPlayer (tp), rating (r) {} copy the constructor of the Code derived class to provide data to new members (if added) and members of the base class. The derived class cannot directly access the Private Members of the base class, but must be accessed through the base class method. For example, the RatedPlayer constructor cannot directly set the inherited members firstname, lastname, and hasTable, the public method of the base class must be used to access the private base class members. The main points of the derived class constructor are as follows: first, create a base class object. The derived class constructor passes the information of the base class to the base class constructor. The derived class constructor should initialize the new data member of the derived class. Ii. Use a derived class to copy code 1 # include <iostream> 2 # include "tabten. h "3 4 int main () 5 {6 using std: cout; 7 using std: endl; 8 TableTennisPlayer player1 (" Tara "," Boomdea ", false ); 9 RatedPlayer rplayer1 (1300, "Mallory", "Duck", true); 10 rplayer1.Name (); // The derived class calls the base class method 11 cout <(rplayer1.HasTable ()? (": Has a table \ n"): ("hasn't a table \ n"); 12 player1.Name (); 13 cout <(rplayer1.HasTable ()? (": Has a table \ n"): ("hasn't a table \ n"); 14 cout <"Name:"; 15 rplayer1.Name (); 16 cout <"; Rating:" <rplayer1.Rating () <endl; 17 18 // uses the base class object to initialize the derived class 19 RatedPlayer rplayer2 (1212, player1 ); 20 cout <"Name:"; 21 rplayer2.Name (); 22 cout <"; Rating:" <rplayer2.Rating () <endl; 23 24 return 0; 25} copy the code running result: 3. Inheritance of protected data when the member data in the base class is protected, the derived class can be directly accessed, instead of using the public methods of the base class to access the protected data, simply put, A derived class can directly inherit protected data members, eliminating the overhead of Calling member functions and slightly improving program performance. In a class declaration, a good class declaration sequence is best to Declare public first, then protected, and finally private. Precautions for using protected data: the derived class object does not need to use the member function to set the protected data member value of the base class. The derived class can easily assign invalid values to the protected data of the base class, as a result, the object is in an unreliable state and the protected data member is used. As a result, the implementation of the derived class member function may be too dependent on the implementation of the base class. In fact, the derived class should only rely on the services provided by the Base Class (that is, non-private member functions), rather than the implementation of the base class, using private data members is a better method for Software Engineering. Although the inheritance of protected data improves the program performance slightly, the code optimization should be done by the compiler, in this way, the code is easier to maintain, modify, and Debug. In a single sentence, do not use the inheritance of protected data unless you have. "Programmers should devote themselves to writing code that complies with the software engineering principles, and leave the optimization problems to the compiler ". A good principle is: "Do not doubt the compiler ". 4. The supplementary Derived classes do not inherit the constructors, destructor, and overloaded value assignment operators of the base classes, however, a derived class can call constructors, destructor, and overloaded value assignment operators of the base class. When a base class is derived from a class, there are three methods to inherit the base class: public inheritance, protected inheritance, and private inheritance. However, it is generally difficult to use private inheritance and protected inheritance, and you need to be very careful when using them. When a class is derived from the public base class, the public Member of the base class becomes the public Member in the derived class, and the protected member of the base class becomes the protected member in the derived class. A derived class can never directly access the private member of the base class, but it can be accessed by calling the public and protected member functions of the base class. When a class is derived from the protected base class, both the public and protected members of the base class are converted to the protected member in the derived class. When a class is derived from the private base class, the public and protected members of the base class are both private members in the derived class. private and protected inheritance are not "is-a" relationships.

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.