用指向基类对象的指针输出数据
#include "stdafx.h"
#include<iostream>
#include<string>using namespace std;
class student{
private:
int num;
int age;
float score;
public:
student(int ,int ,float);
void display();
};
//定义构造函数
student::student(int n,int a,float sc):num(n),age(a),score(sc){}void student::display(){//定义成员函数
cout<<num<<endl;
cout<<age<<endl;
cout<<score<<endl;
}class graduate:public student{
private:
float pay;
public:
graduate(int,int,float,float);
void display();
};
graduate::graduate(int n,int a,float sc,float p):student(n,a,sc),pay(p){}
void graduate::display(){
student::display();
cout<<pay<<endl;
}
int main(){
student st(12222,22,97.4);
graduate gr(353543,26,88.2,8000);
student *pt=&st;
pt->display();
pt=&gr;
pt->display();
return 0;
}输出结果:
12222
22
97.4
353543
26
88.2
Press any key to continue
补充:软件开发 , C语言 ,