C ++ single inheritance

Source: Internet
Author: User

The basic concepts of single inheritance are described in the basic classes and derived classes. This section focuses on the specific application of inheritance.

In single inheritance, each class can have multiple Derived classes, but each derived class can have only one base class to form a tree structure.

  Access Control for Members

In the lecture "base class and derived class", we talked about the access permissions of the objects of the derived class and the derived class to the base class members, here, we use an instance to further discuss the specific control of access permissions, and then obtain the call methods when three inheritance methods are used.

// Example of an inherited public-inherited access permission
# Include

File: // defines the Base Class
Class
{
Public:
A () {cout <"constructor of Class! "<A (int A) {AA = A, AA = A, AAA = ;}
Void aprint () {cout <"Class A prints its own private member AA:" <int AA;
PRIVATE:
Int AA;
Protected:
Int AAA;
};

File: // defines Class B generated by the Base Class.
Class B: public
{
Public:
B () {cout <"constructor of Class B! "<B (int I, Int J, int K );
Void bprint () {cout <"Class B prints its own private member BB and protected member BBB:" <void B _aprint () {cout <"Public Function of Class B contains the public data member AA:" <cout <"Public Function of Class B protects the protected data member AAA of Class: "<getaaaa ();
Getaaaa1 ();}
PRIVATE:
Int BB;
Void getaaaa () {cout <"Class B's private function AA: "<cout <" Class B's private function protected data member AAA: "<protected:
Int BBB;
Void getaaaa1 () {cout <"Class B's protected function AA: "<cout <" Class B's protected function protects Class A's protected data member AAA: "<};

File: // The constructor of base class B. It is responsible for initializing the constructor of Base Class.
B: B (int I, Int J, int K): A (I), BB (J), BBB (k ){}

File: // main program function
Void main ()
{
B B1 (100,200,300); file: // defines an object B1 of Class B, and initializes the constructor and the base class constructor.
B1. B _ aprint (); file: // Class B calls its own member function B _aprint
B1.bprint (); file: // Class B object B1 accesses its own private and protected members
B1.aprint (); file: // call the Public member function of Class A through class B's object B1
}

The output result of this program is:

The public function of Class B is named AA: 100, which is a public data member of Class.

The public function of Class B protects the protected data member AAA: 100 of Class.

The private function of Class B is named AA: 100, which is a public data member of Class.

The private function of Class B protects the protected data member AAA: 100 of Class.

The protected function of Class B contains the public data member AA: 100 of Class.

The protected function of Class B protects the protected data member AAA: 100 of Class.

Class B prints its own private member BB and protected member BBB: 200,300

Class A prints its own private member AA: 100

The above is a public Inheritance Method, and we can draw the following conclusions:

When public inheritance (public) is implemented, the public, private, and protected member functions of the derived classes can access the public and protected members in the base class; the object of the derived class can only access the public members in the base class.

Let's change the Inheritance Method public to private. The following error occurs in the compilation result:

'Aprint': cannot access public member declared in class 'A'

The error statement is b1.aprint ();. Therefore, we can draw the following conclusion:

In public inheritance (private), the public, private, and protected member functions of the derived classes can access the public and protected members in the base class; however, the object of the derived class cannot access any member of the base class. In addition, when the class keyword is used to define a class, the default Inheritance Method is private, that is, when the inheritance method is private inheritance, private can be omitted.

Let's change the Inheritance Method public to protected. We can see that the result is the same as the private inheritance method.

 Constructor and destructor

The constructor of the derived class and the constructor are the main issues discussed. You should master it.

1. Constructor

We know that the data structure of the object in the derived class is composed of the data members described in the base class and the data members described in the derived class. The encapsulation body consisting of the data members and operations described in the base class in the object of the derived class is called the base class sub-object, which is initialized by the constructor in the base class.

Constructor cannot be inherited. Therefore, the constructor of a derived class must call the constructor of the base class to initialize the base class sub-objects. Therefore, when defining the constructor of a derived class, in addition to initializing its own data members, it must also call the base class constructor to initialize the base class data members. If a derived class contains sub-objects, the constructor initialized to the sub-objects should also be included.

The general format of the derived class constructor is as follows:

<Derived class name> (<general parameter table of the derived class constructor>): <base class constructor> (parameter table 1), <sub-Object Name> (<parameter table 2>)
{
<Data member initialization in a derived class>
};

The Calling sequence of the derived class constructor is as follows:

· Constructors of the base class

· Constructor of sub-object classes (if any)

· Constructor of a derived class

In the previous example, B: B (int I, Int J, int K): A (I), BB (J), BBB (k) is the definition of a derived class constructor. The following is an example of constructing a derived class constructor.

# Include
Class
{
Public:
A () {A = 0; cout <"default constructor of Class A./N ";}
A (int I) {A = I; cout <"constructor of Class A./N ";}
~ A () {cout <"Class A destructor./N ";}
Void print () const {cout <int geta () {reutrn ;}
PRIVATE:
Int;
}
Class B: public
{
Public:
B () {B = 0; cout <"default constructor of class B./N ";}
B (int I, Int J, int K );
~ B () {cout <"Class B destructor./N ";}
Void print ();
PRIVATE:
Int B;
A aa;
}
B: B (int I, Int J, int K): A (I), AA (j)
{
B = K;
Cout <"constructor of class B./N ";
}
Void B: Print ()
{
A: Print ();
Cout <}

Void main ()
{
B BB [2];
Bb [0] = B (1, 2, 5 );
Bb [1] = B (3, 4, 7 );
For (INT I = 0; I <2; I ++)
Bb [I]. Print ();
}

2. Constructor

When an object is deleted, the destructor of the derived class is executed. Because the Destructor cannot be inherited, The destructor of the base class is also called when you execute the destructor of the derived class. The execution sequence is to execute the constructor of the derived class first, and then execute the destructor of the base class. The order is the opposite to the order in which the constructor is executed. This can be seen from the examples mentioned above. Please analyze it by yourself.

3. Precautions for using the derived class Constructor

(1) the call to the base class constructor can be omitted in the definition of a derived class constructor. The condition is that the base class must have a default constructor or no constructor is defined at all. Of course, no constructor is defined in the base class, and the derived class does not have to call the destructor of the base class at all.

(2) When the base class constructor uses one or more parameters, the derived class must define the constructor and provide a way to pass the parameters to the base class constructor. In some cases, the function body of the derived class constructor may be null and only serves as a parameter transfer function. This is the case in the first example.

  Subtype and type adaptation

1. Subtype

The sub-type concept involves behavior sharing, which is closely related to inheritance.

There is a specific type S. if and only when it provides at least the type T behavior, the called type S is the child type of the type T. Subtypes are the general and special relationships between types.

In inheritance, public inheritance can implement subtypes. For example:

Class
{
Public:
Void print () const {cout <"A: Print () called./N ";}
};
Class B: public
{
Public:
Void F (){}
};

Class B inherits Class A and is a public inheritance method. Therefore, Class B is a subtype of Class. Class A can also have other child types. Class B is a subtype of Class A. Class B has operations in Class A, or operations in Class A can be used to operate Class B objects.

Subtype relationships are irreversible. This means that we know that B is the subtype of A, and think that A is also the subtype of B is wrong, or that the Subtype Relationship is asymmetric.

Therefore, it can be said that public inheritance can be subtyped.

2. Type adaptation

Type adaptation refers to the relationship between two types. For example, type B is applicable to type A, which means that type B objects can be used by type A objects.

The object of the derived class mentioned above can be used when the base class object can be used. We say that the derived class is applicable to the base class.

Similarly, pointers and references of derived class objects are also applicable to pointers and references of base class objects.

Subtypeization and type adaptation are possible. If a is a subtype of B, A must be applicable to B.

The importance of child types is to reduce the burden on programmers to write program code. Because a function can be used for an object of a certain type, it can also be used for objects of various child types of this type, so that it is not necessary to reload the function to process these child types of objects.

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.