Guidelines for inheritance
- Derived classes do not inherit the interface of the base class > because the public method of the base class becomes a private method of the derived class
- Derived classes inherit the implementation
Student Class Example 1.
Declaration of private inheritance
1 class student:private std::string,privatedouble >2{ 3 Public : 4 .... 5 };
Issues to be aware of:
- Private is the default value, and if omitted here, it also causes private inheritance
- Inheritance using multiple base classes is also known as multiple inheritance in the next section
- The most important point: student this new class does not require private data, and private inheritance provides him with two non-named child object Members
2. Initializing the base class component
Because private inheritance provides two non-named child object members for derived classes, we can no longer use object names to initialize as we did before, which is the second difference between the two student classes:
- Use the class name instead of the object name
1 Student (constChar* str,constdoubleint N)2 : std::string(str), Arraydb (pd,n) {}Description
typedef std::valarray< DOUBLE > Arraydb
- Arraydb is Std::valarraydb's nickname.
The header file looks like this
1 #ifndef Student_h_2 #defineStudent_h_3 4 #include5 #include6 #include7 8 classStudentPrivateSTD::string,PrivateStd::valarray9 {Ten Private: One typedef std::valarray ARRAYDB; AStd::ostream & Arr_out (std::ostream& OS)Const; - Public: -Student (): std::string("Null Student"), Arraydb () {} the ExplicitStudentConstSTD::string&s) -: std::string(s), Arraydb () {} - ExplicitStudentintN): std::string("nully"), Arraydb (n) {} -StudentConstSTD::string& S,intN) +: std::string(s), Arraydb (n) {} -StudentConstSTD::string& S,arraydb &a) +: std::string(s), Arraydb (a) {} AStudentConst Char* STR,Const Double* PD,intN) at: std::string(str), Arraydb (pd,n) {} -~student () {} - DoubleAverage ()Const; - Double&operator[](inti); - Double operator[](intIConst; - ConstSTD::string& Name ()Const; in //Friends -Friend Std::istream &operator>> (Std::istream & is, toStudent &Stu); +Friend Std::istream & Getline (Std::istream & is, -Student &Stu); theFriend Std::ostream &operator<< (Std::ostream &OS, * ConstStudent &Stu); $ };Panax Notoginseng #endif
Program Description:
1 Student (const std::string & S,arraydb & a)2 : std:: String(s), Arraydb (a) {}
In the Valarray concept, the Valarray object has a number of constructors, one of which is as follows:
1 double >v4 (GPA,4);
- Declares an array of four elements to initialize with the first 4 elements of Gap (also an object of Valarray).
3. Methods to access the base class
When we use private inheritance, how do we use the base class method?
记住:只能在派生类的方法中去使用基类的方法,同时,使用类名和作用域解析运算符来调用基类方法
Let's look at a comparison between two pieces of code:
(1) include
1 DoubleStudent::average ()Const2 {3 if(Scores.size () >0)4 {5 returnScores.sum ()/scores.size ();6 }7 Else8 {9 return 0;Ten } One}
(2) Private Inheritance
1 DoubleStudent::average ()Const2 {3 if(Arraydb::size () >0)4 {5 returnArraydb::sum ()/arraydb::size ();6 }7 Else8 {9 return 0;Ten } One}
- You can only use methods of the base class in the methods of a derived class, and use the class name and scope resolution operators to call the base class method
4. Accessing base class objects
Review a snippet of code first
1 Const string & Student::name ()const2{3 return Name; 4 }
This is the name of the data member in the containing version that is used to return the data member, which is a string object.
The name is named, but we know that in private inheritance, two non-named child object members are provided.
So how do you access the base class object?
- The specific code snippet for coercion type conversion is as follows:
1 Const string & Student::name ()const2{3return (const string &) *this4 }
Description
Obviously, student is derived from string, so we can use forced type conversions,
Converts a student object to a string object, transforming the result: A String object that inherits
The this pointer points to the object that called the method, so *this is the object itself, and in this code the student is the
To prevent the constructor from creating a new object, you can use the coercion type cast to create a reference and return a reference to the string object that is inherited from the student object that called the method.
5. Accessing the base class's friend functionIt is obviously not appropriate to use the class name and the scope resolution operator to tune the cell function, because, conceptually, the friend function is not part of the member function.
- Workaround: Convert the reference of the derived class to a reference to the base class, thereby adjusting the function of the element.
The code snippet is as follows:
1 ostream & operator << ( Std::ostream & Os,const student & Stu) 2 { os<< scores for " << (const string &) stu<< :\n " 4 return OS; 5 }
Remember one point: Stu is not automatically converted to a string reference, the root cause: a reference or pointer to a derived class that does not display the transformation, is a reference or a pointer that cannot be assigned to a base class. (What do you mean?) )
C + + code reuse-Private Inheritance (study notes)