Three inheritance methods in class design and three inheritance methods are designed.
Zookeeper
By inheritance, a new class can be derived from an existing class, and the derived class inherits the features of the base class, including methods. Just as inheriting a property is easier than just starting from scratch, it is usually much easier to derive classes from inheritance than to design new classes. The following are some tasks that can be completed through inheritance.
① You can add functions based on existing classes.
② You can add data to the class.
③ You can modify the behavior of class methods.
C ++ has three inheritance Methods: Public inheritance, protection inheritance, and private inheritance.
1. Public inheritance
Public inheritance is the most commonly used method. It establishes a is-a relationship, that is, a derived class object is also a base class object and can perform any operation on the base class object, it can also be executed on the derived class object.
① Public inheritance does not establish the has-a relationship. For example, a lunch may contain fruit, but it is generally not fruit, so it cannot be derived from the fruit class.
② Public inheritance cannot establish a is-like-a relationship, that is, it does not use metaphorical. Generally, lawyers are sharks, but lawyers are not sharks. Therefore, lawyers cannot be derived from sharks.
③ Public inheritance does not establish a is-implemented-as-a (as implemented as...) relationship. For example, an array can be used to implement a stack, but it is not appropriate to derive a stack class from an array class because the stack is not an array, and at least the array index is not a stack attribute. The correct method is to hide the array implementation by letting the stack contain a private array object member.
④ Public inheritance does not establish the uses-a relationship. For example, a Computer can use a laser Printer, but it makes no sense to derive the Printer class from the Computer class. However, you can use friend functions or classes to process the relationship between Printer objects and Computer objects.
Multi-state public inheritance
Implementation Mechanism:
① Redefine the methods of the base class in the derived class.
② Use the virtual method.
For example:
Class
{
Public:
Virtual ~ A (){}
Virtual void show ()
{
Cout <"A" <endl;
}
};
Class B: public
{
Public:
Void show ()
{
Cout <"B" <endl;
}
};
Class C: public B
{
Public:
Void show ()
{
Cout <"C" <endl;
}
};
Int main ()
{
C c;
A;
A * pA = new C;
B B;
B * pB = new C;
A. show ();
B. show ();
C. show ();
PA-> show ();
PB-> show ();
Delete pA;
Delete pB;
}
1) We can see that the show () method in the base class has different behaviors in the derived class, and the program will use the object type to determine which version to use. For example:
A. show (); // use A: show ()
B. show (); // use B: show ()
C. show (); // use C: show ()
2) After using virtual, if the method is referenced by reference or pointer instead of object, it will determine to use that method. If the keyword virtual is not used, the program selects a method based on the reference type or pointer type. If virtual is used, the program selects a method based on the type of the object to which the reference or Pointer Points. For example:
If show () in the base class does not use virtual
PA-> show (); // use A: show ()
PB-> show (); // use B: show ()
If show () in the base class uses virtual
PA-> show (); // use C: show ()
PB-> show (); // use C: show ()
You may wonder why the show () method in Class B is not declared as virtual as the base class of class C, but it also calls the method in Object c. Here we must be clear that after a method is declared as virtual in the base class, it will automatically become a virtual method in the derived class.
3) The base class declares a virtual destructor of the class. This is done to ensure that the Destructor is called in the correct order when the derived object is released. When delete is used to release objects allocated by new, if the Destructor is not virtual, only the Destructor corresponding to the pointer type will be called, for example, the above only calls the destructor of type A and type B, even if the pointer points to A C object. If the Destructor is virtual, the corresponding object type destructor is called. Therefore, if the pointer points to the C object, it will call the C destructor, and then the uterus calls the basic class destructor. Therefore, using virtual destructor ensures that the correct sequence of destructor is called.
4) The difference between private and protected is only manifested in the base class derived class. A member of a derived class can directly access the protected member of the base class, but cannot directly access the private member of the base class. Therefore, for the external world, the behavior of protecting members is similar to that of private members, but for a derived class, the behavior of protecting members is similar to that of public members.
5) The constructor cannot be a virtual function. When creating a derived class object, the constructor of the derived class is called instead of the constructor of the base class. Then, the constructor of the derived class uses a constructor of the base class, this order is different from the inheritance mechanism. Therefore, a derived class does not inherit the constructor of the base class, so it makes no sense to declare a class constructor as a virtual one.
Ii. Private inheritance
A major objective of C ++ is to promote code reuse. Public inheritance is one of the mechanisms to achieve this goal, but it is not the only mechanism. If a class is an object of another class. This method is called include, combine, or hierarchical. You can also implement this inclusion relationship by using private or protected inheritance. Generally, include, private inheritance, and protection inheritance are used to implement the has-a relationship, that is, the new class will contain the object of another class.
With private inheritance, both the public and protected members of the base class will become private members of the derived class. This means that the base class method will not be called part of the public interface of the derived class object, but they can be used in the member functions of the derived class.
Using Public inheritance, the public methods of the base class will become the public methods of the derived class. In short, the derived class inherits the interface of the base class; this is part of the is-a relationship.
When private inheritance is used, the public methods of the base class become the private methods of the derived class. In short, the derived class does not inherit the interface of the base class. This incomplete inheritance is part of the has-a relationship.
The object is added to the class as a named member object, while the private inheritance adds the object to the class as an unnamed inherited object.
Includes:
# Include <string>
# Include <valarray>
Using namespace std;
Class student
{
Public:
Double Average () const;
Private:
Typedef valarray <double> ArrayDb;
String name; // contained object
ArrayDb scores; // contained object
};
Private inheritance:
Class student: private string, private valarray <double>
{
Public:
Double Average () const;
Const string & Name () const;
...
};
1) initialize the base component
For constructor, the following constructor will be used:
Student (const char * str, const double * pd, int n)
: Name (str), scores (pd, n ){}
For an inherited class, it uses the class name instead of the member name to identify the constructor:
Student (const char * str, const double * pd, int n)
: String (str), valarray <double> (pd, n ){}
2) methods for accessing the base class
Contains the call method using objects:
Double student: Average () const
{
If (scores. size ()> 0)
Return scores. sum ()/scores. size ();
Else
Return 0;
}
Private inheritance allows you to use the class name and scope resolution operators to call the methods of the base class:
Double student: Average () const
{
If (valarray <double >:: size ()> 0)
Return valarray <double >:: sum ()/valarray <double >:: size ();
Else
Return 0;
}
3) access the base class Object
When private inheritance is used, this string object does not have a name. How can the code of the student class access the internal string object? The answer is to use forced type conversion. Because the student class is derived from the string class, you can convert the student object to the string object by force type conversion. The result is the inherited string object.
Const string & student: Name () const
{
Return (const string &) * this;
}
The above method returns a reference. The application points to the inherited string object in the student object used to call the method.
4) access the membership functions of the base class
Specifying the function name with the class name display is not suitable for youyuan functions because youyuan does not belong to the class. However, you can call the correct function by converting the display to the base class. For example, for the following user meta Function Definition:
Ostream & operator <(ostream & OS, const student & stu)
{
OS <"scores for" <(const string &) stu <": \ n ";
...
}
Note: References to stu are not automatically converted to string references. The root cause is that, in private inheritance, Without Explicit conversions, you cannot assign a reference or pointer to a derived class to a base class reference or pointer.
Iii. Inheritance Protection
Protection inheritance is a variant of private inheritance. Protection inheritance uses the keyword protected when listing base classes.
When inheritance is protected, both the public and protected members of the base class become the protected members of the derived class. When another class is derived from a derived class (public), the main difference between private Inheritance and Protection inheritance is displayed. When private inheritance is used, the third generation will not be able to use the interface of the base class, because the public methods of the base class will become private methods in the derived class. When using protected inheritance, the public methods of the base class will become protected in the third generation, so the third generation derived classes can use them.
In C ++, there are three inheritance methods for comparative classes: total inheritance, protected inheritance, and private inheritance.
Under the Inheritance of three different methods, the derived class has different access methods to the members of the original base class, as follows:
1> Public inheritance:
(1) The common members of the base class are equivalent to the public members of the derived class. That is to say, the derived class can access the public members inherited from the base class just like its own public members.
(2) The protection Member of the base class is equivalent to the protection Member of the derived class. That is, the derived class can access the protection Member of the base class just like the protected member who accesses the base class.
(3) For Private Members of the base class, the internal members of the derived class cannot be directly accessed, and the users of the derived class cannot directly access it through the object of the derived class.
2> protected inheritance:
(1) The public and protected members of the base class are equivalent to the protected members of the derived class. The Derived classes can access them through their own member functions or their member functions.
(2) Private Members of the base class cannot be directly accessed regardless of the internal members of the derived class or the objects of the derived class.
3> private inheritance:
(1) Both the public and protected members of the base class are equivalent to the Private Members of the derived class. The derived class can only access them through its own member functions.
(2) Private Members of the base class cannot be directly accessed regardless of the internal members of the derived class or the objects of the derived class.
I only wrote a theoretical comparison for you. It is too troublesome to raise the example. If you think it is necessary to explain it through examples, I can write it to you, you can also ask me some questions you don't know.
Different inheritance methods in c ++
Public inheritance
Protected protection inheritance
Private inheritance
We know that the private and protected members of the class cannot be used outside the class. Only public members can be used outside the class.
When the primary class is inherited from the public, the private member derived classes of the base class are also unavailable. The public and protected members of the base class can be directly used in the derived class. only the public member can be directly used outside the derived class.
During protection inheritance, the private Members of the base class still have the public and protected members of the private. base class and become the protected members of the derived class. In this case, the public members of the original base class cannot be directly used outside the derived class.
During private inheritance, the private member of the base class is still the public and protected members of the private. base class, and will become the private member of the derived class.
For example.
Class
{
Public:
Int m_nTelNum;
Protected:
Int m_nAge;
Private:
Int m_nMoney;
};
Class B: public
{
Void SetTelNum (int nTelNum)
{
M_nTelNum = nTelNum;
}
Void SetAge (int nAge)
{
M_nAge = nAge;
}
Void SetMoney (int nMoney)
{
M_nMoney = nMoney; // The error is returned because the private member of the base class cannot be used.
}
};
B objB; // create a class B object objB
ObjB. m_nTelNum = 123456; // Yes
ObjB. m_nAge = 30; // error. public inherits the base class protected, which is protected in the derived class.
ObjB. m_nMoney = 100; // more error. It cannot be used directly in a derived class. It cannot be used outside the class.
Class C: protected
{
Void SetTelNum (int nTelNum)
{
M_nTelNum = nTelNum;
}
Void SetAge (int nAge)
{
M_nAge = nAge;
}
Void SetMoney (int nMoney)
{
M_nMoney = nMoney; // The error occurs here, because this is a private member of the base class and cannot be used.
}
};
C objC; // create the object objC of class C
ObjC. m_nTelNum = 123456; // note the difference between this and public. Here the error is returned. m_nTelNum becomes the protected member of class C.
ObjC. m_nAge = 30; // error. protected inherits the base class protected in the derived class, which is the same as public.
ObjC. m_nMoney = 100; // more error. It cannot be used directly in a derived class. It cannot be used outside the class.
Class D: private
{
Void SetTelNum (int nTelNum)
{
M_nTelNum = nTelNum;
}
Void SetAge (int nAge)
{
... The remaining full text>