【继承与多态】C++:继承中的赋值兼容规则,子类的成员函数,虚函数(重写),多态

  实现基类(父类)以及派生类(子类),验证继承与转换--赋值兼容规则:


  1. 子类对象可以赋值给父类对象(切割/切片)

  2. 父类对象不能赋值给子类对象

  3. 父类的指针/引用可以指向子类对象

  4. 子类的指针/引用不能指向父类对象(可以通过强制类型转换完成)


[cpp]  view plain  copy
  1. #include<iostream>  
  2. using namespace std;  
  3.   
  4. class People    //父类或者基类  
  5. {  
  6. public:  
  7.     void Display()  
  8.     {  
  9.         cout << "_name" << endl;  
  10.     }  
  11. protected:  
  12.     string _name;  
  13. };  
  14.   
  15.   
  16. class Student:public People        //子类或者派生类  
  17. {  
  18. protected:  
  19.     int _num;  
  20. };  
  21.   
  22.   
  23. void Test()  
  24. {  
  25.     People p;  
  26.     Student s;  
  27.     p = s;    //切片  
  28.     //s = p;    //无法通过,说明父类对象不可以赋值给子类对象  
  29.     People* p1 = &s;    //父类的指针和引用可以指向子类  
  30.     People& p2 = s;  
  31.   
  32.     //Student* s1 = &p;    //子类的指针和引用不可以指向父类  
  33.     //Student& s2 = p;  
  34.     Student* s1 = (Student*)&p;    //可以通过强转实现  
  35.     Student& s2 = (Student&)p;  
  36.   
  37.     //p2->_num = 10;    //_num是子类对象,要越界父类对象才能访问到子类对象  
  38.     //s2._num = 20;  
  39. }  
  40.   
  41.   
  42. int main()  
  43. {  
  44.     Test();  
  45.     system("pause");  
  46.     return 0;  
  47. }  


    如何书写基类与派生类的默认成员函数呢?如:构造函数、拷贝构造函数、赋值运算符重载、析构函数。

[cpp]  view plain  copy
  1. #include<iostream>  
  2. using namespace std;  
  3.   
  4. class People  
  5. {  
  6. public:  
  7.     People(const char* name)  
  8.         :_name(name)  
  9.     {  
  10.         cout << "People()" << endl;  
  11.     }  
  12.   
  13.     People(const People& p)  
  14.         :_name(p._name)  
  15.     {  
  16.         cout << "People(const People& p)" << endl;  
  17.     }  
  18.   
  19.     People& operator=(const People& s)  
  20.     {  
  21.         if (&s != this)  
  22.         {  
  23.             cout << "People& operator= (const People& s)"<<endl;  
  24.             _name = s._name;  
  25.         }  
  26.         return *this;  
  27.     }  
  28.   
  29.     ~People()  
  30.     {  
  31.         cout << "~People()" << endl;  
  32.     }  
  33.   
  34. protected:  
  35.     string _name;  
  36. };  
  37.   
  38. class Student:public People  
  39. {  
  40. public:  
  41.     Student(const char* name, int num)  
  42.         :People(name)  
  43.         , _num(num)  
  44.     {  
  45.         cout << "Student()" << endl;  
  46.     }  
  47.   
  48.     Student(const Student& s)  
  49.         :People(s)  
  50.         , _num(s._num)  
  51.     {  
  52.         cout << "Student(const Student& s)" << endl;  
  53.     }  
  54.       
  55.     Student& operator= (const Student& s)  
  56.     {  
  57.         if (this != &s)  
  58.         {  
  59.             cout << "Student& opeartor= (const Student& s)" << endl;  
  60.             People::operator=(s);  
  61.             _num = s._num;  
  62.         }  
  63.         return *this;  
  64.     }  
  65.   
  66.     ~Student()  
  67.     {  
  68.         cout << "~Student()" << endl;  
  69.     }  
  70. protected:  
  71.     int _num;  
  72. };  
  73.   
  74. void Test()  
  75. {  
  76.     Student s1("张三",15);  
  77.     Student s2(s1);  
  78.     Student s3("李四",12);  
  79.     s3 = s1;  
  80. }  
  81.   
  82.   
  83. int main()  
  84. {  
  85.     Test();  
  86.     system("pause");  
  87.     return 0;  
  88. }  


虚函数&多态

虚函数:

在类的成员函数前面加上virtual,成为虚函数。

虚函数的重写:

当在子类中定义了一个与父类相同的虚函数时,就称为子类的函数重写了父类的虚函数。

多态:

使用父类的函数或者指针调用函数时,若指向父类的虚函数就调用父类的虚函数,若调用子类的虚函数就调用子类的虚函数。

    如:

[cpp]  view plain  copy
  1. #include<iostream>  
  2. using namespace std;  
  3.   
  4. class People  
  5. {  
  6. public:  
  7.     virtual void BuyTickets()  
  8.     {  
  9.         cout << "买票" << endl;  
  10.     }  
  11. };  
  12.   
  13. class Student :public People  
  14. {  
  15. public:  
  16.     virtual void BuyTickets()  
  17.     {  
  18.         cout << "买票-半价" << endl;  
  19.     }  
  20. };  
  21.   
  22. void Fun(People& p)  
  23. {  
  24.     p.BuyTickets();  
  25. }  
  26.   
  27. void Test()  
  28. {  
  29.     People p;  
  30.     Student s;  
  31.     Fun(p);//People为父类,则调用父类的虚函数。  
  32.     Fun(s);//调用子类的虚函数。  
  33. }  
  34.   
  35. int main()  
  36. {  
  37.     Test();  
  38.     system("pause");  
  39.     return 0;  
  40. }  



expand:C++多态总结:多态原理、虚函数指针、重载重写 ,赋值兼容性原则

http://blog.sina.com.cn/s/blog_4b9eab320102varo.html

目录

  1. 问题引出赋值兼容性遇上函数重写
  2. 面向对象新需求
  3. C提供的多态解决方案
  4. 重载重写重定义
  5. 多态的实现原理以及多态的理解
  6. 多态原理研究证明VPTR指针的存在
  7. 虚函数表指针VPTR被编译器初始化的过程
  8. 为什么要定义虚析构函数
  9. 基类和子类对象指针混搭风

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值