Chinaunix首页 | 论坛 | 博客
  • 博客访问: 80053
  • 博文数量: 10
  • 博客积分: 202
  • 博客等级: 入伍新兵
  • 技术积分: 221
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-12 20:03
个人简介

一枚老菜鸟 吐槽基地:http://xuxiake1981.blog.hexun.com/

文章分类
文章存档

2020年(1)

2017年(1)

2013年(3)

2012年(5)

分类: C/C++

2013-12-22 23:01:58

转载自:http://blog.csdn.net/gzshun/article/details/7289089

在继承过程中,从基类派生出派生类,可能出现重名的成员,包括数据成员或成员函数,这些属于作用域方面的内容。

一、基类的引用或指针访问派生类

基类的引用或指针只能访问派生类中属于基类的部分成员,不能访问派生类的部分,否则就会编译出错。

  1. #include   
  2.   
  3. using namespace std;  
  4.   
  5. class CBase  
  6. {  
  7. public:  
  8.     void BasePrint() const  
  9.     {  
  10.         cout << "调用基类的成员函数" << endl;  
  11.     }  
  12. };  
  13.   
  14. class CDerived : public CBase  
  15. {  
  16. public:  
  17.     void DerivedPrint() const  
  18.     {  
  19.         cout << "调用派生类的成员函数" << endl;  
  20.     }  
  21. };  
  22.   
  23. int main()  
  24. {  
  25.     CDerived obj;  
  26.     CBase *bObj = &obj;  
  27.     CDerived * dObj = &obj;  
  28.   
  29.     bObj->BasePrint();  
  30. //    bObj->DerivedPrint();//错误,基类指针不能调用派生类的部分  
  31.     dObj->BasePrint();  
  32.     dObj->DerivedPrint();  
  33.     return 0;  
  34. }  

执行结果:

调用基类的成员函数
调用基类的成员函数
调用派生类的成员函数



二、重复的数据成员
有时基类和派生类有同名的数据成员,甚至可能在基类和间接基类中都有同名的数据成员。
这种情况一般会出现在:从 由另一个程序员创建的基类中派生自己的类,可能不知道在基类中有什么私有数据成员,此时自己又创建了与基类当中同名的数据成员。
当然这种情况是不会阻碍继承的,编译器做了很多的工作。但这里主要是介绍如何从派生类中调用派生类的同名私有成员或者基类的私有成员。
例子:
CBase是CDerived的父类,它们拥有一个同名成员为mVal。
在派生类中
1.默认是访问自己的数据成员mVal;
2.若要访问基类的mVal,要用这种格式"<类名>::<数据成员>", CBase::mVal

  1. #include   
  2.   
  3. using namespace std;  
  4.   
  5. class CBase  
  6. {  
  7. public:  
  8.     CBase(int x = 120) : mVal(x) {}  
  9.     int mVal;  
  10. };  
  11.   
  12. class CDerived : public CBase  
  13. {  
  14. public:  
  15.     CDerived(int x = 100) : mVal(x) {}  
  16.     void PrintValue() const  
  17.     {  
  18.         cout << "犀利爹的年龄:" << CBase::mVal << endl  
  19.              << "犀利哥的年龄:" << mVal << endl;  
  20.     }  
  21.     int mVal;  
  22. };  
  23.   
  24. int main()  
  25. {  
  26.     CDerived human;  
  27.     human.PrintValue();  
  28.     return 0;  
  29. }  

执行结果:

犀利爹的年龄:120
犀利哥的年龄:100


三、重复的成员函数
基类与派生类若存在重复的成员函数,则有2种情况:
第一种情况:函数名相同,参数列表不同,如果将基类的成员函数的作用域引入派生类中,就是重载版本了
第二种情况:函数的所有方面都相同,包括函数名,参数列表与返回值

第一种情况:
函数名相同,参数列表不同,这跟函数的重载一样。但这并不是函数的重载,因为函数的重载必须在同一个作用域中定义,而基类和派生类定义了不同的作用域。在这里,可以使用using在派生类中声明基类的函数,将基类的函数的作用域引入派生类中的作用域,让这里的函数成为重载版本,来个小例子。


  1. #include   
  2.   
  3. using namespace std;  
  4.   
  5. class CBase  
  6. {  
  7. public:  
  8.     void Print(const char *str)  
  9.     {  
  10.         cout << "调用基类的打印函数: str = " << str << endl;  
  11.     }  
  12. };  
  13.   
  14. class CDerived : public CBase  
  15. {  
  16. public:  
  17.     using CBase::Print;  
  18.     void Print(int val)  
  19.     {  
  20.         cout << "调用派生类的打印函数: val = " << val << endl;  
  21.     }  
  22. };  
  23.   
  24. int main()  
  25. {  
  26.     CDerived obj;  
  27.     obj.Print(2);  
  28.     obj.Print("hello");  
  29.     return 0;  
  30. }  

执行结果:

调用派生类的打印函数: val = 2
调用基类的打印函数: str = hello


这里如果没有"using CBase::Print;"的声明,那么将会编译错误。

第二种情况:

函数的所有方面都相同,包括函数名,参数列表与返回值,在派生类中,要调用基类的同名成员函数,方法跟同名数据成员的方法一样,格式是:"<类名>::<成员函数>"。


  1. #include   
  2.   
  3. using namespace std;  
  4.   
  5. class CBase  
  6. {  
  7. public:  
  8.     void Print()  
  9.     {  
  10.         cout << "调用基类的成员函数" << endl;  
  11.     }  
  12. };  
  13.   
  14. class CDerived : public CBase  
  15. {  
  16. public:  
  17.     void Print()  
  18.     {  
  19.         cout << "调用派生类的成员函数" << endl;  
  20.     }  
  21. };  
  22.   
  23. int main()  
  24. {  
  25.     CDerived human;  
  26.     human.Print();  
  27.     human.CBase::Print();  
  28.     return 0;  
  29. }  

执行结果:

调用派生类的成员函数
调用基类的成员函数


四、虚函数的作用域
关于虚函数的内容,就会涉及到动态绑定的知识了,这里先不介绍,主要还是说明类继承的作用域的问题。在类的继承过程中,基类中的成员函数是不是虚函数,将会起着非常大的作用,先来看2个虚函数的例子。
1.在基类中没有使用virtual关键字


  1. #include   
  2.   
  3. using namespace std;  
  4.   
  5. class CBase  
  6. {  
  7. public:  
  8.     void Print() const  
  9.     {  
  10.         cout << "调用CBase::Print()" << endl;  
  11.     }  
  12. };  
  13.   
  14. class CDerived1 : public CBase  
  15. {  
  16. public:  
  17.     void Print() const  
  18.     {  
  19.         cout << "调用CDerived1::Print()" << endl;  
  20.     }  
  21. };  
  22.   
  23. class CDerived2 : public CDerived1  
  24. {  
  25. public:  
  26.     void Print() const  
  27.     {  
  28.         cout << "调用CDerived2::Print()" << endl;  
  29.     }  
  30. };  
  31.   
  32. int main()  
  33. {  
  34.     CBase bobj;  
  35.     CDerived1 d1obj;  
  36.     CDerived2 d2obj;  
  37.     CBase *bp1 = &bobj;  
  38.     CBase *bp2 = &d1obj;  
  39.     CBase *bp3 = &d2obj;  
  40.     bp1->Print();  
  41.     bp2->Print();  
  42.     bp3->Print();  
  43.     return 0;  
  44. }  

执行结果:

调用CBase::Print()
调用CBase::Print()
调用CBase::Print()


2.在基类中使用virtual关键字


  1. #include   
  2.   
  3. using namespace std;  
  4.   
  5. class CBase  
  6. {  
  7. public:  
  8.     virtual void Print() const  
  9.     {  
  10.         cout << "调用CBase::Print()" << endl;  
  11.     }  
  12. };  
  13.   
  14. class CDerived1 : public CBase  
  15. {  
  16. public:  
  17.     void Print() const  
  18.     {  
  19.         cout << "调用CDerived1::Print()" << endl;  
  20.     }  
  21. };  
  22.   
  23. class CDerived2 : public CDerived1  
  24. {  
  25. public:  
  26.     void Print() const  
  27.     {  
  28.         cout << "调用CDerived2::Print()" << endl;  
  29.     }  
  30. };  
  31.   
  32. int main()  
  33. {  
  34.     CBase bobj;  
  35.     CDerived1 d1obj;  
  36.     CDerived2 d2obj;  
  37.     CBase *bp1 = &bobj;  
  38.     CBase *bp2 = &d1obj;  
  39.     CBase *bp3 = &d2obj;  
  40.     bp1->Print();  
  41.     bp2->Print();  
  42.     bp3->Print();  
  43.     return 0;  
  44. }  

执行结果:

调用CBase::Print()
调用CDerived1::Print()
调用CDerived2::Print()


1,2两个例子可以看到,一个virtual的关键字起着这么大的作用。当基类的成员函数使用virtual关键字修饰的话,基类指针会根据指向的对象的实际类型来寻找相应的类的成员函数的定义。要获得动态绑定,必须通过基类的引用或指针调用虚成员。
在第二个例子中,将Print函数声明为虚函数,这样子编译器会生成代码,在运行时基于引用或指针所绑定的对象的实际类型进行调用。bp2指向CDerived1对象,bp3指向CDerived2对象,所以都是调用属于自己的Print函数版本。



阅读(2803) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~