码迷,mamicode.com
首页 > 编程语言 > 详细

c++继承中的对象模型

时间:2019-09-26 21:12:47      阅读:81      评论:0      收藏:0      [点我收藏+]

标签:对象模型   mic   类继承   end   std   sizeof   info   代码   main   

我们知道继承方式有三种

public,protect,private

不同继承方式,在子类中有些是不可以访问的

那么我们想知道到底子类继承了多少?

看代码做一下验证

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 class father
 5 {
 6 public:
 7     int a;
 8 protected:
 9     int b;
10 private:
11     int c;
12 };
13 
14 class son1:public father
15 {
16 public:
17     int d;
18 };
19 
20 class son2:protected father
21 {
22 public:
23     int d;
24 };
25 
26 class son3:private father
27 {
28 public:
29     int d;
30 };
31 void test()
32 {
33     son1 s1;
34     cout << "size of s1 is " << sizeof(s1) << endl;
35 
36     son2 s2;
37     cout << "size of s2 is " << sizeof(s2) << endl;
38 
39     son3 s3;
40     cout << "size of s3 is " << sizeof(s3) << endl;
41 }
42 
43 int main()
44 {
45     test();
46     return 0;
47 }

技术图片

无论何种继承都是16,也就是说父类的东西都继承下来了。只是有些访问权限

c++继承中的对象模型

标签:对象模型   mic   类继承   end   std   sizeof   info   代码   main   

原文地址:https://www.cnblogs.com/mch5201314/p/11594007.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!