1 #include <iostream> 2 #include <string.h> 3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 4 using namespace std; 5 6 class Student 7 { 8 public: 9 Student(int,string,float); 10 void display(); 11 protected: 12 int num; 13 string name; 14 float score; 15 }; 16 17 Student::Student(int n,string nam,float s) 18 { 19 num=n; 20 name=nam; 21 score=s; 22 } 23 24 void Student::display() 25 { 26 cout<<"num:"<<num<<" name:"<<name<<" score:"<<score<<" "; 27 } 28 29 class Graduate:public Student 30 { 31 public: 32 Graduate(int,string,float,float); 33 void display(); 34 private: 35 float pay; 36 }; 37 38 void Graduate::display() 39 { 40 cout<<"num:"<<num<<" name:"<<name<<" score:"<<score<<" pay="<<pay<<endl; 41 } 42 43 Graduate::Graduate(int n,string nam,float s,float p):Student(n,nam,s),pay(p){ 44 45 } 46 int main(int argc, char** argv) { 47 Student stud1(1001,"li",88); 48 Graduate grad1(2001,"wang",99,555); 49 Student *pt=&stud1; 50 pt->display(); 51 pt=&grad1; 52 pt->display(); 53 return 0; 54 }