#include<iostream>
#include<string>
using namespace std;
class Student
{
protected:
int num;
string name;
float score;
public:
Student(int,string,float);
virtual void display();
};
Student::Student(int n,string nam,float s)
{
num=n;
name=nam;
score=s;
}
void Student::display()
{
cout<<"num:"<<num<<"\nname:"<<name<<"\nscore:"<<score<<"\n\n";
}
class Gratuate:public Student
{
private:
float wage;
public:
Gratuate(int,string,float,float);
void display();
};
Gratuate::Gratuate(int n,string nam,float s,float w):Student(n,nam,s),wage(w){}
void Gratuate::display()
{
cout<<"num:"<<num<<"\nname:"<<name<<"\nscore:"<<score<<"\nwage="<<wage<<endl;
}
int main()
{
Student s1(1001,"li",87.5);
Gratuate g1(2001,"wang",98.5,1200);
Student*pt=&s1;
pt->display();
pt=&g1;
pt->display();
return 0;
}
虚函数
最新推荐文章于 2022-10-17 21:23:28 发布