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

输出结果:

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱睡觉的小馨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值