考察类的继承,虚函数的用法。
#include <iostream>
#include <cstring>
using namespace std;
class Human{
private:
string name;
public:
Human(string);
void input();
void virtual print();
};
Human::Human(string n){
name=n;
}
void Human::print()
{
cout << name << endl;
}
void Human::input()
{
cin >> name;
}
class Student :public Human{
double score;
public:
Student(string);
void print();
void addScore(double);
};
Student::Student(string n):Human(n)
{
score=0;
}
void Student::print()
{
Human::print();
cout << score << endl;
}
void Student::addScore(double s)
{
score+=s;
}
class Worker :public Human{
double money;
public:
Worker(string);
void print();
void addWage(double);
};
Worker::Worker(string n):Human(n)
{
money=0;
}
void Worker::print()
{
Human::print();
cout << money << endl;
}
void Worker::addWage(double m)
{
money+=m;
}
int main()
{
Student s("student");
Worker w("worker");
s.input(); s.print();
w.input(); w.print();
s.addScore(10);
w.addWage(100);
s.print();
w.print();
return 0;
}