导航

C++中的多态

Posted on 2014-03-23 23:10  可可武凌  阅读(350)  评论(4)    收藏  举报

多态性是面向对象程序设计的重要特征之一。多态性是指发出同样的消息被不同类型的对象接收时有可能导致完全不同的行为。

多态的实现方式包括以下3种:函数重载、运算符重载、虚函数。

1、运算符重载:

 1 #include<iostream>
 2 using namespace std;
 3 
 4 class Complex
 5 {
 6 private:
 7     int real,imag;
 8 public:
 9     void show() const;
10     Complex operator +(const Complex c);
11     Complex operator -(const Complex c);
12     /*
13     Complex(int a=0,int b=0)
14     {
15         real=a;imag=b;
16     }
17     */
18     Complex(int a=0,int b=0):real(a),imag(b)
19     {}
20 };//don't forget this!
21 
22 void Complex::show() const
23 {
24     cout<<"real:"<<real<<"imag:"<<imag<<endl;
25 }
26 
27 Complex Complex::operator +(const Complex c)
28 {
29     Complex ret;
30     ret.real=real+c.real;
31     ret.imag=imag+c.imag;
32     return ret;
33 }
34 
35 Complex Complex::operator -(const Complex c)
36 {
37     Complex ret;
38     ret.real=real-c.real;
39     ret.imag=imag-c.imag;
40     return ret;
41 }
42 
43 int main()
44 {
45     Complex a(2,3),b(-1,5),c,d;
46     a.show();
47     b.show();
48     c=a+b;
49     d=a-b;
50     c.show();
51     d.show();
52     return 0;
53 }

注意运算符重载操作数个数:重载为类的成员函数时,参数个数=原操作数个数-1(后置++、--除外);重载为非成员函数时,参数个数=原操作数个数,且至少有一个自定义类型。

 1 #include<iostream>
 2 using namespace std;
 3 
 4 class Clock
 5 {
 6 private:
 7     int hour,minute,second;
 8 public:
 9     Clock(int a=0,int b=0,int c=0):hour(a),minute(b),second(c)
10     {
11         //hour=a;minute=b;second=c;
12     }
13     void show() const;
14     Clock operator ++();
15     Clock operator ++(int);
16 };
17 
18 void Clock::show() const
19 {
20     cout<<hour<<":"<<minute<<":"<<second<<endl;
21 }
22 
23 //before
24 Clock Clock::operator ++()
25 {
26     if(second+1>=60)
27     {
28         second=second-60;
29         if(minute+1>=60)
30         {
31             minute=0;
32             if(hour+1>=24)
33                 hour=0;
34             else hour=hour+1;
35         }
36         else minute=minute+1;
37     }
38     else second=second+1;
39     return *this;
40 }
41 
42 //behind   still one argument
43 Clock Clock::operator ++(int)
44 {
45     Clock ret=*this;
46     ++(*this);
47     return ret;
48 }
49 
50 int main()
51 {
52     Clock clock(23,59,59);
53     cout<<"The time is:";
54     clock.show();
55 
56     cout<<"(Time++) is:";
57     (clock++).show();
58 
59     cout<<"(++Time) is:";
60     (++clock).show();
61     return 0;
62 }

前置++重载,0个操作数;后置++重载,1个操作数,函数中不需要使用该操作数。

 

2、虚函数:

注意事项:若需要使用多态,则继承方式必须是public或者protected继承,且需要使用到的虚函数在基类中必须是public类型。

 1 #include<iostream>
 2 using namespace std;
 3 
 4 class Base1
 5 {
 6 public:
 7     virtual void show()
 8     {
 9         cout<<"I am base1!"<<endl;
10     }
11 };
12 
13 class Base2
14 {
15 public:
16     void show()
17     {
18         cout<<"I am base2!"<<endl;
19     }
20 };
21 
22 class Derived1:public Base1
23 {
24     void show()
25     {
26         cout<<"I am Derived1!"<<endl;
27     }
28 };
29 
30 class Derived2:public Base2
31 {
32     void show()
33     {
34         cout<<"I am Derived2!"<<endl;
35     }
36 };
37 
38 class Derived3:public Base1,public Base2
39 {
40     void show()
41     {
42         cout<<"I am Derived3!"<<endl;
43     }
44 };
45 
46 int main()
47 {
48     Base1 *p1;
49     Base2 *p2;
50     Derived1 d1;
51     Derived2 d2;
52     Derived3 d3;
53 
54     p1=&d1;
55     p1->show();
56 
57     p2=&d2;
58     p2->show();
59 
60     p1=&d3;
61     p1->show();
62     p2=&d3;
63     p2->show();
64 
65     return 0;
66 }

纯虚函数:virtual void func()=0;

抽象类:包含了纯虚函数的类。