前几天面试的时候被问到了多态的实现,平时确实没有用到过。今天特意去看了相关的知识,并实现了一下,来看一看输出结果,并分析一下导致这样的结果的原因。
#include "stdafx.h"
#include<iostream>
using namespace std;
class Father
{
public:
virtual void TestV()
{
cout << "father.virtual test" << endl;
}
void Test()
{
cout << "father.normal test" << endl;
}
};
class Son1:public Father
{
void TestV()
{
cout << "son1.virtual test" << endl;
}
void Test()
{
cout << "son1.normal test" << endl;
}
};
class Son2 :public Father
{
void TestV()
{
cout << "son2.virtual test" << endl;
}
void Test()
{
cout << "son2.normal test" << endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Son1 son1;
Son2 son2;
Father *pf1 = &son1;
Father *pf2 = &son2;
pf1->TestV();
pf2->Test();
return 0;
}
Fath