Home  >  Article  >  What is the default inheritance method for derived classes?

What is the default inheritance method for derived classes?

angryTom
angryTomOriginal
2019-08-21 13:51:3013673browse

What is the default inheritance method for derived classes?

Let me answer your questions first: There are two default inheritance methods for derived classes. Use class to define a derived class, and the default inheritance method is private. Use struct to define a derived class, and the default inheritance method is public.

Recommended tutorial: C Video tutorial

## Inheritance is the second major feature of object-oriented programming. Allows the creation of new classes based on existing classes. The new class can inherit the data members and member functions of the existing class, add its own unique data members and member functions, and redefine the member functions in the existing class. Using class inheritance and derivation to achieve a higher level of code reusability is in line with the ideas of modern software development.

The C language supports both single inheritance and multiple inheritance. Single inheritance means that a derived class inherits from only one base class; correspondingly, multiple inheritance means that a derived class inherits from two or more base classes at the same time. Java only supports single inheritance.

1. Derived class

The definition format of a derived class is as follows:

      class <派生类名>:[继承方式]<基类名1>
            [,[继承方式]<基类名2>,...,[继承方式]<基类名n>]
      {
              <派生类新增的数据成员和成员函数定义>
      };

Description:

(1) The keyword for defining a derived class can be class or struct. The difference between the two is: use class to define a derived class, the default inheritance method is private, use struct to define a derived class, the default inheritance method is public. The default attributes of newly added members are also class corresponding to private attributes and struct corresponding to public attributes.

(2) The two types of functions that cannot be inherited by a derived class are constructors and destructors.

2. Access attributes of base class members in derived classes under 3 inheritance methods

Inheritance descriptorParent public memberParent protected memberParent private memberpublicChild public member r ProteCted member # -# Protect 子 子 - privatechild private memberchild private member -
Use the following code to briefly understand:

#include "stdafx.h"
#include<iostream>
 using namespace std; 
 class Base
 {
 private:
     int priData;
 9 protected:
    int proData;
 public:
    int pubData;
 };

class D1:private Base//私有继承
 {
    void f1()
     {
        //priData=1;//基类private成员在派生类中不可直接访问
        proData=2;//基类的protected成员在派生类中为private访问属性
         pubData=3;//基类的public成员在派生类中为private访问属性
    }
 };
 class D2:protected Base//保护继承
 {
     void f2()
    {
         //priData=1;//基类private成员在派生类中不可直接访问
        proData=2;//基类的protected成员在派生类中为protected访问属性
         pubData=3;//基类的public成员在派生类中为protected访问属性
     }
 };
 
 class D3:public Base//公有继承
 {
    void f3()
     {
        //priData=1;//基类private成员在派生类中不可直接访问
         proData=2;//基类的protected成员在派生类中为protected访问属性
        pubData=3;//基类的public成员在派生类中为public访问属性
     }
 };
 
 int main()
 {
     Base obj;
    //obj.priData=1;//对象不可访问Base类中private成员
     //obj.proData=2;//对象不可访问Base类中protected成员
    obj.pubData=3;
   D1 objD1;
    //objD1.pubData=3;//private属性,不可访问
    D2 objD2;
     //objD2.pubData=3;//protected属性,不可访问
    D3 objD3;
     objD3.pubData=3;//public属性,可以访问
    return 0;
}

Although the private member functions of the base class are not directly accessible in the member functions of the derived class, the member functions of the derived class can be inherited by calling the base class function to access these members indirectly. If a function of the base class is inherited and remains a public member in the derived class, it can be called directly through the derived class object.

Let’s first look at the access attributes and functions of class members:

Access attributesFunctionprivateOnly allows access to member functions and friend functions of this class, and cannot be accessed by other functionsprotectedAllows both Access to member functions and friend functions of this class also allows access to member functions of its derived classespublicAllows access to member functions of this class as well as Access to other functions outside the class
Well, continue to understand through the code:

#include "stdafx.h"
 #include<iostream>
 using namespace std;
 
class Base
 {
 private:
     int priData;
 protected:
    int proData;
 public:
     int pubData;
 //在类的定义中不能对数据成员进行初始化
     void SetData()//为基类中的数据成员赋值
     {
         priData=100;
        proData=200;
         pubData=300;
    }
    void Print()
     {
        cout<<"priData="<<priData<<endl;
       cout<<"proData="<<proData<<endl;
        cout<<"pubData="<<pubData<<endl;
     }
 };

 class Derived:public Base
 {
 public:
     void ChangeData()
     {
         SetData();
         proData=12;//在派生类的成员函数类可以访问基类的非私有成员                   
     }
 };

 int main()
 {
    Base b;
     b.SetData();
     b.Print();
 
     Derived d1;
     d1.ChangeData();
     d1.pubData=13;
    d1.Print();
     
     return 0;
 }

The program running results are as follows:

What is the default inheritance method for derived classes?

The above is the detailed content of What is the default inheritance method for derived classes?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn