C++ _ 构造函数(一)

构造函数
  • 用于初始化类的成员变量
  • 构造函数没有返回值, 函数名和类名一样, 可以有参数, 可以重载, 可以有多个
  • 如果没有自定义构造函数, 那么编译器会自动生成一个默认的,公有的,无参的构造函数
  • 一旦自定义了一个构造函数,就不会再生成默认构造函数了
有参的构造函数
  • 初始化列表(在类的构造函数调用之前就初始化完成了, 推荐以这种方式初始化成员变量)

  • 引用成员必须初始化

  • 常变量必须初始化
  • 构造函数内部不能初始化常变量和引用成员,而在初始化列表中可以

  • 类的成员变量初始化的顺序以成员变量在类中声明的顺序为准,与在初始化列表中的顺序无关

  • 类的组合,一个类的对象是另一个类的成员

  • 先调用类的组合成员的构造函数去构造这个类的对象

  • 再调用自己的构造函数构造自己的其他成员

代码演示

一个生物类

#pragma once
#include <iostream>
#include <string>
#include "food.h"

using namespace std;

class biology
{
public:
    //构造函数没有返回值, 函数名和类名一样, 可以有参数, 可以重载, 可以有多个
    //如果没有自定义构造函数, 那么编译器会自动生成一个默认的,公有的,无参的构造函数
    //一旦自定义了一个构造函数,就不会再生成默认构造函数了
    biology();  //无参的构造函数
    biology(const string &t_name, int t_height, int t_weight, const string &food_name, int time);  //有参的构造函数
    void print()
    {
        cout << "biology name: " << m_name << endl;
        cout << "biology height: " << m_height << endl;
        cout << "biology weight: " << m_weight << endl; 
        cout << "biology number: " << number << endl;
        cout << "biology height01: " << m_height01 << endl;
        m_food.print();
    }

private:
    string m_name;
    int m_weight;
    int m_height;
    int &m_height01; //引用成员也必须初始化
    const int number; //常变量必须初始化

    food m_food; //类的组合,一个类的对象是另一个类的成员
                 //先调用类的组合成员的构造函数去构造这个类的对象
                 //再调用自己的构造函数构造自己的其他成员

};

#include "biology.h"

//构造函数用于初始化类的成员变量
biology::biology()
    :number(200), m_height01(m_height)
{
    cout << "进入了无参的构造函数biology()" << endl;
    m_name = "老虎";
    m_height = 200;
    m_weight = 300;
}
//有参的构造函数
//初始化列表(在类的构造函数调用之前就初始化完成了, 推荐以这种方式初始化成员变量)
//构造函数内部不能初始化常变量和引用成员,而在初始化列表中可以
//类的成员变量初始化的顺序以成员变量在类中声明的顺序为准,与在初始化列表中的顺序无关
biology::biology(const string & t_name, int t_height, int t_weight, const string &food_name, int time)
    :m_name(t_name), m_height(t_height), m_weight(t_weight), number(200), m_height01(m_height), m_food(food_name, time)
{
    cout << "进入了有参的构造函数biology(...)" << endl;
    /*m_name = t_name;
    m_height = t_height;
    m_weight = t_weight;*/
}

一个食物类

#pragma once

#include <iostream>
#include <string>

using namespace std;

class food
{
public:
    food();
    food(const string &m_name, int shelf_life);
    void print();

private:
    string m_name;
    int shelf_life;
};

#include "food.h"


food::food()
    :m_name("面包"), shelf_life(12)
{
    cout << "进入food()" << endl;
}

food::food(const string & t_name, int t_shelf_life)
    :m_name(t_name), shelf_life(t_shelf_life)
{
    cout << "进入food(...)" << endl;
}

void food::print()
{
    cout << "食物名称: " << m_name << endl;
    cout << "保质期: " << shelf_life << endl;
}
#include <iostream>
#include "biology.h"


using namespace std;

int main()
{
    biology t_biology1;
    t_biology1.print();
    cout << endl;

    biology t_biology2("兔子", 99, 88, "胡萝卜", 6);
    t_biology2.print();
    cout << endl;

    food t_food1;
    t_food1.print();
    cout << endl;


    system("pause");
    return 0;
}

这里写图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值