display中virtual,在父类中必须有virtual 否则可能会出错
#include <iostream>
#include <string>
using namespace std;
class Person
{
public:
Person(const string& name="")
:_name(name)
{}
~Person()
{
}
void Display()
{
cout << "person" << endl;
}
protected:
string _name;
};
class student:public Person
{
public:
student(const string& name,const int num)
:Person(name)
, _num(num)
{
}
~student()
{}
virtual void Display()
{
cout << "student" << endl;
}
protected:
int _num;
};
void Test2()
{
Person p1("头头");
Person* p2 = &p1;
p1.Display();
p2->Display();
student s1("大头",4130905);
p2 = (student* )&s1;
s1.Display();
p2->Display();
}
int main()
{
Test2();
system("pause");
return 0;
}
//class Botany
//{
//public:
// Botany(const string& name="")
// :_name(name)
// {
// cout << "Botany(const string& name)" << endl;
// ++_sCount;
// }
// Botany(const Botany& s)
// {
// cout << "Botany(const Botany& s)" << endl;
// _name = s._name;
// ++_sCount;
// }
// Botany operator=(const Botany& b)
// {
// cout << "Botany operator=(const Botany& b)" << endl;
// if (this != &b)
// {
// _name = b._name;
// }
//
// return *this;
// }
// virtual void Display()
// {
// cout << "Botany:" << _name<< endl;
// }
//protected:
// string _name; //名字
// static int _sCount;
//};
//int Botany::_sCount = 0;
//
//class Tree: virtual public Botany
//{
//public:
// Tree(const string& name,int hight)
// :Botany(name)
// , _hight(hight)
// {
// ++_sCount;
// }
// Tree(Tree& t)
// :Botany(t._name)
// , _hight(t._hight)
// {
// }
// Tree operator=(Tree& t)
// {
// if (this != &t)
// {
// _name = t._name;
// _hight = t._hight;
// }
//
// return *this;
// }
// virtual void Display()
// {
// cout << "Tree:" << _name << endl;
// cout << "Tree hight:" << _hight << endl;
// }
//protected:
// int _hight;
//};
//class Flower :virtual public Botany
//{
//public:
// Flower(const string& name, const string& colour)
// :Botany(name)
// , _colour(colour)
// {
// ++_sCount;
// }
// Flower(Flower& f)
// :Botany(f._name)
// , _colour(f._colour)
// {
// }
// Flower operator=(Flower& f)
// {
// if (this != &f)
// {
// _name = f._name;
// _colour = f._colour;
// }
//
// return *this;
// }
// virtual void Display()
// {
// cout << "flower:" << _name << endl;
// cout << "flower colour:" << _colour << endl;
// }
//protected:
// string _colour;
//};
//class MicheliaAlba : public Flower, public Tree
//{
//public:
// MicheliaAlba(const string& name,const string& colour, const int hight,const string& kind)
// :Botany(name)
// ,Flower(name,colour)
// , Tree(name,hight)
// , _kind(kind)
// {
// ++_sCount;
// }
// MicheliaAlba(MicheliaAlba& f)
// :Botany(f._name)
// , Flower(f._name, f._colour)
// , Tree(f._name, f._hight)
// , _kind(f._kind)
// {
// }
// MicheliaAlba operator=(MicheliaAlba& m)
// {
// if (this != &m)
// {
// _name = m._name;
// _colour = m._colour;
// _hight = m._hight;
// _kind = m._kind;
// }
//
// return *this;
// }
// virtual void Display()
// {
// cout << "MicheliaAlba flower colour:" << _colour << endl;
// cout << "MicheliaAlba tree hight:" << _hight << endl;
// cout << "MicheliaAlba kind:" << _kind << endl;
//
// }
//protected:
// string _kind;;
//};
//void Test1()
//{
// Botany b1("植物");
// b1.Display();
//
// Tree t1("松树",100);
// t1.Display();
//
// Flower f1("百合","白色");
// f1.Display();
// MicheliaAlba m1("白兰花","白色",200,"好看");
// m1.Display();
//}
//int main()
//{
// Test1();
// system("pause");
// return 0;
//}
转载于:https://blog.51cto.com/10541695/1720082