你也没说Display要怎么多态! 我就给你空着了。自个写吧。
#include <iostream>
using namespace std;
class Organism {
public:
virtual void Display() = 0;
};
class Person : public Organism {
public:
Person() :name(), age(1) {}
Person(string nName, unsigned int nAge) :name(nName), age(nAge) {}
void Display() { /* code */ }
friend ostream& operator<<(ostream& o, const Person & per){
// 奶奶个腿 VS太毒了!居然找不到string的转换!奈何我只有转成C风格字符串!
o << '(' << (per.name).c_str() << ',' << per.age << ')';
return o;
}
protected:
string name;
unsigned int age;
};
class Student : public Person {
public:
Student() :Person(), num(0) {}
Student(string nName, unsigned int nAge, unsigned long nNum)
:Person(nName, nAge), num(nNum) {}
void Display() { /* code */ }
friend ostream& operator<<(ostream& o, const Student& stu){
o << '(' << (stu.name).c_str() << ',' << stu.age << ',' << stu.num << ')';
return o;
}
protected:
unsigned long num;
};
class Graduate : public Student {
public:
Graduate() :Student(), spe() {}
Graduate(string nName, unsigned int nAge, unsigned long nNum, string nSpe)
:Student(nName, nAge, nNum), spe(nSpe) {}
void Display() { /* code */ }
friend ostream& operator<<(ostream& o, const Graduate& gra){
o << '(' << (gra.name).c_str() << ',' << gra.age << ',' << gra.num << ',' << (gra.spe).c_str() << ')';
return o;
}
private:
string spe;
};
int main()
{
Graduate a("123",5,6,"研究生");
cout << a << endl;
system("pause");
return 0;
}
|