一个简单派生类的定义
#include <iostream> #include<cstring> using namespace std; class Student //声明基类Student { public: Student(int n,string nam,char s):num(n),name(nam),sex(s) {} //基类构造函数 ~Student( ) { } //基类析构函数 void show( ) { cout<<"num: "<<num<<endl; cout<<"name: "<<name<<endl; cout<<"sex: "<<sex<<endl<<endl; } protected: //保护部分 int num; string name; char sex ; }; class Student1: public Student //声明派生类Student1 { public: //在函数体中只对派生类新增的数据成员初始化 //派生类构造函数 Student1(int n,string nam,char s,int a,string ad):Student(n,nam,s),age(a),addr(ad) {} void show1( ) { cout<<"num: "<<num<<endl; cout<<"name: "<<name<<endl; cout<<"sex: "<<sex<<endl; cout<<"age: "<<age<<endl; cout<<"address: "<<addr<<endl<<endl; } ~Student1( ) { } //派生类析构函数 private: //派生类的私有部分 int age; string addr; }; int main( ) { Student1 stud1(10010,"Wang-li",'f',19,"115 Beijing Road,Shanghai"); Student1 stud2(10011,"Zhang-fun",'m',21,"213 Shanghai Road,Beijing"); Student stud3(20010,"He-xin",'m'); stud1.show1( ); //输出第一个学生的数据 stud2.show( ); //输出第二个学生的数据 stud3.show( ); //输出第二个学生的数据 return 0; }
构造函数的写法
#include <iostream> #include<string> using namespace std; class Student//声明基类Student { public: Student(int n,string nam,char s); //基类构造函数 ~Student( ) { } //基类析构函数 protected: //保护部分 int num; string name; char sex ; }; Student::Student(int n,string nam,char s) //基类构造函数 { num=n; name=nam; sex=s; } class Student1: public Student //声明派生类Student1 { public: //派生类的公用部分 Student1(int n,string nam,char s,int a,string ad);//声明 void show( ); ~Student1( ) { } //派生类析构函数 private: //派生类的私有部分 int age; string addr; }; Student1::Student1(int n,string nam,char s,int a,string ad):Student(n,nam,s) { age=a; //在函数体中只对派生类新增的数据成员初始化 addr=ad; } void Student1::show( ) { cout<<"num: "<<num<<endl; cout<<"name: "<<name<<endl; cout<<"sex: "<<sex<<endl; cout<<"age: "<<age<<endl; cout<<"address: "<<addr<<endl<<endl; } int main( ) { Student1 stud1(10010,"Wang-li",'f',19,"115 Beijing Road,Shanghai"); Student1 stud2(10011,"Zhang-fun",'m',21,"213 Shanghai Road,Beijing"); stud1.show( ); //输出第一个学生的数据 stud2.show( ); //输出第二个学生的数据 return 0; }
构造函数和析构函数执行的顺序
#include<iostream> using namespace std; class Base { protected: Base() { cout<<'A'; } Base(char c) { cout<<c; } }; class Derived: public Base { public: Derived(char c) { cout<<c; } }; int main() { Derived d1('B'); return 0; }
有子对象的派生类的构造函数
#include <iostream> #include <string> using namespace std; class Student//声明基类 { public: //公用部分 Student(int n, string nam ):num(n), name(nam) {} //基类构造函数,与例相同 void display( ) //成员函数,输出基类数据成员 { cout<<"num:"<<num<<endl<<"name:"<<name<<endl; } protected: //保护部分 int num; string name; }; class Student1: public Student //声明公用派生类Student1 { public: Student1(int n, string nam,int n1, string nam1,int a, string ad): Student(n,nam),monitor(n1,nam1),age(a),addr(ad) {} //派生类构造函数 void show( ) { cout<<"This student is:"<<endl; display(); //输出num和name cout<<"age: "<<age<<endl; //输出age cout<<"address: "<<addr<<endl<<endl; //输出addr } void show_monitor( ) //成员函数,输出子对象 { cout<<endl<<"Class monitor is:"<<endl; monitor.display( ); //调用基类成员函数 } private: //派生类的私有数据 Student monitor; //定义子对象(班长) int age; string addr; }; int main( ) { Student1 stud1(10010,"Wang-li",10001,"Li-sun",19,"115 Beijing Road,Shanghai"); stud1.show( ); //输出学生的数据 stud1.show_monitor(); //输出子对象的数据 return 0; }
多层派生时的构造函数
#include <iostream> #include<string> using namespace std; class Student//声明基类 { public: //公用部分 Student(int n, string nam ) //基类构造函数 { num=n; name=nam; } void display( ) //输出基类数据成员 { cout<<"num:"<<num<<endl; cout<<"name:"<<name<<endl; } protected: //保护部分 int num; //基类有两个数据成员 string name; }; class Student1: public Student //声明公用派生类Student1 { public: Student1(int n,string nam,int a):Student(n,nam)//派生类构造函数 { age=a; } //在此处只对派生类新增的数据成员初始化 void show( ) //输出num,name和age { display( ); //输出num和name cout<<"age: "<<age<<endl; } private: //派生类的私有数据 int age; //增加一个数据成员 }; class Student2:public Student1 //声明间接公用派生类Student2 { public: //下面是间接派生类构造函数 Student2(int n, string nam,int a,int s):Student1(n,nam,a) { score=s; } void show_all( ) //输出全部数据成员 { show( ); //输出num和name cout<<"score:"<<score<<endl; //输出age } private: int score; //增加一个数据成员 }; int main( ) { Student2 stud(10010,"Li",17,89); stud.show_all( ); //输出学生的全部数据 return 0; }
时间: 2024-09-20 06:37:49