设为首页
加入收藏
首页
C语言
C++
面试
Linux
函数
Windows
数据库
下载
搜索
我要投稿
全站搜索
文章
图片
软件
视频
商品
FLASH
产品
高级搜索
当前位置:
首页
->
基础
->
c++编程基础
TOP
C++语言笔记系列之十二――C++的继承
2015-07-24 05:50:36
来源:
作者: 【
大
中
小
】 浏览:
4
次
Tags:
语言
笔记
系列
十二
继承
C++
的继承
1.继承方式
public(公有继承)
派生类中的成员可以访问基类的public成员和protected成员,但不能访问基类的private成员。
派生类的对象只能访问基类的public成员。
protected(保护继承),private(私有继承)
派生类中的成员可以访问基类的public成员和protected成员,但不能访问基类的private成员。
派生类的对象不能访问基类的任何成员。
2.例子
example 1:
#include
class A
{
public:
void fun1(int a) {cout<
void fun2(int b) {cout<
};
class B:public A
{
public:
void fun3() {cout<<"It is in class B."<
};
int main()
{
B b;
A a;
b.fun3(); //Y(正确)
b.fun2(); //Y
b.fun1(); //Y
a.fun3(); //N(错误)
a.fun2(); //Y
a.fun1(); //Y
}
example2:
#include
class A
{
public:
void f1();
A() {i1 = 10; j1 = 11;}
protected:
int j1;
private:
int i1;
};
class B:public A
{
public:
void f2();
B() {i2 = 20; j2 = 21;}
protected:
int j2;
private:
int i2;
};
class C:public B
{
public:
void f3();
C() {i3 = 30; j3 = 31;}
protected:
int j3;
private:
int i3;
};
以下说法:
(1)派生类B中的成员f2()可以访问类A中的成员f1()(Y)、i1(N)、j1(Y)。
(2)派生类对象B能够访问类A的成员f1()(Y)、i1(N)、j1(N)。
(3)派生类C中的成员函数f3()能否访问直接基类B中的成员f2()(Y)、i2(N)、j2(Y);能否访问间接基类A中的f1()(Y)、i1(N)、j1(Y)。
(4)派生类对象C可否访问f2()(Y)、i2(N)、j2(N);可否访问i1(N)、f1()(Y)、j1(N)。
注:类可以直接访问类中的private、protected以及public成员;类的对象只可以直接访问类中的public。
【
大
中
小
】【
打印
】
【
繁体
】【
投稿
】【
收藏
】 【
推荐
】【
举报
】【
评论
】 【
关闭
】 【
返回顶部
】
分享到:
上一篇
:
POJ 2407 Relatives 欧拉函数题解
下一篇
:
LeetCode――Roman to Integer
评论
帐 号:
密码:
(
新用户注册
)
验 证 码:
表 情:
内 容:
Copyright@https://www.cppentry.com all rights reserved
粤ICP备13067022号-3
Powered by
qibosoft V7.0
Code © 2003-11
qibosoft