Chapter 14th Code reuse in C + +

Source: Internet
Author: User

? during the development of a derived class, the base class member is manipulated by the access control of the base class member in the base class.

C + + provides the following methods of code reuse:

1. Inheritance ( public inheritance (IS-A), private inheritance, and protection inheritance (has-a));

2. inclusive/hierarchical/combination (HAS-A);

3. Multiple inheritance

4. Class templates

Inheriting the implementation is inheriting the data member, inheriting the interface is inheriting the member function.

Interfaces ( methods of the base class can be used by derived class objects ) and implementations ( derived class objects store data members of the base class, which is implemented as data members can be manipulated (via member functions))

When using public inheritance, a class can inherit an interface and possibly an implementation (a pure virtual function of the base class provides an interface, but does not provide an implementation). The acquisition interface is part of the is-a relationship. In combination, a class can be implemented, but it cannot get an interface. Not getting an interface is part of the has-a relationship.

Although the inheritance of the has-a relationship does not inherit the interface of the base class, you can use the base class name and scope operators inside the member functions of the derived class to access the base class method.

Private inheritance

With private inheritance, the private members of the base class, and the protected members, become private members of the derived class. This means that base class methods do not become part of the public interface of derived class objects, but they can be used in member functions of derived classes.

' contains ' to add an object to the class as a named member object, while private inheritance adds the object to the class as an unnamed inherited object.

Techniques for private Inheritance:

1. Initializing the base class component

The constructor uses the member initialization list to represent the constructor using the class name instead of the member name.

Each inherited base class corresponds to only one member object.

2. Methods to access the base class

You can call the methods of the base class by using the class name and scope resolution operators.

3. Accessing base class objects

How does a derived class with private inheritance access the base class object? The answer is to use coercion type conversions.

4. Accessing the base class's friend function

Also by forcing type conversions

When a CV has-a relationship is required, the base class or private inheritance is used

Benefits included: 1. Easy to understand; 2. Inheritance can cause many problems (multiple inheritance); 3. Contains the ability to include multiple homogeneous sub-objects.

Advantages of Inheritance: 1. You can use a protected member of a base class in a derived class, and a protected member that cannot access an object member; 2. When you need to redefine a virtual function.

Protect inheritance

Protection inheritance is a variant of private inheritance, and when protected inheritance is used, the public and protected members of the base class become protected members of the derived class.

The main difference between private inheritance and protection inheritance is embodied in the third generation class. The third-generation class of private inheritance cannot use the method of the base class (because the public and protected members of the base class of the private inheritance will become private members of the second-generation class), while the protection inheritance can.

An implicit upward-type conversion means that you can point a base-class pointer or reference to a derived class without explicit type conversions.

Private inheritance cannot be implicitly type-cast, public inheritance can, and protection inheritance can be in derived classes (?). )。

redefine access with using using

When using protection or private derivation, to make a method of a base class available outside a derived class, wrap the function call in another function call, even if a using declaration is used to indicate that a derived class can use a particular base class member.

class student:std::valarray<Double>{    ...  Public :     using std::valarray<double>:: Min;     ......  }

Multiple Inheritance (MI)

Mi mainly brings two questions to programmers:

1. Inherit a method of the same name from two different base classes (a method with the same name between the base classes) (use class qualifiers in derived classes to differentiate them);

2. Multiple instances of the same class are inherited from multiple related base classes (when multiple base classes inherit from one ancestor, multiple instances of a class are produced in the object).

Resolve Issue 2.

Multiple derived objects will produce each base class object, and the ancestor class objects at each level of the base class. If there are multiple base classes inheriting from one ancestor, there will be multiple instances of this ancestor, resulting in two righteous questions.

C + + introduces a virtual base class that uses the keyword virtual in a class declaration to make the qualified Class A virtual base class. A virtual base class enables multiple derived objects to have only one ancestor class copy (from multiple base classes at the same time).

1 //ancestor classes (defined as virtual base classes in singer and waiter class declarations)2 classWorker {...};3 4  //base class singer5 classSinger:Virtual  PublicWorker6  { ...... };7  //base class Waiter8 classWaiter:Virtual  PublicWorker9 { ...... };Ten  One //multiple derived classes Singerwaiter A classSingerwaiter: PublicWorker, PublicWaiter -{ ...... };

When you introduce a virtual base class, you must modify the code: (The following ancestor class represents a common (virtual) base class for multiple classes)

Before a virtual base class is introduced, when a base class member object is initialized with a list in a constructor, an ancestor class object is passed through multiple paths to multiple base class constructors.

To avoid this problem C + + when the ancestor class is virtual, the forbidden information is automatically passed to the ancestor class through the intermediate class, and the default constructor of the ancestor class is called by default.

You can also explicitly call the ancestor class's constructor in the member list initialization.

question 1.

If you inherit two or more members of the same name from a different base class, using that member name will result in two semantics if you do not qualify with the class name.

However, if you are using a virtual base class, doing so does not necessarily result in two semantics. If a name takes precedence over all other names , it is used, even if it is not qualified with the class name, and does not result in two semantics.

How do I determine the name first?

Names in derived classes take precedence over the same names in direct or indirect ancestor classes.

Virtual ambiguity rules are independent of access rules:

Even if a function with the same name is private, it does not affect its precedence.

The complexity of MI is mainly caused by deriving classes inheriting the same base class through multiple paths.

。。。 Cond

Introduction to template class Valarray

The Valarray class object is used to store numbers and provides rich arithmetic support.

Valarray<double> v1; Empty Array valarray<int> v2 (8); An array of 8 int elements valarray<float> v3 (10.1, 8); An array of 8 float elements, each of which has a value of 10.1int gpa[5] = {1, 2, 3, 4, 5};valarray<int> v4 (GPA, 4);//v4 is initialized to the first 4 elements of a GPA

In c++11, you can initialize a list of Valarray objects.

Common methods:

Size ();

SUM ();

Min ();

Max ().

Chapter 14th Code reuse in C + +

Related Article

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: 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.