c++中的继承类_C ++中的继承

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.

继承是一类从另一类获取属性和特性的能力。 其属性被其他类继承的类称为ParentBaseSuper类。 并且,它继承其他类的属性的类被称为儿童派生类。

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++)

  1. Code Reusability

    代码可重用性

  2. Method Overriding (Hence, Runtime Polymorphism.)

    方法覆盖(因此,运行时多态。)

  3. 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上使用该概念。 这是一个简单的例子,

Example for Inheritance in C++
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 ClassDerived ClassDerived Class
Base classPublic ModePrivate ModeProtected Mode
PrivateNot InheritedNot InheritedNot Inherited
ProtectedProtectedPrivateProtected
PublicPublicPrivateProtected
派生类 派生类 派生类
基类 公共模式 私人模式 保护模式
私人的 不继承 不继承 不继承
受保护的 受保护的 私人的 受保护的
上市 上市 私人的 受保护的

翻译自: https://www.studytonight.com/cpp/overview-of-inheritance.php

c++中的继承类

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值