C++继承

 

 

  C++继承可以是单一继承多重继承,每一个继承连接可以是public,protected,private也可以是virtualnon-virtual。然后是各个成员函数选项可以是virtual或non-virtualpure virtual。本文仅仅作出一些关键点的验证。

  public继承,例如下:

1 class base
2 {...}
3 class derived:public base
4 {...}
  如果这样写,编译器会理解成类型为derived的对象同时也是类型为base的对象,但类型为base的对象不是类型为derived的对象。这点很重要。那么函数形参为base类型适用于derived形参为derived不适用于base。下面是验证代码,一个参数为base的函数,传入derived应该成功执行,相反,一个参数为derived的函数
复制代码
 1 #include <iostream>
 2 #include <stdio.h>
 3 
 4 class base
 5 {
 6     public:
 7     base()
 8     :baseName(""),baseData(0)
 9     {}
10     
11     base(std::string bn,int bd)
12     :baseName(bn),baseData(bd)
13     {}
14     
15     std::string getBaseName() const
16     {
17         return baseName;
18     }
19     
20     int getBaseData()const
21     {
22         return baseData;
23     }
24     
25     private:
26         std::string baseName;
27         int baseData;
28 };
29 
30 class derived:public base
31 {
32     public:
33         derived():base(),derivedName("")
34         {}
35         derived(std::string bn,int bd,std::string dn)
36         :base(bn,bd),derivedName(dn)
37         {}
38         std::string getDerivedName() const
39         {
40             return derivedName;
41         }
42     private:
43         std::string derivedName;
44 };
45 
46 void show(std::string& info,const base& b)
47 {
48     info.append("Name is ");
49     info.append(b.getBaseName());
50     info.append(", baseData is ");
51     char buffer[10];
52     sprintf(buffer,"%d",b.getBaseData());
53         info.append(buffer);
54 }
55 
56 int main(int argc,char* argv[])
57 {
58     base b("test",10);
59     std::string s;
60     show(s,b);
61     std::cout<<s<<std::endl;
62     derived d("btest",5,"dtest");
63     std::string ss;
64     show(ss,d);
65     std::cout<<ss<<std::endl;
66     return 0;
67 }
复制代码

运行结果为:

base:baseName is test, baseData is 10
base:baseName is btest, baseData is 5

下面改改代码,将函数参数变为derived

复制代码
void show2(std::string& info,const derived& d)
{
    info.append("Name is ");
    info.append(d.getBaseName());
    info.append(", baseData is ");
    char buffer[10];
    sprintf(buffer,"%d",d.getBaseData());
    info.append(buffer);
}
复制代码

调用show(ss,d);编译器报错

1 derived_class.cpp: In function `int main(int, char**)':
2 derived_class.cpp:84: error: invalid initialization of reference of type 'const derived&' from expression of type 'base'
3 derived_class.cpp:70: error: in passing argument 2 of `void show2(std::string&, const derived&)'

第二点对各种形式的继承作出验证,首先给出表格

继承方式\成员类型 public protected private
public public protected 无法继承
protected protected protected 无法继承
private private private 无法继承

这里解释一下,这里仅仅表达基类的成员,被public,protected,private三种方式继承后,在原基类为public,protectedc,private的成员在继承类里类型为表格里内容

复制代码
 1 class base
 2 {
 3     public:
 4         std::string testPublic()
 5         {
 6             return std::string("this is public base");
 7         }
 8     protected:
 9         std::string testProtected()
10         {
11             return std::string("this is protected base");
12         }
13     private:
14         std::string testPrivate()
15         {
16             return std::string("this is private base");
17         }
18 };
19 
20 class derivedPublic:public base
21 {
22     public:
23         std::string testPubPublic()
24         {
25             return testPublic()+= "in derived";
26         }
27         
28         std::string testProPublic()
29         {    
30             return testProtected()+= "in derived";
31         }
32         
33         std::string testPriPublic()                   
34         {    
35             return testPrivate()+= "in derived";
36         }
37 };
38 
39 int main(int argc,char* argv[])
40 41     derivedPublic dpub;
42     std::cout << dpub.testPublic() << std::endl; 
43
复制代码

报下面错误,说明testPrivate()不是derived私有函数而是base的私有函数

derived11.cpp:16: error: `std::string base::testPrivate()' is private
derived11.cpp:36: error: within this context

这样验证private类型成员无法被继承(public,private,protected)注:private,protected略去不做证明

下面只要验证 testProtected 能被第三层继承类继承,但是无法被第三层类直接调用就说明是public继承后继承类型为protected,而基类为Public类型成员则即可被继承又可以直接调用。

复制代码
 1 #include <iostream>
 2 #include <string>
 3 
 4 class base
 5 {
 6     public:
 7         std::string testPublic()
 8         {
 9             return std::string("this is public base");
10         }
11     protected:
12         std::string testProtected()
13         {
14             return std::string("this is protected base");
15         }
16     private:
17         std::string testPrivate()
18         {
19             return std::string("this is private base");
20         }
21 };
22 
23 class derivedPublic:public base
24 {
25     public:
26         std::string testPubPublic()
27         {
28             return testPublic()+= "in derived";
29         }
30         
31         std::string testProPublic()
32         {    
33             return testProtected()+= "in derived";
34         }
35         
36 //        std::string testPriPublic()                   
37 //        {    
38 //            return testPrivate()+= "in derived";
39 //        }
40 };
41 
42 class deepDerived:public derivedPublic
43 {
44     public:
45         std::string deepProtected()
46         {
47             return testProtected() +="in deep";
48         }
49         
50         std::string deepPublic()
51         {
52             return testPublic() +="indeep";
53         }
54 };
55 
56 int main(int argc,char* argv[])
57 {
58     derivedPublic dpub;
59     std::cout << dpub.testProtected() << std::endl; 
60     deepDerived deepdpub;
61     std::cout<<deepdpub.testPublic() <<std::endl;
62     std::cout<<deepdpub.testProtected() <<std::endl;
63     std::cout<<deepdpub.deepProtected() <<std::endl;
64     std::cout<<deepdpub.deepPublic() <<std::endl;
65 }
复制代码

这里服务器报错

derived12.cpp:13: error: `std::string base::testProtected()' is protected
derived12.cpp:62: error: within this context
这样就验证了一个是public,一个是protected,protected是不能直接调用的,但是被继承后是可以被public成员调用的。
下面的已经证明,详细步骤就略去如果对该部分验证感兴趣,可以看下面代码。
View Code

 

posted @ 2013-05-16 18:05  herizai  阅读(167)  评论(0编辑  收藏  举报