C++ Inheritance

本文详细介绍了C++中的继承机制,解释了基类如何将公共和受保护的成员传递给派生类,但不包括私有成员。文章还讨论了在继承过程中的一些常见误区,并提供了最佳实践建议。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 C++ Inheritance

Introduction

Inheritance is the way of inheriting some property of some existing class (which is called as Base class) in new class (which is called as Derived class). Derived class can inherit and expose public area of Base, and inherit protected area of Base, but can’’t inherit private area of Base.

While inheriting from a Base, Derived class’ s public functions won’t get access to Base’s private area. Derived class can access/modify private area of Base, only if Base has some public/protected members, which allow doing so. The reason for this restriction is obvious. By allowing so, this will lead to changing the state of a class just by inheriting it publicly. It’s the responsibility of Derived class to call the Base class’s constructor with proper initializes while constructing Derived class. By default, compiler will call the default constructor of its Base. Base class’s constructors can be called explicitly from initialization list, when we want to initialize them with some particular value.

Even though Derived class can inherit public/protected members of Base, it can’t inherit constructors, destructor and assignment operators of Base even if they are public. The reason is, these operations are Base specific. They construct/destruct/assign only Base part of a class. But these functions are silently called by the compiler from the Derived class’s constructor/destructor/assignment operator.

There is a general misconception that “Inheritance is for code re-use”. Here there are some tips on Inheritance:

  1. Inheritance is not for code re-use; it’s for flexibility. It is composition, which is for code re-use.
  2. Use inheritance only if doing so will remove if/else/switch statements from the caller of code.
  3. Inheritance will be more effective and useful with dynamic binding, that is, with virtual functions.
  4. Try hard to make all your Base classes as Abstract Base Class (ABC)
  5. Keep as little data as possible in ABC with most member functions as pure virtual. This will avoid situations where we need to inherit data/code along multiple paths.
  6. There is some overhead with Multiple Inheritance (MI). Compiler will have to add some more code when we refer a derived class object using its Base reference/pointer. This requires “this” pointer alignment. Still, it’s not a bad idea to use MI as long as it gives the efficiency. There are other alternatives for MI like Bridge pattern, nested generalization with its own advantage and disadvantage. Before using MI, think of those alternatives and go for the best one which suits your design.
<script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type=text/javascript> </script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值