对C++下struct 和 类默认继承的认识

 1 #include <iostream>
 2 using namespace std;
 3 struct  struct1{
 4     int data1 ;
 5     double data2 ;
 6     struct1(){
 7         (*this).data1 = data1 ;
 8         (*this).data2 = data2 ;
 9         cout<<"this is output by struct1 function !";
10     }
11     public : 
12     void privateFunc(){
13         cout<<"this is private function , so this massage should never show in console ! \n";
14     }
15 };
16 struct struct2 : struct1{
17     /*
18     
19     according the result in console , we can get the conclusion :
20     
21     the default inherit method of struct is public 
22 
23     */
24 };
25 class class1 : public  struct1 {
26     /*
27     
28     if ignore 'public' identifiers , compile will show that privateFunc() in 35 line 
29     
30     can't access, this indicate that it is private inhert when subclass is a struct. 
31     
32     */
33     string name ;
34     public :
35     class1(){
36         name = "i'm a class !";
37         cout<<"this is output by class1 !";
38     }
39 };
40 class class2{
41     public :
42     void showClass2(){
43         cout<<"this is output by class2 ! \n ";
44     }
45 };
46 class class3 : class2{
47     /*
48 
49     according to the result : showClass2 can't accessiable can prove class default 
50     
51     inherit method is private 
52 
53     */
54 };
55 struct struct4 : class2{
56     /*
57 
58     default inherit is public 
59     
60     */
61 };
62 class class4 : struct1 {
63     /*
64 
65     default inherit is private 
66     */
67 }
68 int main(){
69     //struct2 onestruct2 ;
70     //onestruct2.privateFunc();    
71     /* test class default inherit */
72     //class3 * oneclass3 = class3;
73     //oneclass3->showClass2();
74     /* test struct inherit class */
75     //struct4 onestruct4 ;
76     //onestruct4.showClass2();
77     class4 * oneclass4;
78     oneclass4->privateFunc();
79     return 0;
80 }

 

posted @ 2014-04-11 23:53  Birding  阅读(980)  评论(1编辑  收藏  举报