简单继承(inheritance)的实现及访问限定
来源:互联网 发布:123d建模软件 编辑:程序博客网 时间:2024/06/03 19:28
泛化/具体化层次结构建立了一种is-a关系。例如,人是一种灵长目动物,灵长目动物是哺乳动物,哺乳动物是动物。
派生是一中表示is-a关系的方式,你从类Mammal派生出新类Dog。由于Dog类从Mammal类继承了运动功能,因此你不必显式说明狗能运动。
在已有的类的基础上添加了新功能的类被称为从原来的类派生而来,原来的类被称为新类的基类。
派生的语法格式如下:
class derivedClass : accessType baseClass
例如要创建一个名为Dog的从Mammal类派生而来的新类,可以这样做:
class Dog : public Mammal
下面是具体实现:
注意Mammal类中的数据成员是保护型(protected),该种访问限定类型的主要用途是允许其派生类的成员函数(方法)访问基类的保护数据成员和函数。
即三种访问限定符的区别如下:
如果在一个函数中声明了一个对象,如,Mammal bigAnimal;
则
公有(public):调用函数可以访问所有公有数据成员和函数。
cout << bigAnimal.getAge();
保护(protected):仅其派生类的成员函数可以访问。可以将Dog类的成员函数WagTail()改为如下:
void Dog::WagTail(){cout << "I'm " << itsAge << " years old. I am wagging tail..../n";}
私有(private):仅bigAnimal的成员函数可以使用,调用函数和派生类函数均不能访问。
- 简单继承(inheritance)的实现及访问限定
- 简单实现限定phpmyadmin访问ip的方法
- java的继承 inheritance
- C++ - 多层继承(inheritance) 的 使用 及 示例
- hibernate实现继承关系Inheritance
- JAVA多继承(multiple inheritance)的实现
- c++类的继承(inheritance)
- Cpp的继承Inheritance-笔记
- Python - 继承(Inheritance) 详解 及 代码
- Python - 继承(Inheritance) 详解 及 代码
- 继承 Inheritance
- inheritance(继承)
- Inheritance(继承)
- Inheritance继承
- Inheritance继承
- 继承的简单实现
- EXT 的继承 Manual:Intro:Inheritance
- EXT 的继承 Manual:Intro:Inheritance
- 微软是否已经输给了开源?
- 将Windows下编写的JAVA程序移植到Linux下的编码解决
- Ajax 使用XMLHttpRequest对象发送数据和接收处理XML源代码
- Registry values for use with the State
- JAVA SOCKET服务端接收C客户端字节长度的问题
- 简单继承(inheritance)的实现及访问限定
- Thread(二) --> 后台线程
- 正则表达式
- Joomla之路
- Project Management Orientation - Define the project team (Concepts -1)
- tomcat6配置JNDI数据库连接池常易出现的问题
- 确认删除用法
- VC从文件中加载图片
- TCP/IP编程基础——超时、多路复用、非阻塞