程序阅读——多态性与抽象类
(1)虚函数
#include<iostream>
using namespace std;
class A {
int a;
public:
A():a(5){}
virtual void print()const { cout<<a;}
};
class B: public A {
char b;
public:
B() { b='E'; }
void print() const { cout<<b; }
};
void show(A &x) { x.print(); }
int main()
{
A d1,*p;
B d2;
p=&d2;
d1.print();
d2.print();
p->print();
show(d1);
show(d2);
return 0;
}
输出:5EE5E
调试过程:
调试心得:通过虚拟函数,可以通过基类的指针达到访问子类成员函数的目的,去掉virtual关键字将得到不一样的结果(参看附录令人迷惑的隐藏规则)。
(2)虚析构函数
#include <iostream>
using namespace std;
class BASE
{
private:
char c;
public:
BASE(char n):c(n) {}
virtual ~BASE() { cout<<c; }
};
class DERIVED:public BASE
{
private:
char c;
public:
DERIVED(char n):BASE(n+1),c(n) {}
~DERIVED(){ cout<<c; }
};
int main(){
DERIVED d('X');
return 0;
}
输出:XY
(3)纯虚函数
#include <iostream>
using namespace std;
class Base
{
public:
virtual void Who() =0;
};
class FirstDerived:public Base
{
public:
void Who() { cout<<"F"; }
};
class SecondDerived:public Base
{
public:
void Who() { cout<<"S"; }
};
int main()
{
FirstDerived first_obj;
SecondDerived second_obj;
Base &bref=first_obj;
bref.Who();
bref=second_obj;
bref.Who();
Base *bp;
bp=&first_obj;
bp->Who();
bp=&second_obj;
bp->Who();
return 0;
}
输出:FFFS
调试心得:
基类为纯虚函数,不能通过基类的引用来访问派生类的同名函数,只能通过基类的指针来访问。
附录:
==========
令人迷惑的隐藏规则
本来仅仅区别重载与覆盖并不算困难,但是C++的隐藏规则使问题复杂性陡然增加。
这里“隐藏”是指派生类的函数屏蔽了与其同名的基类函数,规则如下:
(1)如果派生类的函数与基类的函数同名,但是参数不同。此时,不论有无virtual
关键字,基类的函数将被隐藏(注意别与重载混淆)。
(2)如果派生类的函数与基类的函数同名,并且参数也相同,但是基类函数没有virtual
关键字。此时,基类的函数被隐藏(注意别与覆盖混淆)。
举例:
//注意:
//1、有virtual才可能发生多态现象
//2、不发生多态(无virtual)调用就按原类型调用
#include<iostream>
using namespace std;
class Base
{
public:
virtual void f(float x)
{
cout << "Base::f(float)" << x << endl;
}
void g(float x)
{
cout << "Base::g(float)" << x << endl;
}
void h(float x)
{
cout << "Base::h(float)" << x << endl;
}
};
class Derived : public Base
{
public:
virtual void f(float x)
{
cout << "Derived::f(float)" << x << endl; //多态、覆盖
}
void g(int x)
{
cout << "Derived::g(int)" << x << endl; //隐藏
}
void h(float x)
{
cout << "Derived::h(float)" << x << endl; //隐藏
}
};
int main(void)
{
Derived d;
Base *pb = &d;
Derived *pd = &d;
// Good : behavior depends solely on type of the object
pb->f(3.14f); // Derived::f(float) 3.14
pd->f(3.14f); // Derived::f(float) 3.14
// Bad : behavior depends on type of the pointer
pb->g(3.14f); // Base::g(float) 3.14
pd->g(3.14f); // Derived::g(int) 3
// Bad : behavior depends on type of the pointer
pb->h(3.14f); // Base::h(float) 3.14
pd->h(3.14f); // Derived::h(float) 3.14
return 0;
}
输出:
上面的程序中:
(1)函数Derived::f(float)覆盖了Base::f(float)。
(2)函数Derived::g(int)隐藏了Base::g(float),而不是重载。
(3)函数Derived::h(float)隐藏了Base::h(float),而不是覆盖。