派生类
#include <iostream> #include<string> using namespace std; class Student//声明基类Student { public: void sets(int n,string nam,char s); void show( ); protected: //保护部分 int num; string name; char sex ; }; void Student::sets(int n,string nam,char s) { num=n; name=nam; sex=s; } void Student::show( ) { cout<<"num: "<<num<<endl; cout<<"name: "<<name<<endl; cout<<"sex: "<<sex<<endl<<endl; } class Student1: public Student //声明派生类Student1 { public: void sets1(int n,string nam,char s,int a,string ad); void show1( ); private: //派生类的私有部分 int age; string addr; }; void Student1::sets1(int n,string nam,char s,int a,string ad) { sets(n,nam,s); age=a; addr=ad; } void Student1::show1( ) { cout<<"num: "<<num<<endl; cout<<"name: "<<name<<endl; cout<<"sex: "<<sex<<endl; cout<<"age: "<<age<<endl; cout<<"address: "<<addr<<endl<<endl; } int main( ) { Student1 stud; stud.sets1(10010,"Wang",'f',19,"Shanghai"); stud.show1( ); //输出第一个学生的数据 stud.show(); return 0; }