C++学习笔记基础篇28——子类的构造函数与析构函数

    科技2022-08-07  118

    1 如果子类没有显示的调用父类的构造函数,那么默认会调用父类无参的构造函数!!! 2 如果父类只提供了有参数的构造函数,那么子类在默认情况下调用父类的无参构造函数时就会报错!

    class CXiaoStudent : public CStudent { public: int yuwen_score; int shuxue_score; int english_score; CXiaoStudent() : CStudent("zhangsan", 'm', 1001, 20) { yuwen_score = 2; shuxue_score = 0; english_score = 0; flag_private = 0; flag_protected = 0; } private: int flag_private; protected: int flag_protected; };

    由子类对象给父类对象赋值是可以的,俗称大材小用。在赋值的时候会舍弃子类的新增成员 由父类的对象给子类的对象赋值是不允许的,会出现填不满的不可预知行为

    完整程序

    涉及到父类对象与子类对象之间的相互转换的应用

    #include <iostream> #include <string> using namespace std; enum EStudentType { EStudentType_Error = 0, EStudentType_Xiao = 1, EStudentType_Zhong = 2, }; class CStudent { public: char *p_name; char sex; int num; int age; static int master; EStudentType type; CStudent(){ type = EStudentType_Error; }; CStudent(char* pname, char t_sex, int t_num, int t_age) :sex(t_sex), num(t_num), age(t_age) { type = EStudentType_Error; p_name = NULL; int n_len = 0; if (pname) { n_len = strlen(pname); } if (n_len > 0) { p_name = new char[n_len + 1]; memset(p_name, 0, n_len + 1); strcpy(p_name, pname); } } CStudent(const CStudent& stud) { type = EStudentType_Error; int n_len = 0; n_len = strlen(stud.p_name); p_name = new char[n_len + 1]; memset(p_name, 0, n_len + 1); strcpy(p_name, stud.p_name); num = stud.num; age = stud.age; sex = stud.sex; } CStudent& operator=(const CStudent& stud) { type = EStudentType_Error; int n_len = 0; n_len = strlen(stud.p_name); p_name = new char[n_len + 1]; memset(p_name, 0, n_len + 1); strcpy(p_name, stud.p_name); num = stud.num; age = stud.age; sex = stud.sex; return *this; } bool operator==(const CStudent& stud) { int n_len = 0; n_len = strlen(stud.p_name); if ((num == stud.num) && (age == stud.age) && (sex == stud.sex) && (*p_name == *stud.p_name)) { for (int i = 0; i < n_len;i++) { if (*(p_name+i )!= *(stud.p_name+i)) { return false; } } return true; } else { return false; } } ~CStudent(); }; CStudent::~CStudent() { if (p_name) { delete[] p_name; p_name = NULL; } } int CStudent::master = 0; class CXiaoStudent : public CStudent { public: int yuwen_score; int shuxue_score; int english_score; CXiaoStudent() : CStudent("zhangsan", 'm', 1001, 20) { yuwen_score = 2; shuxue_score = 0; english_score = 0; type = EStudentType_Xiao; } ~CXiaoStudent() { cout <<"析构了" << endl; } }; class CZhongStudent : public CXiaoStudent { public: int wuli_score; int huaxue_score; CZhongStudent() { type = EStudentType_Zhong; wuli_score = 0; huaxue_score = 0; } ~CZhongStudent() { cout << "析构了" << endl; } }; void average_age(CStudent* p_arr_stud, int n_size) { if (!p_arr_stud || n_size <= 0) return; int total_age = 0; EStudentType type = p_arr_stud[0].type; for (int idx = 0; idx < n_size; ++idx) { switch (type) { case EStudentType_Xiao: total_age += ((CXiaoStudent*)p_arr_stud)[idx].age; break; case EStudentType_Zhong: total_age += ((CZhongStudent*)p_arr_stud)[idx].age; break; default: break; } } int aver_age = total_age / n_size; switch (type) { case EStudentType_Xiao: cout << "小学生的平均年龄是:" << aver_age << endl; break; case EStudentType_Zhong: cout << "中学生的平均年龄是:" << aver_age << endl; break; default: break; } } void test() { CXiaoStudent arr_stud[3]; arr_stud[0].age = 20; arr_stud[1].age = 16; arr_stud[2].age = 18; average_age(arr_stud, 3); //cout << zhangshan.p_name << endl; } int main(int argc, char* argv[]) { test(); system("pause"); return 0; }
    Processed: 0.011, SQL: 8