• C++ 构造函数的使用:创建一个Birth类,在Student类中增加一个成员变量是Birth类的对象。增加两个类的构造方法,在main中进行测试。


    创建一个Birth类,在Student类中增加一个成员变量是Birth类的对象。增加两个类的构造方法,在main中进行测试。

    #include <iostream>
    
    using namespace std;
    
    class Birth{//定义出生日期类
    public:
        Birth(int year,int month, int day);//构造函数
        void show();//成员函数show方法
        void setYear(int year);
        void setMonth(int month);
        void setDay(int day);
        int getYear();
        int getMonth();
        int getDay();
    private:
        int _year;     int _month;     int _day;
    };
    
    //类外实现Birth类构造函数
    Birth::Birth(int year, int month, int day):_year(year),_month(month),_day(day){
        cout<<"Birth类构造函数"<<endl;
    }
    
    //类外实现show函数
    void Birth::show(){
        cout<<"出生日期:"<<_year<<"-"<<_month<<"-"<<_day<<endl;
    }
    
    class Student{//定义学生类Student
    public:
        //    构造方法
        Student(string name, int id, int year, int month, int day);
        void show();
        void set_Name(string name);
        void set_Id(int id);
        string get_Name();
        int get_Id();
    private:
        string _name;    int _id;     Birth birth;
    };
    
    //类外实现构造方法
    Student::Student(string name, int id, int year, int month, int day):_name(name),_id(id),birth(year,month,day){//调用Birth的构造方法
        cout<<"Student类构造函数"<<endl;
    }
    
    //类外实现show函数
    void Student::show(){
        cout<<"姓名:"<<_name<<endl;
        cout<<"学号:"<<_id<<endl;
        birth.show();
    }
    
    int main(){
        Student stu("王彤",12001,2000,12,25);    //创建学生对象stu
        stu.show();            //显示学生信息
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58

    输出结果:

    对于上面的代码中,使用构造函数进行初始化的方式是未在方法体内进行实现的,也可以修改上面的构造函数,通过下面的代码进行变量的初始化。

    #include <iostream>
    
    using namespace std;
    
    class Birth{//定义出生日期类
    public:
        Birth(int year,int month, int day);//构造函数
        void show();//成员函数show方法
        void setYear(int year);
        void setMonth(int month);
        void setDay(int day);
        int getYear();
        int getMonth();
        int getDay();
    private:
        int _year;     int _month;     int _day;
    };
    
    //类外实现Birth类构造函数
    Birth::Birth(int year, int month, int day){
        cout<<"Birth类构造函数"<<endl;
        _year = year;
        _month = month;
        this->_day = day;   //其实加不加this->都可以,我只是想说明,可以这么使用
    }
    
    //类外实现show函数
    void Birth::show(){
        cout<<"出生日期:"<<_year<<"-"<<_month<<"-"<<_day<<endl;
    }
    
    class Student{//定义学生类Student
    public:
        //    构造方法
        Student(string name, int id, int year, int month, int day);
        void show();
        void set_Name(string name);
        void set_Id(int id);
        string get_Name();
        int get_Id();
    private:
        string _name;    int _id;     Birth birth;
    };
    
    //类外实现构造方法
    Student::Student(string name, int id, int year, int month, int day):birth(year,month,day){//调用Birth的构造方法
        cout<<"Student类构造函数"<<endl;
        this->_name = name;
        this->_id = id;
    }
    
    //类外实现show函数
    void Student::show(){
        cout<<"姓名:"<<_name<<endl;
        cout<<"学号:"<<_id<<endl;
        birth.show();
    }
    
    int main(){
        Student stu("徐蛋",77009,2000,1,31);    //创建学生对象stu
        stu.show();            //显示学生信息
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63

    输出结果:

  • 相关阅读:
    概率论期末复习
    zsh: command not found: conda问题解决
    .NET 简介:跨平台、开源、高性能的开发平台
    前端例程20220815:拟物风格复选按钮
    如何在前端开发中实现摄像头拍照和人像定位
    测试用例的设计方法及测试分类
    keras 识别手写数字mnist
    外贸公司官网产品展示网站搭建源码
    GII全球创新指数2013-2020
    通过 vue-cli 创建一个 vue3 的项目
  • 原文地址:https://blog.csdn.net/qq_45696288/article/details/125451579