个人主页:guoguoqiang. 专栏:我与C++的爱恋

本篇着重介绍构造函数和析构函数,剩余内容在下篇解答。

一、类的默认成员函数

如果一个类中什么成员都没有,简称为空类。 任何类在什么都不写时,编译器会自动生成以下6个默认成员函数。 默认成员函数:用户没有显式实现,编译器会生成的成员函数称为默认成员函数。

class Date {};

特点 1.我们不写,编译器会自动生成,我们写了编译器就不会自动生成。 默认生成构造函数和析构函数 a.内置类型不做处理 b.自定义类型会调用对应构造/析构。 2.自动调用

二、构造函数

1.概念

class Date

{

public:

void Init(int year, int month, int day)

{

_year = year;

_month = month;

_day = day;

}

void Print()

{

cout << _year << "-" << _month << "-" << _day << endl;

}

private:

int _year;

int _month;

int _day;

};

int main()

{

Date d1;

d1.Init(2022, 7, 5);

d1.Print();

Date d2;

d2.Init(2022, 7, 6);

d2.Print();

return 0;

}

构造函数是一个特殊的成员函数,名字与类名相同,创建类类型对象时由编译器自动调用,以保证每个数据成员都有 一个合适的初始值,并且在对象整个生命周期内只调用一次。

2.特性

构造函数是特殊的成员函数,构造函数的主要任务是初始化对象。 其特征如下: 1. 函数名与类名相同。 2. 无返回值。(不需要void) 3. 对象实例化时编译器自动调用对应的构造函数。 4. 构造函数可以重载 一般情况下,建议每个类都写个全缺省构造函数 5.如果类中没有显式定义构造函数,则C++编译器会自动生成一个无参的默认构造函数,一旦用户显式定义编译器将不再生成。

class Date

{

public:

// 1.无参构造函数

Date()

{}

// 2.带参构造函数

Date(int year, int month, int day)

{

_year = year;

_month = month;

_day = day;

}

private:

int _year;

int _month;

int _day;

};

void TestDate()

{

Date d1; // 调用无参构造函数

Date d2(2015, 1, 1); // 调用带参的构造函数

// 注意:如果通过无参构造函数创建对象时,对象后面不用跟括号,否则就成了函数声明

// 以下代码的函数:声明了d3函数,该函数无参,返回一个日期类型的对象

// warning C4930: “Date d3(void)”: 未调用原型函数(是否是有意用变量定义的?)

Date d3();

}

6.编译器自动生成构造函数,对于内置类型成员变量不做处理, 编译器生成的默认构造函数会对自定义类型成员调用它的默认成员函数(无参构造)。

class Time

{

public:

Time()

{

cout << "Time()" << endl;

_hour = 0;

_minute = 0;

_second = 0;

}

private:

int _hour;

int _minute;

int _second;

};

class Date

{

private:

// 基本类型(内置类型)

int _year;

int _month;

int _day;

// 自定义类型

Time _t;

};

int main()

{

Date d;

return 0;

}

C++11 中针对内置类型成员不初始化的缺陷,又打了补丁,即:内置类型成员变量在类中声明时可以给默认值。

class Time

{

public:

Time()

{

cout << "Time()" << endl;

_hour = 0;

_minute = 0;

_second = 0;

}

private:

int _hour;

int _minute;

int _second;

};

class Date

{

private:

// 基本类型(内置类型)

int _year=1970;

int _month=1;

int _day=1;

// 自定义类型

Time _t;

};

int main()

{

Date d;

return 0;

}

无参的构造函数和全缺省的构造函数都称为默认构造函数,并且默认构造函数只能有一个。 注意:无参构造函数、全缺省构造函数、我们没写编译器默认生成的构造函数,都可以认为是默认构造函数。

class Date

{

public:

Date()

{

_year = 1900;

_month = 1;

_day = 1;

}

Date(int year = 1900, int month = 1, int day = 1)

{

_year = year;

_month = month;

_day = day;

}

private:

int _year;

int _month;

int _day;

};

// 以下测试函数能通过编译吗?

void Test()

{

Date d1;

}

不能,因为Date::Date对重载函数的调用不明确。

#include

using namespace std;

class Date

{

public:

Date()

{

_year = 1900;

_month = 1;

_day = 1;

}

//Date(int year = 1900, int month = 1, int day = 1)

//{

// _year = year;

// _month = month;

// _day = day;

//}

private:

int _year;

int _month;

int _day;

};

void Test()

{

Date d1;

}

int main() {

Test();

}

#include

using namespace std;

class Date

{

public:

//Date()

//{

// _year = 1900;

// _month = 1;

// _day = 1;

//}

Date(int year = 1900, int month = 1, int day = 1)

{

_year = year;

_month = month;

_day = day;

}

private:

int _year;

int _month;

int _day;

};

void Test()

{

Date d1;

}

int main() {

Test();

}

三、析构函数

1.概念

析构函数:与构造函数功能相反,析构函数不是完成对对象本身的销毁,局部对象销毁工作是由编译器完成的。而对象在销毁时会自动调用析构函数,完成对象中资源的清理工作。

2.特性

析构函数是特殊的成员函数,其特征如下:

析构函数名是在类名前加上字符 ~。无参数无返回值类型。一个类只能有一个析构函数。若未显式定义,系统会自动生成默认的析构函数。注意:析构 函数不能重载对象生命周期结束时,C++编译系统系统自动调用析构函数。

typedef int DataType;

class Stack

{

public:

Stack(size_t capacity = 3)

{

_array = (DataType*)malloc(sizeof(DataType) * capacity);

if (NULL == _array)

{

perror("malloc申请空间失败!!!");

return;

}

_capacity = capacity;

_size = 0;

}

void Push(DataType data)

{

// CheckCapacity();

_array[_size] = data;

_size++;

}

// 其他方法...

~Stack()

{

if (_array)

{

free(_array);

_array = NULL;

_capacity = 0;

_size = 0;

}

}

private:

DataType* _array;

int _capacity;

int _size;

};

void TestStack()

{

Stack s;

s.Push(1);

s.Push(2);

}

int main() {

TestStack();

}

5.编译器生成的默认析构函数,对自定类型成员调用它的析构函数。

class Time

{

public:

~Time()

{

cout << "~Time()" << endl;

}

private:

int _hour;

int _minute;

int _second;

};

class Date

{

private:

// 基本类型(内置类型)

int _year = 1970;

int _month = 1;

int _day = 1;

// 自定义类型

Time _t;

};

int main()

{

Date d;

return 0;

}

创建哪个类的对象则调用该类的析构函数,销毁那个类的对象则调用该类的析构函数 用一个OJ题来体会一下编译器生成析构函数的作用 C语言实现

bool isValid(char* s) {

ST st;

STInit(&st);

while(*s){

if(*s=='{'||*s=='('||*s=='['){

STPush(&st,*s);

}

else{

if(STEmpty(&st)){

STDestroy(&st);

return false;

}

char top=STTop(&st);

STPop(&st);

if((*s==']'&&top!='[')

||(*s=='}'&&top!='{')

||(*s==')'&&top!='(')){

STDestroy(&st);

return false;

}

}

s++;

}

bool ret=STEmpty(&st);

STDestroy(&st);

return ret;

}

C++实现

bool isValid(char* s) {

Stack st;

while(*s){

if(*s=='{'||*s=='('||*s=='['){

St.Push(*s);

}

else{

if(St.Empty(&st)){

return false;

}

char top=STTop(&st);

STPop(&st);

if((*s==']'&&top!='[')

||(*s=='}'&&top!='{')

||(*s==')'&&top!='(')){

return false;

}

}

s++;

}

return st.Empty();

}

//两个栈实现一个队列

struct Stack

{

int* _a;

int _top;

int _capacity;

};

class MyQueue

{

private:

Stack _pushst;

Stack _popst;

};

int main()

{

MyQueue q;

return 0;

}

如果类中没有申请资源时,析构函数可以不写,直接使用编译器生成的默认析构函数,有资源申请时,一定要写,否则会造成资源泄漏.

感谢观看!!

参考阅读

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: