C + + Learning Note (12): Class inheritance, virtual functions, pure virtual functions, abstract classes, and nested classes

Source: Internet
Author: User

Class inheritance

In C + + class inheritance, a derived class can derive from a base class or from more than one base class. Inheritance derived from a base class is called single inheritance, and inheritance derived from multiple base classes is called multiple inheritance.

1 //definition of single inheritance2 classB: PublicA3 {   4< new definition members for derived classes >5 }; 6 //definition of multiple inheritance7 classC: PublicAPrivateB8 {   9< new definition members for derived classes >Ten};

This article mainly says single inheritance.

There are three types of C + + class inheritance in a derived class:

Public inheritance

    1. The public and protected members of the base class can be public members and protected members of their derived classes
    2. member functions of derived classes can access public and protected members in the base class and cannot access private members in the base class
    3. The object of the derived class can access the public members of the base class

Private Inheritance (private)

    1. Both public and protected members of the base class are private members of their derived classes
    2. In private inheritance, a member of a base class can only be accessed by a direct derived class and cannot be inherited further down

Protect C + + class inheritance (protected)

    1. All public and protected members of the base class become protected members of derived classes
    2. The public and protected members of a base class can only be accessed by its direct derived class member function or friend

Constructors and destructors cannot be inherited

The general syntax for a derived class constructor is as follows:

1 derived class Name:: derived class name (parameter summary table)   2 : base class name 1 (parameter Table 1),..., base class name N (parameter table N)   3 {  4 Initialization of statement   5 }

Call the parent class constructor:

1 classParent {2  Public:3Parent (intXinty);4 };5 6 classSubclass: PublicParent {7  Public:8Subclass (intXinty): Parent (x, y) {}9};

Call the parent class function:

1 classA2 {3      Public:4        voidShow ()5        {6std::cout<<"I am Class A"<Std::endl;7         }8 }9 classB: PublicATen { One      Public: A        voiddosomething () -        { - a::show (); the        } -}

Virtual functions

The function of virtual function in C + + is mainly to realize the mechanism of polymorphism. In terms of polymorphism, in short, a pointer to the parent type points to an instance of its child class, and then invokes the member function of the actual subclass through a pointer to the parent class.

A method that is not defined as a virtual function, a function called by a pointer to an instance of its child class, is still a function of the parent class.

Once this definition is placed in the Java language, it can be said that all functions in a Java class are virtual functions.

Defined:

1 class Base {2public:3virtualvoid"  Base::f" << Endl;} 4 };

Pure virtual function

Pure virtual function is a special virtual function, in many cases, in the base class can not give a meaningful implementation of virtual function, and declare it as pure virtual function, its implementation is left to the derived class of the base class to do. This is the function of pure virtual functions.

Defined:

1 virtual < type >< function name > (< parameter table >) =0;

Abstract class

In C + + there is no abstract this keyword, whether a class is an abstract class is judged by whether it contains pure virtual functions.
Abstract classes cannot define objects, and in practice, in order to emphasize that a class is an abstract class, the constructor of the class can be described as Protected Access control permissions.
Abstract classes can only be used as base classes, and implementations of their pure virtual functions are given by derived classes. If a derived class does not redefine a pure virtual function, and the derived class simply inherits the pure virtual function of the base class, the derived class is still an abstract class. If the implementation of a base class pure virtual function is given in a derived class, then the derived class is no longer an abstract class, and it can create an instance of the class.

The provisions of the abstract class:

    1. An abstract class can only be used as a base class for other classes and cannot create abstract class objects.
    2. Abstract classes cannot be used as types of parameter types, function return types, or explicit conversions.
    3. You can define pointers and references to abstract classes, which can point to its derived classes for polymorphism.

Nested classes

Define another class within one class, which we call a nested class (nested Class), or a nested type. The introduction of such a nested class is often due to the fact that the perimeter class needs to use a nested class object as the underlying implementation, and that the nested class is used only for the implementation of the perimeter class, and that the underlying implementation can be hidden from the user at the same time.

Although a nested class is defined inside a perimeter class, it is a separate class that is basically not related to the perimeter class. Its members are not part of the perimeter class, and similarly, members of the perimeter class do not belong to that nested class. The appearance of nested classes only tells the perimeter class that there is one such type member for use by the perimeter class. Also, the perimeter class does not have any privileges for access to nested class members, and so does the nested class's access to the perimeter class members, all of which follow the label access control that the normal class has.

If a member is not defined inside a nested class, its definition can only be written to the same scope as the perimeter class, and the definition cannot be written in the perimeter class unless it is qualified with a perimeter class. For example, a static member of a nested class is one such example.

As mentioned earlier, another reason for using nested classes is to achieve the hidden purpose of the underlying implementation. To achieve this, we need to define the nested class in another header file, and only forward the nested class in the perimeter class. Of course, when defining this nested class outside the perimeter class, you should use a perimeter class to qualify. When used, you only need to include this header file in the implementation file of the perimeter class.

In addition, nested classes can refer directly to the static members, type names, and enumeration members of the perimeter class, even if these are private.

1 #ifndef Nestclass_h_2 #defineNestclass_h_3 classA4 {5  Public:6 A ();7~A ();8     voidoperate ();9 Private:Ten     classB; Oneb*M_b; A }; - #endif -#include"Nestclass.h" the#include <iostream> - using namespacestd; - classa::b - { +  Public: - B () {} +~B () {} A     voidoperate () at     { -cout<<"B operate!"<<Endl; -     } - }; - a::a () - { in } -a::~A () to { + } - voida::operate () the { *M_b =NewB; $cout<<"A operate!"<<Endl;Panax NotoginsengM_b->operate (); - } the#include"Nestclass.h" + voidMain () A { the A; + a.operate (); -}

We can only declare pointers and references to nested classes before the definition of the nested class is seen, as the above is defined in a as B m_b instead of b* M_b will throw a compilation error.

C + + Learning Note (12): Class inheritance, virtual functions, pure virtual functions, abstract classes, and nested classes

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