【class】类的构造函数与析构函数

构造函数

  • 构造函数的任务是初始化类对象的数据成员,无论何时只要类的对象被创建,就会执行构造函数。
  • 通常需要初始化的数据成员有两大类:一类是无需开发者自己管理内存的变量,一类需要开发者自行管理内存,即new或malloc分配的内存。
  • 如果我们的类中没有显式定义构造函数,那么编译器会为我们隐式的定义一个默认构造函数。

三种初始化方式

  • 变量通过构造函数初始化有三种方式。在定义变量时给定初值,通过初始化列表初始化和在构造函数体内初始化。
//定义变量时给定初值
#include <iostream>
#include <string>
class people {
public:
    int age = 10;
    double money = 100.5;
    std::string name = "jack";
};

int main() {
    people p;
    std::cout << p.age << " " << p.money << " " << p.name << std::endl;//10 100.5 jack
}
//通过初始化列表初始化
#include <iostream>
#include <string>
class people {
public:
    int age;
    double money;
    std::string name;
public:
    people();
};

people::people():age(10),money(100.5),name("jack"){

}

int main() {
    people p;
    std::cout << p.age << " " << p.money << " " << p.name << std::endl;//10 100.5 jack
}
//构造函数体内初始化
#include <iostream>
#include <string>
class people {
public:
    int age;
    double money;
    std::string name;
public:
    people();
};

people::people() {
    age = 10;
    money = 100.5;
    name = "jack";
}

int main() {
    people p;
    std::cout << p.age << " " << p.money << " " << p.name << std::endl;//10 100.5 jack
}

多个构造函数

  • 构造函数可以定义多个,每个函数传入参数不同。创建对象时根据需要调用不同的构造函数。
#include <string>
#include <iostream>

class people {
public:
    int age = 5;
    double money = 100.5;
    std::string name = "jack";
public:
    people();
    people(int age, double money, std::string name);
};

people::people() {

}

people::people(int age, double money, std::string name) {
    this->age = age;
    this->money = money;
    this->name = name;
}
int main() {
    people q;
    cout << q.age << " " << q.money << " " << q.name << endl;//5 100.5 jack

    people p(1, 10.5, "lucy");
    cout << p.age << " " << p.money << " " << p.name << endl;//1 10.5 lucy
}

继承中的构造函数

  • 继承时,构造函数调用方式为:父类默认构造函数、自身构造函数。
#include <iostream>
#include <string>

class people {
public:
    people();

    people(std::string message);

};

class teacher : people {
public:
    teacher();

    teacher(std::string message);

};

people::people() {
    std::cout << "people()" << std::endl;
}

people::people(std::string message) {
    std::cout << "people(std::string message)" << std::endl;
}

teacher::teacher() {
    std::cout << "teacher()" << std::endl;
}

teacher::teacher(std::string message) {
    std::cout << "teacher(std::string message)" << std::endl;
}


int main() {
    teacher t;
    /*people()
	teacher()*/

    teacher t2("jack") ;
	/*people()
	teacher(std::string message)*/
	
}
  • 那么问题来了,如果想调用父类某个特定的构造函数怎么办。只需在子类构造函数中用列表初始化的方式指明调用父类哪个构造函数。如下:
#include <iostream>
#include <string>

class people {
public:
    people();

    people(std::string message);

};

class teacher : people {
public:
    teacher();

    teacher(std::string message);

};

people::people() {
    std::cout << "people()" << std::endl;
}

people::people(std::string message) {
    std::cout << "people(std::string message)" << std::endl;
}

teacher::teacher() {
    std::cout << "teacher()" << std::endl;
}

teacher::teacher(std::string message):people("luck") {
    std::cout << "teacher(std::string message)" << std::endl;
}

int main() {
    teacher t;
    /*people()
	teacher()*/

    teacher t2("jack") ;
	/*people(std::string message)
	teacher(std::string message)*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值