c++中的继承类
Inheritance is the capability of one class to acquire properties and characteristics from another class. The class whose properties are inherited by other class is called the Parent or Base or Super class. And, the class which inherits properties of other class is called Child or Derived or Sub class.
继承是一类从另一类获取属性和特性的能力。 其属性被其他类继承的类称为Parent或Base或Super类。 并且,它继承其他类的属性的类被称为儿童或派生或子类。
Inheritance makes the code reusable. When we inherit an existing class, all its methods and fields become available in the new class, hence code is reused.
继承使代码可重用。 当我们继承一个现有的类时,它的所有方法和字段都将在新类中可用,因此代码被重用。
NOTE: All members of a class except Private, are inherited
注意:类的所有成员(不包括私有)都被继承
C ++中继承的目的 (Purpose of Inheritance in C++)
Code Reusability
代码可重用性
Method Overriding (Hence, Runtime Polymorphism.)
方法覆盖(因此,运行时多态。)
Use of Virtual Keyword
虚拟关键字的使用
继承的基本语法 (Basic Syntax of Inheritance)
class Subclass_name : access_mode Superclass_name
While defining a subclass like this, the super class must be already defined or atleast declared before the subclass declaration.
在定义这样的子类时,必须已定义超类或在子类声明之前至少声明了父类。
Access Mode is used to specify, the mode in which the properties of superclass will be inherited into subclass, public, privtate or protected.
访问模式用于指定将超类的属性继承为子类,公共,私有或受保护的模式。
继承的例子 (Example of Inheritance)
Whenever we want to use something from an existing class in a new class, we can use the concept on Inheritace. Here is a simple example,
每当我们想在新类中使用现有类中的内容时,都可以在Inheritace上使用该概念。 这是一个简单的例子,
class Animal
{
public:
int legs = 4;
};
// Dog class inheriting Animal class
class Dog : public Animal
{
public:
int tail = 1;
};
int main()
{
Dog d;
cout << d.legs;
cout << d.tail;
}
4 1
4 1
访问修饰符和继承:类成员的可见性 (Access Modifiers and Inheritance: Visibility of Class Members)
Depending on Access modifier used while inheritance, the availability of class members of Super class in the sub class changes. It can either be private, protected or public.
根据继承时使用的Access修饰符,子类中Super类的类成员的可用性会发生变化。 它可以是私有的,受保护的或公共的。
1)公共继承 (1) Public Inheritance)
This is the most used inheritance mode. In this the protected member of super class becomes protected members of sub class and public becomes public.
这是最常用的继承模式。 在这种情况下,超类的受保护成员成为子类的受保护成员,而公众则成为公共。
class Subclass :public Superclass
2)私有继承 (2) Private Inheritance)
In private mode, the protected and public members of super class become private members of derived class.
在私有模式下,超类的受保护成员和公共成员成为派生类的私有成员。
class Subclass : Superclass // By default its private inheritance
3)受保护的继承 (3) Protected Inheritance)
In protected mode, the public and protected members of Super class becomes protected members of Sub class.
在保护模式下,Super类的公共成员和受保护成员成为Sub类的受保护成员。
class subclass :protected Superclass
该表显示了所有可见性模式 (Table showing all the Visibility Modes)
Derived Class | Derived Class | Derived Class | |
---|---|---|---|
Base class | Public Mode | Private Mode | Protected Mode |
Private | Not Inherited | Not Inherited | Not Inherited |
Protected | Protected | Private | Protected |
Public | Public | Private | Protected |
派生类 | 派生类 | 派生类 | |
---|---|---|---|
基类 | 公共模式 | 私人模式 | 保护模式 |
私人的 | 不继承 | 不继承 | 不继承 |
受保护的 | 受保护的 | 私人的 | 受保护的 |
上市 | 上市 | 私人的 | 受保护的 |
翻译自: https://www.studytonight.com/cpp/overview-of-inheritance.php
c++中的继承类