-------------------------------------资源来源于网络,仅供自学使用,如有侵权,联系我必删.
第一:
函数重写
问题
如果子类定义了与父类中原型相同的函数会发生什么?
函数重写
在子类中定义与父类中原型相同的函数
函数重写只发生在父类与子类之间
#include <cstdlib>
#include <iostream>
using namespace std;
class Parent
{
public:
void print()
{
cout<<"I'm Parent..."<<endl;
}
};
class Child : public Parent
{
public:
void print()//子类定义的print()函数与父类定义的一样,只是打印不一样
{
cout<<"I'm Child..."<<endl;
}
};
int main(int argc, char *argv[])
{
Child child;
child.print();//默认情况下子类中重写的函数将 隐藏 父类中的函数
child.Parent::print();//通过 作用域分辨符 ::可以访问到父类中被隐藏的函数
cout << "Press the enter key to continue ...";
cin.get();
return EXIT_SUCCESS;
}
父类中被重写的函数依然会继承给子类
默认情况下子类中重写的函数将 隐藏 父类中的函数
通过 作用域分辨符 ::可以访问到父类中被隐藏的函数
第二:
当函数重写遇上赋值兼容性原则
你期望下面的程序输出什么?
#include <cstdlib>
#include <iostream>
using namespace std;
class Parent
{
public:
void print()
{
cout<<"I'm Parent..."<<endl;
}
};
class Child : public Parent
{
public:
void print()
{
cout<<"I'm Child..."<<endl;
}
};
void howToPrint(Parent* p)
{
p->print();
}
void run()
{
Child child;
//子类和父类 都有print()函数情况
//从程序安全的角度,编译器假设父类指针只指向父类对象,因此编译的结果为调用父类的成员函数
//编译器认为最安全的做法是编译到父类的 print 函数
Parent* pp = &child;
Parent& rp = child;
child.print();
pp->print();
rp.print();
}
int main(int argc, char *argv[])
{
run();
cout << "Press the enter key to continue ...";
cin.get();
return EXIT_SUCCESS;
}
问题所在
C++ 与C 相同,是静态编译型语言
在编译时,编译器自动根据指针的类型判断指向的是一个什么样的对象
所以编译器认为父类指针指向的是父类对象(根据赋值兼容性原则,这个假设合理)
由于程序没有运行,所以不可能知道父类指针指向的具体是父类对象还是子类对象
从程序安全的角度,编译器假设父类指针只指向父类对象,因此编译的结果为调用父类的成员函数
下面函数调用后的输出是什么?
在编译这个函数的时候,编译器不可能知道指针 p 究竟指向了什么。但是编译器没有理由报错。于是,编译器认为最安全的做法是编译到父类的 print 函数,因为父类和子类肯定都有相同的 print 函数。
第三:
函数重写实例
#include <cstdlib>
#include <iostream>
using namespace std;
class Boss
{
private:
static Boss* cInstance;//单例模式
Boss()
{
}
public:
static Boss* GetInstance()
{
if( cInstance == NULL )
{
cInstance = new Boss();
}
return cInstance;
}
int fight()
{
cout<<"Boss::fight()"<<endl;
return 10;
}
};
Boss* Boss::cInstance = NULL;
class Master
{
public:
//使用 virtual 声明的函数被重写后即可展现多态特性
virtual int eightSwordKill()
{
cout<<"Master::eightSwordKill()"<<endl;
return 8;
}
};
class NewMaster : public Master
{
public:
//重写也需要加上 virtual 关键字
virtual int eightSwordKill()//多态 函数重写
{
cout<<"NewMaster::eightSwordKill()"<<endl;
return Master::eightSwordKill() * 2;
}
};
void fieldPK(Master* master, Boss* boss)
{
int k = master->eightSwordKill();
int b = boss->fight();
if( k < b )
{
cout<<"Master is killed..."<<endl;
}
else
{
cout<<"Boss is killed..."<<endl;
}
}
int main(int argc, char *argv[])
{
Boss* boss = Boss::GetInstance();
cout<<"Master vs Boss"<<endl;
Master master;
fieldPK(&master, boss);
cout<<"New Master vs Boss"<<endl;
NewMaster newMaster;
fieldPK(&newMaster, boss);
cout << "Press the enter key to continue ...";
cin.get();
return EXIT_SUCCESS;
}
第四:
面向对象的新需求
根据实际的对象类型来判断重写函数的调用
如果父类指针指向的是 父类对象 则调用 父类中定义的函数
如果父类指针指向的是 子类对象 则调用 子类中定义的重写函数
面向对象中的多态
根据实际的对象类型决定函数调用语句的具体调用目标
多态:同样的调用语句有多种不同的表现形态
C++ 中的多态支持
C++ 中通过 virtual 关键字对多态进行支持
使用 virtual 声明的函数被重写后即可展现多态特性
真相只有一个!
这就是传说中的虚函数!
小结
函数重写是面向对象中很可能发生的情形
函数重写只可能发生在父类与子类之间
需要根据实际对象的类型确定调用的具体函数
virtual 关键字是 C++ 中支持多态的唯一方式
被重写的虚函数即可表现出多态的特性