You can use a variable whose type is a base class to reference instances of a derived class.

However, using the variable whose type is the base class, you can use it to access only members of the base class and not members of the derived class.

In the example below, we have two instances of the Terrier class, which derives from Dog. One instance is referenced by a variable of type Terrier. Using this variable, you have access to all members of the Terrier class. On the other hand, the variable whose type is Dog can only reference members of the Dog class, even though the reference points to an instance of a Terrier.

 Terrier bubba = new Terrier("Bubba", , "Happy");
bubba.Growl(); // Can call Terrier.Growl Dog jack = new Terrier("Jack", , "Surly");
jack.Growl(); // ERROR: Can't call Growl method

原文地址:#335 - Accessing a Derived Class Using a Base Class Variable

【转载】#335 - Accessing a Derived Class Using a Base Class Variable的更多相关文章

  1. (转载)1248 - Every derived table must have its own alias

    (转载)http://hi.baidu.com/lylegend13/item/a79f17eb51f5dff7e0a5d43b 1. select count(distinct CName) fro ...

  2. 如何实现 Copying derived entities using only base class pointer

    #include <iostream> struct CloneableBase { ; }; template<class Derived> struct Cloneable ...

  3. 关于C#你应该知道的2000件事

    原文 关于C#你应该知道的2000件事 下面列出了迄今为止你应该了解的关于C#博客的2000件事的所有帖子. 帖子总数= 1,219 大会 #11 -检查IL使用程序Ildasm.exe d #179 ...

  4. 【转载】#330 - Derived Classes Do Not Inherit Constructors

    A derived class inherits all of the members of a base class except for its constructors. You must de ...

  5. [转载] google mock cookbook

    原文: https://code.google.com/p/googlemock/wiki/CookBook Creating Mock Classes Mocking Private or Prot ...

  6. C++ 强制类型转换(转载)

    转载自:http://www.weixueyuan.net/view/6329.html 在C++语言中新增了四个关键字static_cast.const_cast.reinterpret_cast和 ...

  7. 【转载】C++中的基类与派生类

    转自:http://www.cnblogs.com/sujz/articles/2044365.html 派生类的继承方式总结: 继承方式 说明 public 基类的public和protected的 ...

  8. [C++] OOP - Base and Derived Classes

    There is a base class at the root of the hierarchy, from which the other class inherit, directly or ...

  9. 【转载】#344 - Hidden Base Class Member Is Invoked Based on Declared Type of Object

    When you use the new modifier to hide a base class method, it will still be called by objects whose ...

随机推荐

  1. 1040 有几个PAT (25 分

    字符串 APPAPT 中包含了两个单词 PAT,其中第一个 PAT 是第 2 位(P),第 4 位(A),第 6 位(T):第二个 PAT 是第 3 位(P),第 4 位(A),第 6 位(T). 现 ...

  2. TensorFlow入门测试程序

    import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data mnist=input_data. ...

  3. Go语言基础之15--文件基本操作

    一.文件读写 1.1 os.File os.File封装所有文件相关操作, 是一个自定义的struct. a. 打开一个文件进行读操作: os.Open(name string) (*File, er ...

  4. div css 伪类 不固定图片大小 居中, css div 实现三角形

    div css 伪类 不固定图片大小 居中 <style> .pic_box{width:300px; height:300px; background-color:#beceeb; fo ...

  5. python3 杂记

    python3 杂记 test001 --test001.py       ( from test2.test002 import * def test1(): print('1') if __nam ...

  6. 进入wordpress中的模板文件

    Wordpress的页面结构 一个简单的wordpress由 头部.内容.页脚组成,每个块中的每一个都由当前wordpress主题中模板文件生成. eg: 头:包含重要的信息,一般都是网页的头部. 内 ...

  7. 在vue2.x中安装sass并配置

    在vue中安装sass先检查系统中有没有安装sass,在命令行中输入 sass -v 表示sass在电脑中已有,否者可以参考我这篇博客安装Sass遇到的坑 一.先安装sass cmd打开命令行,到项目 ...

  8. ubuntu15.04 xampp 安装memcache

    要ThinkPHP3.2里由于要用到 memcache 缓存, 如果没有配置memcache,会报错,说系统支持 memcache. 所在配置的时候有点问题,现在解决了,现把它记录下来,以便以后查阅之 ...

  9. Python collections

    #count对象 Only 2.7 from collections import Counter #统计字母出现的次数 Counter('hello world') Counter(['red', ...

  10. Sublime Text格式化HTML JS CSS代码

    Sublime Text是开发Hybrid应用的神器,但是有时候对糟糕的代码格式很懊恼,尤其是团队成员比较多,并且代码风格不是很统一的时候.幸好有可用的格式化插件,比较好用的就是HTML-CSS-JS ...