Private Inheritance for C++

  C++ has a second means of implementing the has-a relationship: private inheritance.With private inheritance, public and protected members of the base class become private members of the derived class.This means the methods of the base class do not become part of the public interface of the derived object.They can be used, however, inside the member functions of the derived class.
Let’s look at the interface topic more closely.With public inheritance, the public methods of the base class become public methods of the derived class. In short, the derived class inherits the base-class interface.This is part of the is-a relationship.With private inheritance, the public methods of the base class become private methods of the derived class. In short, the derived class does not inherit the base-class interface.As you saw with contained objects, this lack of inheritance is part of the has-a relationship.

from 《c++ primer plus》

时间: 2024-10-12 16:15:21

Private Inheritance for C++的相关文章

条款39:明智而审慎地使用private继承(use private inheritance judiciously)

NOTE: 1.private 继承意味 is-implemented-in-terms-of(根据某物实现出).它通常比复合(composition)的级别低.但是当derivated class需要访问protected base class的 成员,或需要重新定义继承而来的virtual函数时,这么设计是合理的. 2.和复合(composition)不同,private 继承可以造成empty base 最优化.这对致力于“对象尺寸最小化”的程度开发者而言,可能很重要.

C++ inheritance examples ZZ

1 c++继承经典例子 2 #include <iostream.h> 3 class Base 4 { 5 private: 6 int b_number; 7 public: 8 Base( ){} 9 Base(int i) : b_number (i) { } 10 int get_number( ) {return b_number;} 11 void print( ) {cout << b_number << endl;} 12 }; 13 14 class

c++ private 继承

继承相关知识: private继承对派生类不可见(exist but not accessable) private 和protect 继承不允许基类指针指向派生类(编译error: 'BaseClass' is an inaccessible base of 'DerivedClass' reinterpret_cast可以将private继承的派生类指针强制转化为基类指针(通过pbase = static_cast <BaseClass*>(pdrived);但是不要这么做,因为违反了pr

Cocos2d-x AppDelegate

//AppDelegate.h ifndef  _APP_DELEGATE_H_ #define  _APP_DELEGATE_H_ #include "cocos2d.h" /** @brief    The cocos2d Application. The reason for implement as private inheritance is to hide some interface call by Director. */ class  AppDelegate : pr

Android cocos2dx游戏开发——示例程序HelloCpp源码分析

本文通过分析cocos2dx提供的示例程序HelloCpp来分析cocos2dx的启动过程. 我们从HelloCpp.java开始: [java] view plaincopyprint? package org.cocos2dx.hellocpp; import org.cocos2dx.lib.Cocos2dxActivity; import android.os.Bundle; public class HelloCpp extends Cocos2dxActivity{ protecte

C++继承经典样例

c++继承经典样例 #include <iostream.h> class Base { private: int b_number; public: Base( ){} Base(int i) : b_number (i) { } int get_number( ) {return b_number;} void print( ) {cout << b_number << endl;} }; class Derived : public Base { private:

HelloWorld入门

HelloWorld入门    代码解释:        AppDelegate.h类是Cocos2d-x引擎要求实现的游戏应用委托对象,在Cocos2d-x游戏    运行的不同生命周期阶段会触发他的不同函数        AppDelegate继承了cocos2d::Application,是Cocos2d-x引擎提供的基类 AppDelegate.h 1 #ifndef _APP_DELEGATE_H_ 2 #define _APP_DELEGATE_H_ 3 4 #include "co

C++primer第十五章. 面向对象编程

面向对象编程基于三个基本概念:数据抽象.继承和动态绑定. 15.1. 面向对象编程:概述 面向对象编程的关键思想是多态性(polymorphism). 之所以称通过继承而相关联的类型为多态类型,是因为在许多情况下可以互换地使用派生类型或基类型的“许多形态”.正如我们将看到的,在 C++ 中,多态性仅用于通过继承而相关联的类型的引用或指针. 继承 派生类(derived class)能够继承基类(baseclass)定义的成员,派生类可以无须改变而使用那些与派生类型具体特性不相关的操作,派生类可以

Effective c++(笔记)之继承关系与面向对象设计

1.公有继承(public inheritance) 意味着"是一种"(isa)的关系 解析:一定要深刻理解这句话的含义,不要认为这大家都知道,本来我也这样认为,当我看完这章后,就不这样认为了. 公有继承可以这样理解,如果令class D以public 的形式继承了class B ,那么可以这样认为,每一个类型为D的对象同时也可以认为是类型为B的对象,但反过来是不成立的,对象D是更特殊化更具体的的概念,而B是更一般化的概念,每一件事情只要能够施行于基类对象身上,就一定可以应用于派生类对