C++ //继承同名成员处理方式
2024-09-06 17:12:50
1 #include <iostream>
2 #include <string>
3 using namespace std;
4
5 class Base
6 {
7 public:
8 Base()
9 {
10 m_A = 100;
11 }
12 void func()
13 {
14 cout << "Base -func()调用" << endl;
15 }
16 int m_A;
17
18 void func(int a)
19 {
20 cout << "Base -func(int a)调用" << endl;
21 }
22
23
24 };
25
26 class Son : public Base
27 {
28 public:
29 Son()
30 {
31 m_A = 200;
32 }
33
34 void func()
35 {
36 cout << "Son -func()调用" << endl;
37 }
38
39 void func(int a)
40 {
41 cout << "Son -func(int a)调用" << endl;
42 }
43
44
45 int m_A;
46 };
47
48 //同名成员属性处理
49 void test01()
50 {
51 Son s;
52 cout << "Son m_A= " << s.m_A << endl;
53 //Base b;
54 //如果通过子类对象 访问到父类中的同名对象 要在子类后面加 父类的作用域
55 cout << "Base m_A= " << s.Base::m_A << endl;
56 //cout << "Base m_A= " << b.m_A << endl;
57 }
58
59 //同名成员函数处理
60 void test02()
61 {
62 Son s;
63 s.func(); //直接调用的是: 子类用的同名成员
64
65 //如何调用父类中的同名函数
66 s.Base::func();
67
68 //如果子类出现和父类中的同名的成员函数 子类的同名成员会隐藏掉
69 //父类中所有的同名成员函数
70
71 //如果想访问到父类中被隐藏的同名成员函数 需要加作用域
72 s.func(100);
73 s.Base::func(200);
74
75 }
76 int main()
77 {
78
79 //test01();
80 test02();
81
82 system("pause");
83
84 return 0;
85
86 }
最新文章
- windows下node环境配置
- java.lang.Class
- Winform 基本属性
- JSon_零基础_007_将JSon格式的";数组";字符串转换为Java对象";数组";
- Codeigniter 集成sphinx搜索 这里采用的是coreseek中文搜索引擎,具体安装请参考官方网站
- Javascript中的Keycode值列表
- 《C++ primer》--第1,2章小结
- 第十三章、学习 Shell Scripts 善用判断式
- careercup-树与图 4.8
- angularjs图片上传后不刷新的解决办法
- HDU2673:shǎ崽 OrOrOrOrz
- 第四弹:overfeat
- JS中new的自定义实现创建实例对象
- 无需超级用户mpi多机执行
- 五道java小题,补更四道java小题
- Python web框架对比
- php(curl请求)测试接口案例
- Node.js读取文件内容并返回值(非异步)
- android windows的一些item属性
- TensorRT简介-转载