#include <iostream>
#include <string>
using namespace std;
//声æåºç±»Student
class Student
{
public:
Student(int, string,float); //声ææé å½æ°
void display( );//声æè¾åºå½æ°
protected: //åä¿æ¤æåï¼æ´¾çç±»å¯ä»¥è®¿é®
int num;
string name;
float score;
};
//Studentç±»æåå½æ°çå®ç°
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";
}
//声æå
¬ç¨æ´¾çç±»Graduate
class Graduate:public Student
{
public:
Graduate(int, string, float, float);//声ææé å½æ°
void display( );//声æè¾åºå½æ°
private:float pay;
};
// Graduateç±»æåå½æ°çå®ç°
void Graduate::display( )//å®ä¹è¾åºå½æ°
{
cout<<"num:"<<num<<"\nname:"<<name<<"\nscore:"<<score<<"\npay="<<pay<<endl;
}
Graduate::Graduate(int n, string nam,float s,float p):Student(n,nam,s),pay(p){}
//主å½æ°
int main()
{
Student stud1(1001,"Li",87.5);//å®ä¹Student类对象stud1
Graduate grad1(2001,"Wang",98.5,563.5);//å®ä¹Graduate类对象grad1
Student *pt=&stud1;//å®ä¹æååºç±»å¯¹è±¡çæéåépt
pt->display( );
pt=&grad1;
pt->display( );
return 0;
}
温馨提示:答案为网友推荐,仅供参考