Home Common Problem What is the default inheritance method for derived classes?

What is the default inheritance method for derived classes?

Aug 21, 2019 pm 01:51 PM
Derived class inherit

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>]
      {
              <派生类新增的数据成员和成员函数定义>
      };
Copy after login

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;
}
Copy after login

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;
 }
Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1261
29
C# Tutorial
1234
24
Detailed explanation of C++ function inheritance: How to use 'base class pointer' and 'derived class pointer' in inheritance? Detailed explanation of C++ function inheritance: How to use 'base class pointer' and 'derived class pointer' in inheritance? May 01, 2024 pm 10:27 PM

In function inheritance, use "base class pointer" and "derived class pointer" to understand the inheritance mechanism: when the base class pointer points to the derived class object, upward transformation is performed and only the base class members are accessed. When a derived class pointer points to a base class object, a downward cast is performed (unsafe) and must be used with caution.

Detailed explanation of C++ function inheritance: How to debug errors in inheritance? Detailed explanation of C++ function inheritance: How to debug errors in inheritance? May 02, 2024 am 09:54 AM

Inheritance error debugging tips: Ensure correct inheritance relationships. Use the debugger to step through the code and examine variable values. Make sure to use the virtual modifier correctly. Examine the inheritance diamond problem caused by hidden inheritance. Check for unimplemented pure virtual functions in abstract classes.

Solve PHP error: problems encountered when inheriting parent class Solve PHP error: problems encountered when inheriting parent class Aug 17, 2023 pm 01:33 PM

Solving PHP errors: Problems encountered when inheriting parent classes In PHP, inheritance is an important feature of object-oriented programming. Through inheritance, we can reuse existing code and extend and improve it without modifying the original code. Although inheritance is widely used in development, sometimes you may encounter some error problems when inheriting from a parent class. This article will focus on solving common problems encountered when inheriting from a parent class and provide corresponding code examples. Question 1: The parent class is not found. During the process of inheriting the parent class, if the system does not

Calculate interest on fixed deposits (FDs) and fixed deposits (RDs) using inherited Java program Calculate interest on fixed deposits (FDs) and fixed deposits (RDs) using inherited Java program Aug 20, 2023 pm 10:49 PM

Inheritance is a concept that allows us to access the properties and behavior of one class from another class. The class that inherits methods and member variables is called a superclass or parent class, and the class that inherits these methods and member variables is called a subclass or subclass. In Java, we use "extends" keyword to inherit a class. In this article, we will discuss a Java program to calculate interest on fixed deposits and time deposits using inheritance. First, create these four Java files - Acnt.java − in your local machine IDE. This file will contain an abstract class ‘Acnt’ which is used to store account details like interest rate and amount. It will also have an abstract method 'calcIntrst' with parameter 'amnt' for calculating

Detailed explanation of C++ function inheritance: How to understand the 'is-a' and 'has-a' relationship in inheritance? Detailed explanation of C++ function inheritance: How to understand the 'is-a' and 'has-a' relationship in inheritance? May 02, 2024 am 08:18 AM

Detailed explanation of C++ function inheritance: Master the relationship between "is-a" and "has-a" What is function inheritance? Function inheritance is a technique in C++ that associates methods defined in a derived class with methods defined in a base class. It allows derived classes to access and override methods of the base class, thereby extending the functionality of the base class. "is-a" and "has-a" relationships In function inheritance, the "is-a" relationship means that the derived class is a subtype of the base class, that is, the derived class "inherits" the characteristics and behavior of the base class. The "has-a" relationship means that the derived class contains a reference or pointer to the base class object, that is, the derived class "owns" the base class object. SyntaxThe following is the syntax for how to implement function inheritance: classDerivedClass:pu

How to use polymorphism and inheritance in PHP to deal with data types How to use polymorphism and inheritance in PHP to deal with data types Jul 15, 2023 pm 07:41 PM

How to use polymorphism and inheritance to handle data types in PHP Introduction: In PHP, polymorphism and inheritance are two important object-oriented programming (OOP) concepts. By using polymorphism and inheritance, we can handle different data types more flexibly. This article will introduce how to use polymorphism and inheritance to deal with data types in PHP, and show their practical application through code examples. 1. The basic concept of inheritance Inheritance is an important concept in object-oriented programming. It allows us to create a class that can inherit the properties and methods of the parent class.

How to force inheritance of proxy final class using Java? How to force inheritance of proxy final class using Java? Sep 06, 2023 pm 01:27 PM

How to force inheritance of proxy final class using Java? In Java, the final keyword is used to modify classes, methods, and variables, indicating that they cannot be inherited, overridden, or modified. However, in some cases, we may need to force inheritance of a final class to achieve specific needs. This article will discuss how to use the proxy pattern to implement such functionality. The proxy pattern is a structural design pattern that allows us to create an intermediate object (proxy object) that can control the behavior of another object (proxy object).

Packaging technology and application in PHP Packaging technology and application in PHP Oct 12, 2023 pm 01:43 PM

Encapsulation technology and application encapsulation in PHP is an important concept in object-oriented programming. It refers to encapsulating data and operations on data together in order to provide a unified access interface to external programs. In PHP, encapsulation can be achieved through access control modifiers and class definitions. This article will introduce encapsulation technology in PHP and its application scenarios, and provide some specific code examples. 1. Encapsulated access control modifiers In PHP, encapsulation is mainly achieved through access control modifiers. PHP provides three access control modifiers,