Inheritance and derivation of [C + +] Classes

Source: Internet
Author: User
Tags access properties types of functions

Inheritance is the second most characteristic of object-oriented programming, which allows the creation of new classes on the basis of existing classes, which can inherit data members and member functions from existing classes, add their own unique data members and member functions, and redefine member functions in existing classes. The use of class inheritance and derivation to achieve a higher level of code reusability, in line with modern software development ideas.

The C + + language supports both single inheritance and multiple inheritance. A single inheritance is a derived class that inherits from only one base class, and, accordingly, multiple inheritance refers to a derived class that inherits from two or more base classes at the same time. Java only supports single inheritance.

I. Derived classes

The defined format of the derived class is as follows:

Class < derived class name >:[inheritance]< base class name 1>

[, [Inherit Way]< base class name 2>,...,[inherit Way]< base class name N>]

{

< new data members and member function definitions for derived classes >

};

Description

(1) The definition of derived class keywords can be class or struct, the difference is: Define derived classes with class, the default is private, the struct defines the derived class, the default inheritance is public. The newly added member default property is also the class corresponding to the private property, and the struct corresponds to the public property.

(2) The two types of functions that a base class cannot inherit from a derived class are constructors and destructors.

Two. The access properties of base class members in derived classes in 3 ways of inheriting

Take a quick look at the following code:

#include"stdafx.h"#include<iostream>using namespacestd;classbase{Private:    intPridata;protected:    intProdata; Public:    intPubdata;};classD1:PrivateBase//Private Inheritance{    voidF1 () {//pridata=1;//base class Private members are not directly accessible in derived classesProdata=2;//The protected member of the base class accesses the property in the derived class for privatePubdata=3;//The public member of the base class accesses the property in the derived class for private    }};classD2:protectedBase//Protect Inheritance{    voidF2 () {//pridata=1;//base class Private members are not directly accessible in derived classesProdata=2;//The protected member of the base class accesses the property for protected in the derived classPubdata=3;//The public member of the base class accesses the property for protected in the derived class    }};classD3: PublicBase//Public Inheritance{    voidF3 () {//pridata=1;//base class Private members are not directly accessible in derived classesProdata=2;//The protected member of the base class accesses the property for protected in the derived classPubdata=3;//The public member of the base class accesses the property in the derived class for public    }};intMain () {Base obj; //obj.pridata=1;//object cannot access the private member in the base class//obj.prodata=2;//object cannot access the protected member in the base classObj.pubdata=3;    D1 objD1; //objd1.pubdata=3;//private property, inaccessibleD2 objD2; //objd2.pubdata=3;//protected property, inaccessibleD3 objD3; Objd3.pubdata=3;//the public property can be accessed    return 0;}

The private member function of the base class is not directly accessible in the member functions of the derived class, but the member functions of the derived class can access those members indirectly by calling the function inherited by the base class. If a function of the base class is inherited and is still a public member in the derived class, it can be called directly from the derived class object.
Let's take a look at the access properties and functions of class members:

Accessing properties Role
Private Only member functions and friend functions of the class are allowed, and cannot be accessed by other functions
Protected Both the member function and the friend function of the class are allowed access, and the member functions of its derived classes are allowed access
Public Allows access to both member functions of the class and other functions outside of the class

Well, continue to understand it through the code:

#include"stdafx.h"#include<iostream>using namespacestd;classbase{Private:    intPridata;protected:    intProdata; Public:    intPubdata;//data members cannot be initialized in the definition of a class    voidSetData ()//Assigning a value to a data member in a base class{pridata= -; Prodata= $; Pubdata= -; }    voidPrint () {cout<<"pridata="<<priData<<Endl; cout<<"prodata="<<proData<<Endl; cout<<"pubdata="<<pubData<<Endl; }};classDerived: Publicbase{ Public:    voidChangedata () {SetData (); Prodata= A;//A member function class in a derived class can access a non-private member of the base class    }};intMain () {Base B;    B.setdata ();    B.print ();    Derived D1; D1.    Changedata (); D1.pubdata= -; D1.        Print (); return 0;}

The results of the program run as follows:

Three. Constructors and destructors for derived classes

When defining an object of a derived class, the newly added data member in the derived class is, of course, initialized with the constructor of the derived class, but the initialization of the data member inherited from the base class must be done by the constructor of the base class, which requires that the call to the base class constructor be completed in the constructor of the derived class. Similarly, the value of the destructor of a derived class can complete the cleanup of newly added data members in the derived class, and the cleaning up of data members inherited from the base class should also be done by the destructor of the base class. Because destructors cannot take parameters, destructors for derived classes directly call the destructor of the base class by default.

The derived class constructor defines the following format:

< derived class name > (< total form parameter table >):< base class name 1> (< parameter table 1>),

< base class name 2> (< parameter table 2>),[...,< base class name n> (< parameter table n>), other initialization;]

{

[< initialization of a derived class's own data member;]

}

Description

(1) The Total form table gives all the formal parameters in the constructor of the derived class, as the actual parameters of the call to the base class parameter constructor, and initializes the parameters of the data members of the class;

(2) In general, the actual parameters in the parameter table after the base class name are derived from the general table of the constructor form parameters of the preceding derivation class, but may also be constants independent of the preceding form parameters;

(3) In multi-level inheritance, each derived class only needs to provide parameters to the constructors of the direct base class, and if a base class has more than one derived class, each derived class is responsible for supplying the arguments to the accumulated constructor.

1. Single Inheritance
#include"stdafx.h"#include<iostream>using namespacestd;classother{ Public: Other () {cout<<"Constructing other class"<<Endl; }    ~Other () {cout<<"destructing Other Class"<<Endl; }};classbase{ Public: Base () {cout<<"Constructing Base class"<<Endl; }    ~Base () {cout<<"destructing Base class"<<Endl; }};classDerive: Publicbase{Private: other ot; Public: Derive () {cout<<"Constructing Derive class"<<Endl; }    ~Derive () {cout<<"destructing Derive class"<<Endl; }};intMain () {Derive D; return 0;}

The results of the program run as follows:

You can see the order in which constructors are called when defining derived class objects:

A. Calling the constructor of the base class first

B. Then call the constructor of the class to which the derived class object member belongs (if there are object members)

C. Last call to a constructor of a derived class

Destructors are called exactly the same sequence as the constructor call order.

2. Multiple inheritance
#include"stdafx.h"#include<iostream>using namespacestd;classgrand{intG; Public: Grand (intN): g (n) {cout<<"Constructor of class Grand g="<<g<<Endl; }    ~Grand () {cout<<"destructor of class Grand"<<Endl; }};classFather: Publicgrand{intF; Public: Father (intN1,intn2): Grand (N2), F (N1) {cout<<"Constructor of class Father f="<<f<<Endl; }    ~Father () {cout<<"destructor of class Father"<<Endl; }};classmother{intm; Public: Mother (intN): M (n) {cout<<"Constructor of class mother m="<<m<<Endl; }    ~mother () {cout<<"destructor of class mother"<<Endl; }};classSon: PublicFather, Publicmother{ints; Public: Son (intN1,intN2,intN3,intN4): Mother (N2), Father (N3,N4), S (N1) {cout<<"Constructor of class Son s="<<s<<Endl; }    ~Son () {cout<<"destructor of class Son"<<Endl; }};intMain () {Son s (1,2,3,4); return 0;}

The results of the program run as follows:

As you can see, unlike single inheritance, in multiple inheritance, a derived class has multiple parallel base classes that are in the order of invocation of the base class constructors at the same level, regardless of the order of the base classes specified when the derived class is declared, and not the sequence in which the base class constructor is called in the member initialization list of the derived class constructor.

Inheritance and derivation of [C + +] Classes

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.