深入理解C++中public、protected及private用法
来源:互联网 发布:淘宝客订单部分退款 编辑:程序博客网 时间:2024/06/05 19:43
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include<iostream>
#include<assert.h>
using
namespace
std;
class
A{
public
:
int
a;
A(){
a1 = 1;
a2 = 2;
a3 = 3;
a = 4;
}
void
fun(){
cout << a << endl;
//正确
cout << a1 << endl;
//正确
cout << a2 << endl;
//正确,类内访问
cout << a3 << endl;
//正确,类内访问
}
public
:
int
a1;
protected
:
int
a2;
private
:
int
a3;
};
int
main(){
A itema;
itema.a = 10;
//正确
itema.a1 = 20;
//正确
itema.a2 = 30;
//错误,类外不能访问protected成员
itema.a3 = 40;
//错误,类外不能访问private成员
system
(
"pause"
);
return
0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include<iostream>
#include<assert.h>
using
namespace
std;
class
A{
public
:
int
a;
A(){
a1 = 1;
a2 = 2;
a3 = 3;
a = 4;
}
void
fun(){
cout << a << endl;
//正确
cout << a1 << endl;
//正确
cout << a2 << endl;
//正确
cout << a3 << endl;
//正确
}
public
:
int
a1;
protected
:
int
a2;
private
:
int
a3;
};
class
B :
public
A{
public
:
int
a;
B(
int
i){
A();
a = i;
}
void
fun(){
cout << a << endl;
//正确,public成员
cout << a1 << endl;
//正确,基类的public成员,在派生类中仍是public成员。
cout << a2 << endl;
//正确,基类的protected成员,在派生类中仍是protected可以被派生类访问。
cout << a3 << endl;
//错误,基类的private成员不能被派生类访问。
}
};
int
main(){
B b(10);
cout << b.a << endl;
cout << b.a1 << endl;
//正确
cout << b.a2 << endl;
//错误,类外不能访问protected成员
cout << b.a3 << endl;
//错误,类外不能访问private成员
system
(
"pause"
);
return
0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include<iostream>
#include<assert.h>
using
namespace
std;
class
A{
public
:
int
a;
A(){
a1 = 1;
a2 = 2;
a3 = 3;
a = 4;
}
void
fun(){
cout << a << endl;
//正确
cout << a1 << endl;
//正确
cout << a2 << endl;
//正确
cout << a3 << endl;
//正确
}
public
:
int
a1;
protected
:
int
a2;
private
:
int
a3;
};
class
B :
protected
A{
public
:
int
a;
B(
int
i){
A();
a = i;
}
void
fun(){
cout << a << endl;
//正确,public成员。
cout << a1 << endl;
//正确,基类的public成员,在派生类中变成了protected,可以被派生类访问。
cout << a2 << endl;
//正确,基类的protected成员,在派生类中还是protected,可以被派生类访问。
cout << a3 << endl;
//错误,基类的private成员不能被派生类访问。
}
};
int
main(){
B b(10);
cout << b.a << endl;
//正确。public成员
cout << b.a1 << endl;
//错误,protected成员不能在类外访问。
cout << b.a2 << endl;
//错误,protected成员不能在类外访问。
cout << b.a3 << endl;
//错误,private成员不能在类外访问。
system
(
"pause"
);
return
0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include<iostream>
#include<assert.h>
using
namespace
std;
class
A{
public
:
int
a;
A(){
a1 = 1;
a2 = 2;
a3 = 3;
a = 4;
}
void
fun(){
cout << a << endl;
//正确
cout << a1 << endl;
//正确
cout << a2 << endl;
//正确
cout << a3 << endl;
//正确
}
public
:
int
a1;
protected
:
int
a2;
private
:
int
a3;
};
class
B :
private
A{
public
:
int
a;
B(
int
i){
A();
a = i;
}
void
fun(){
cout << a << endl;
//正确,public成员。
cout << a1 << endl;
//正确,基类public成员,在派生类中变成了private,可以被派生类访问。
cout << a2 << endl;
//正确,基类的protected成员,在派生类中变成了private,可以被派生类访问。
cout << a3 << endl;
//错误,基类的private成员不能被派生类访问。
}
};
int
main(){
B b(10);
cout << b.a << endl;
//正确。public成员
cout << b.a1 << endl;
//错误,private成员不能在类外访问。
cout << b.a2 << endl;
//错误, private成员不能在类外访问。
cout << b.a3 << endl;
//错误,private成员不能在类外访问。
system
(
"pause"
);
return
0;
}
1
2
3
4
5
6
7
int
main(){
cout <<
sizeof
(A) << endl;
cout <<
sizeof
(B) << endl;
system
(
"pause"
);
return
0;
}
1
2
3
4
5
6
7
8
9
int
main(){
B b(10);
cout << b.a << endl;
cout << b.A::a << endl;
system
(
"pause"
);
return
0;
}
0 0
- 深入理解C++中public、protected及private用法
- 深入理解C++中public、protected及private用法
- 深入理解c++中public,protected,及private用法。
- 深入理解C++中public、protected及private用法
- 深入理解C++中public、protected及private用法
- 深入理解C++中public、protected及private用法
- 深入理解C++中public、protected及private用法
- 深入理解C++中public、protected及private用法
- 深入理解C++中public、protected及private用法
- 深入理解C++中public、protected及private用法
- 深入理解C++中public、protected及private用法
- 深入理解C++中public、protected及private用法
- 深入理解C++中public、protected及private用法
- 深入理解C++中public、protected及private用法
- 深入理解C++中public、protected及private用法
- 深入理解C++中public、protected及private用法
- 深入理解C++中public、protected及private用法
- 深入理解C++中public、protected及private用法
- Queue Reconstruction by Height
- Genymotion ova文件下载过慢怎么办?
- LeetCode(6)
- C++抽象编程——STL实战(4)——计算集合的交集,并集及区别
- vagrant系列(1):使用vagrant在线/离线安装centos7
- 深入理解C++中public、protected及private用法
- Java基础回顾--String对象中常用的方法
- 作业4:杨辉三角&奇偶阶乘&数组平均数
- LeetCode98. Validate Binary Search Tree Add to List
- python 创建多级目录
- Maven项目中没有出错,但是项目有个红色的叉
- Atitit easyui翻页组件与vue的集成解决方案attilax总结
- Atitit 视图参数解决方案 oracle版和mysql版本 attilax总结.docx
- java类的三大特征