简单的派生类的构造函数
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 class Student 6 { 7 public: 8 Student(int n,string nam,char s) 9 { 10 num=n; 11 name=nam; 12 sex=s; 13 } 14 ~Student(){ 15 } 16 protected: 17 int num; 18 string name; 19 char sex; 20 }; 21 22 class Student1:public Student 23 { 24 public: 25 Student1(int n,string nam,char s,int a,string ad):Student(n,nam,s) 26 { 27 age=a; 28 addr=ad; 29 } 30 void show() 31 { 32 cout<<"num:"<<num<<endl; 33 cout<<"name:"<<name<<endl; 34 cout<<"sex:"<<sex<<endl; 35 cout<<"age:"<<age<<endl; 36 cout<<"address:"<<addr<<endl<<endl; 37 } 38 ~Student1(){ 39 } 40 private: 41 int age; 42 string addr; 43 }; 44 int main(int argc, char** argv) { 45 Student1 stud1(10010,"wang",'f',19,"bj"); 46 Student1 stud2(10011,"zhang",'m',21,"sh"); 47 stud1.show(); 48 stud2.show(); 49 return 0; 50 }