Several inheritance methods in class design

Source: Internet
Author: User
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;
A;
A * pA = new C;
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.

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.