"C + +" in-depth understanding of the basics of inheritance and its relationship to access qualifiers

Source: Internet
Author: User
Tags inheritance
The relationship between inheritance mode and access qualifier

Three ways of inheriting:

    1. Public inheritance

    2. Private inheritance

    3. Protect inheritance

Relationship: The member function of the destructors class is private, and the derived class is not visible to the private members of the base class, and the other criteria are small in scope for the final access qualification.

Protect Member Qualifiers: Some base class members do not want to be accessed directly by the base class's objects, but need to be accessible in a derived class, and are defined as protected members. The protection member qualifier occurs because of inheritance.

Understanding hidden

Shadowing refers to a function of a derived class that masks a base class function with the same name. The rules are as follows:

    1. If a function of a derived class has the same name as a function of the base class, but the parameters are different, the function of the base class is hidden, regardless of the virtual keyword.

    2. If a function of a derived class has the same name as a function of the base class, and the parameters are the same, but the base class function does not have the virtual keyword, the function of the base class is hidden.

Default member functions for derived classes

In a derived class, if no six default member functions are displayed, the compilation system will default to the six member functions.

    1. constructor function

    2. Copy constructor

    3. Destructors

    4. Assignment operator overloading

    5. Fetch address operator overload

    6. Const-Modified FETCH address operator overloading

#include <iostream>using namespace Std;class person{Public:person (const char* name): _name (name) {    cout<< "person ()" <<endl;    } person (const person& p) {cout<< ' person (const person& p) ' <<endl; } person& operator= (const person& p) {cout<< "person& operator= (const person& p)" <<endl      ;      if (this!=&p) {_name=p._name;    } return *this;     } ~person () {cout<< "~person ()" <<endl; } protected:string _name;}; Class Student:public person{public:student (const char* name,int num):p erson (name), _num (num) {CO       ut<< "Student ()" <<endl; } student (const student& s):p Erson (s), _num (s._num) {cout<< ' student (const student& s    ) "<<endl; } student& operator= (const student& s) {cout<< "student& operator= (const student& p)" <<      Endl if (This!=& s) {person::operator= (s);//must specify domain, otherwise it will die loop _num=s._num;    } return *this;    } ~student () {//Refactor the subclass first, then clean up the parent class, and do not need to display the call cout<< "~student ()" <<endl; } private:int _num;};  int main () {student S1 ("Jack", 18);  Student S2 (S1);  Student S3 ("Rose", 16); S1=S3;}

Diamond Inheritance

There is ambiguity in diamond inheritance and the problem of data redundancy.

example, the data in the inheritance is two copies, each of which is different:

Workaround: Virtual inheritance

Add virtual to the second layer of the diamond

Cases:

Related articles:

C # Supplements's trivia (iv): inheritance

Basic knowledge of C #: Basic Knowledge (2) class

Related videos:

Thousand Front C language Basic video tutorial

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.