多态
重载重写和隐藏
1 #include <iostream>
2 using namespace std;
3 void Func(int a)
4 {
5 cout<<a<<endl;
6 }
7 void Func(int* a)//重载:函数名相同,参数不同
8 {
9 cout<<*a<<endl;
10 }
11 class CFather
12 {
13 public :
14 int m_a;
15 int m_b;
16 void Func(int a)
17 {
18 cout<<"CFather::Func()"<<endl;
19 }
20 };
21 class CSon:public CFather
22 {
23 public:
24 void Func(int b)//重写:子类与父类函数名相同,参数相同
25 {
26 cout<<"重写:CSon::Func()"<<endl;
27 }
28 void Func(int*a)//隐藏:子类与父类函数名相同,参数不同
29 {
30 cout<<"重载;CSon::Func()"<<endl;
31 }
32 };
33 int main()
34 {
35 CSon son;
36 int a = 0;
37 Func(a);
38 Func(&a);//重载
39 son.Func(&a);//隐藏: 只能调用子类中的函数
40 son.CFather::Func(a);//若要用父类中的函数要加作用域
41 son.Func(a);//重写
42 }
结果如图:
父类指针指向子类对象
1 #include <iostream> 2 using namespace std; 3 4 class CFather 5 { 6 public : 7 int m_a; 8 CFather() 9 { 10 m_a = 0; 11 } 12 void show() 13 { 14 cout<<"Father"<<endl; 15 } 16 }; 17 class CSon:public CFather 18 { 19 public: 20 int m_a; 21 int m_b; 22 CSon() 23 { 24 m_a = 0; 25 m_b = 0; 26 } 27 void show() 28 { 29 cout<<"Son"<<endl; 30 } 31 }; 32 int main() 33 { 34 CFather *Father = new CSon; 35 cout<<Father->m_a<<endl; 36 Father->show(); 37 //cout<<Father->m_b<<endl; 不可以,父类指针只能调用父类包含的成员变量和函数 38 }
结果如图:
- 多态
虚函数实现多态
1 #include<iostream> 2 using namespace std; 3 4 class CWater 5 { 6 public: 7 virtual void Show() // 虚函数 通过父类的指针 调用子类的函数 8 { 9 cout << "CWater::Show" << endl; 10 } 11 }; 12 13 class CMilk : public CWater 14 { 15 public: 16 void Show() 17 { 18 cout << "CMilk::Show" << endl; 19 } 20 }; 21 22 class CCoco: public CWater 23 { 24 public: 25 void Show() 26 { 27 cout << "CCoco::Show" << endl; 28 } 29 }; 30 31 class CBeer: public CWater 32 { 33 public: 34 void Show() 35 { 36 cout << "CBeer::Show" << endl; 37 } 38 }; 39 40 class CCoffee : public CWater 41 { 42 public: 43 void Show() 44 { 45 cout << "CCoffee::Show" << endl; 46 } 47 }; 48 49 void Bottle(CWater* water) 50 { 51 water->Show(); 52 } 53 54 int main() 55 { 56 57 /*CMilk* p1 = new CMilk; 58 CCoco* p2 = new CCoco; 59 CBeer* p3 = new CBeer; 60 CCoffee* p4 = new CCoffee;*/ 61 62 CWater* p1 = new CMilk; 63 CWater* p2 = new CCoco; 64 CWater* p3 = new CBeer; 65 CWater* p4 = new CCoffee; 66 67 Bottle(p1); 68 Bottle(p2); 69 Bottle(p3); 70 Bottle(p4); 71 72 system("pause"); 73 return 0; 74 }
结果如图:
多态的原理
1 #include<iostream> 2 using namespace std; 3 4 5 class CWater 6 { 7 public: 8 virtual void AA() 9 { 10 cout << "CWater::AA" << endl; 11 } 12 virtual void BB() 13 { 14 cout << "CWater::BB" << endl; 15 } 16 void CC() 17 { 18 cout << "CWater::CC" << endl; 19 } 20 }; 21 22 class CCoffee : public CWater 23 { 24 public: 25 virtual void AA() 26 { 27 cout << "CCoffee::Show" << endl; 28 } 29 void DD() 30 { 31 cout << "CCoffee::CC" << endl; 32 } 33 }; 34 35 36 int main() 37 { 38 39 cout << sizeof(CWater) << endl; 40 41 CWater* p = new CWater; 42 p->AA(); 43 p->BB(); 44 45 p = new CCoffee; 46 p->AA(); 47 p->BB(); 48 p->CC(); 49 50 system("pause"); 51 return 0; 52 }
结果如图:
虚函数列表;
纯虚函数,虚析构
1 #include <iostream> 2 using namespace std; 3 4 class CJT 5 { 6 public: 7 virtual void Run()=0;//纯虚函数,没有大括号且将值赋为0 8 CJT() 9 { 10 cout<<"CJT"<<endl; 11 } 12 //~CJT() 13 //{ 14 // cout<<"~CJT"<<endl; 15 //} 16 virtual ~CJT()//虚析构函数通过父类的指针删除子类对象,通俗来讲可以在delete父类指针的时候调用子类的析构函数 17 { 18 cout<<"~CJT"<<endl; 19 } 20 21 }; 22 class CCar :public CJT 23 { 24 public: 25 virtual void Run() 26 { 27 cout << "汽油" << endl; 28 } 29 CCar() 30 { 31 cout<<"CCar"<<endl; 32 } 33 ~CCar() 34 { 35 cout<<"~CCar"<<endl; 36 } 37 }; 38 class CBus :public CJT 39 { 40 public: 41 virtual void Run() 42 { 43 cout << "电动" << endl; 44 } 45 }; 46 class CPlane :public CJT 47 { 48 public: 49 virtual void Run() 50 { 51 cout << "飞机有" << endl; 52 } 53 }; 54 55 class CPerson 56 { 57 public: 58 void Play(CJT* p) 59 { 60 cout << "张个大嘴" << endl; 61 p->Run(); 62 cout << "好喝" << endl; 63 64 delete p; 65 } 66 }; 67 68 69 int main() 70 { 71 CPerson ps; 72 73 /*CCar* car = new CCar; 74 CBus* bus = new CBus; 75 CPlane* plane = new CPlane; 76 77 ps.Play(car); 78 ps.Play(bus); 79 ps.Play(plane);*/ 80 81 CJT * p = new CCar; 82 delete p ; 83 84 85 system("pause"); 86 return 0; 87 }
结果如图:
成员静态变量
1 #include <iostream> 2 using namespace std; 3 4 class CPerson 5 { 6 public: 7 static int n; // 编译期存在 8 //static const int a = 99; 9 int m_nAge; 10 public: 11 static void Show() // static函数 不能使用非static成员 因为static 函数中 没有this 12 { 13 //cout << m_nAge << endl; 14 cout << n << endl; 15 } 16 }; 17 int CPerson::n = 100; // static 一定要在类外初始化 18 19 int main() 20 { 21 22 cout << CPerson::n << endl; // static 成员 可以直接用类名作用 取得 23 CPerson::Show(); 24 25 CPerson ps; 26 cout << ps.n << endl; 27 28 29 system("pause"); 30 return 0; 31 }