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;
}

输出结果:

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

#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;
}

输出结果:

猜你喜欢

转载自blog.csdn.net/qq_45696288/article/details/125451579