Features of C + +--Inheritance

Source: Internet
Author: User

inheritance is the most important feature of object-oriented programming, so to speak, if you do not know the inheritance, it is equal to No


Master the essence of classes and objects, so a good grasp of inheritance is the basis for us to learn C + +, the following we introduce it.

Inheritance is an important means of object-oriented reuse . By inheriting the definition of a class, the relationships between their types are modeled , sharing common things, and implementing something different in their nature.

There are three ways of inheriting: public inheritance, protected inheritance, private inheritance.

Summary :

  1. A private member of a base class cannot be accessed in a derived class, and if some base class members do not want to be accessed directly by the base class object but need to be accessible in the derived class, they are defined as protected members. You can see that the Protection member qualifier occurs because of inheritance .

  2. Public inheritance is an interface inheritance that maintains the is-a principle, and the members that are available to each parent class are also available to the subclass, because each child class object is also a parent class object.

  3. Protetced/private inheritance is an implementation of inheritance, some members of the base class are not completely part of the subclass interface, is the has-a relationship principle, so the two inheritance relationships are not used under special circumstances, and in most scenarios, public inheritance is used.

  4. Either way, the public and protected members of the base class can be accessed inside the derived class, but the private members of the base class exist but are not visible in the subclass (inaccessible).

  5. The default inheritance method when using the keyword class is private, and the default inheritance method when using a struct is public, but it is best to show the inheritance in the way it is written.

  6. In the actual use of the general is the public inheritance, very few scenarios will use Protetced/private inheritance.


    • The following example shows the changes of the access relationships of various types of members of the base class under three inheritance relationships

class Person

{

Public :

void Display ()

{

cout << "_name" << Endl;

}


protected :

string _name; //Name

Private :

int _age; //Age

};



class Student : Public Person //You can modify the Inheritance property here

{

protected :

int _num; //Study No.

};


void Test ()

{

            person < Span style= "FONT-SIZE:14PT;" >p;

Student s; //created an object of a derived class (subclass) type


//1. Subclass objects can be assigned to parent class objects (cut/slice)

p = s;

//2. The parent class object cannot be assigned to a child class object

s = p;


//3. A pointer/reference to a parent class can point to a child class object

person* p1 = &s;

person& r1 = s;


//4. A pointer/reference to a subclass cannot point to the parent class object (can be done by forcing the type conversion)

            student * p2 = ( student *) & P;

Student& r2 = (Student &) p;


//What will happen here?

P2->_num = 10;

R2._num = 20;

}

After debugging, the following conclusions are obtained:

    • Inheritance and transformation-assignment compatibility rules

    1. Subclass objects can be assigned to parent class objects (cut/slice)

    2. Parent class object cannot be assigned to a subclass object

    3. A pointer/reference to a parent class can point to a child class object

    4. A pointer/reference to a subclass cannot point to the parent class object (can be done by forcing the type conversion)

    • Scopes in the inheritance system

    1. In the inheritance system, both the base class and the derived class have separate scopes.

    2. Subclasses and parent classes have members of the same name, and child class members block direct access to members of the parent class. (in subclass member functions, you can use the base class:: base class member access)--Hide

    3. Note that in practice, it is best not to define a member with the same name in the inheritance system .


    • Single Inheritance & Multiple inheritance

    1. Single inheritance--a subclass that has only one direct parent class that is called the inheritance relationship as a single inheritance

    2. Multiple inheritance--when a subclass has two or more direct parent classes, this inheritance relationship is called multiple inheritance

    • Virtual Inheritance--solving the two semantics of diamond inheritance and the problem of data redundancy

    1. Virtual inheritance solves the problem of the data redundancy & waste space of the face class object containing multiple parent objects in the diamond-like inheritance system.

    2. Virtual inheritance systems look complicated, and in practice we don't usually define such a complex inheritance system. It is generally not the last resort to define the virtual inheritance architecture of a diamond structure, because using virtual inheritance to solve data redundancy problems also leads to performance loss.


This article is from the "10911544" blog, please be sure to keep this source http://10921544.blog.51cto.com/10911544/1762307

Features of C + +--Inheritance

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.