C ++ inheritance Basic Concepts

Source: Internet
Author: User

The C ++ programming language also has object-oriented features. So what are the features of it? Here we will give you a detailed introduction to some basic features of C ++ inheritance, hoping to help you have a detailed understanding of this language.

  • C ++ exception throwing skills
  • C ++ new usage experience
  • Introduction to the correct use of C ++ inline functions
  • Introduction to unsuitable use of C ++ inline functions
  • Detailed description of the use of the C ++ comma Operator

1. What are the basic concepts of object-oriented technology?

Objects, classes, and inheritance.

2. virtual functions and Polymorphism

A virtual function is a member function that can be redefined by its subclass. The sub-class redefinition of the parent class virtual function is called override ).

Polymorphism allows you to set a parent object to be equal to one or more sub-objects. After a value is assigned, the parent object can operate in different ways based on the sub-objects assigned to it. To put it simply, a pointer of the subclass type can be assigned to a pointer of the parent class. Polymorphism is implemented through virtual functions in C ++.

3. What is the role of polymorphism?

Encapsulation can hide implementation details to modularize the Code; C ++ inheritance can expand existing code module classes); they are all for code reuse. Polymorphism aims to achieve another purpose-interface reuse!

4. Interface

C ++ does not actually have the interface keyword. It is implemented by pure virtual functions of abstract classes.

5. About classes

Class private internal representation is called encapsulated), and a common part of the class is called class interface ).

Common members: they can be accessed anywhere in the program. Classes that implement information hiding restrict their public members to member functions. To access a member function, you must define a class object.

Private Members: they can only be accessed by Friends of member functions and classes. classes that hide information declare their data members as private.

Protected members: They act like public to a derived class and private to other programs. In a class without C ++ inheritance, the protected and private controllers are no different.

6. shared inheritance, private inheritance, and protection inheritance

In public inheritance, the object of the derived class can access the public members in the base class, and the member functions of the derived class can access the public members and protected members in the base class.

In private inheritance, the base class members are invisible to all the members of the base class for the derived class objects.

For a derived class, the public and protected members of the base class are visible and can be accessed using member functions.

Private inheritance means that the member of the base class cannot be inherited by the later class.

When C ++ inheritance is protected, both the common and protected members of the base class serve as the protected members of the derived class, which means they can be accessed by "classes derived from the class later, but cannot be accessed outside the hierarchy.

7. Inheritance of virtual functions and virtual inheritance

These two concepts are confusing.

The inheritance of virtual functions is actually virtual functions. However, virtual functions must be used in C ++ inheritance. Otherwise, they do not need to exist. It refers to a dynamic binding process.

Virtual inheritance is a special concept in Multi-inheritance. It refers to retaining only the parent class of the parent class in the subclass ...... So that the subclass contains the same member of the parent class multiple times.

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.