1.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.Print();
return 0;
}
但是栈要是没有初始化,就会崩溃
#include"Func.h"
void Stack::Init()//告诉编译器Init是Stack的成员函数
{
a = nullptr;
top = capacity = 0;
}
void Stack::push(int x)
{
if (top == capacity)
{
int newcapacity = capacity == 0 ? 4 : capacity * 2;
int* temp = (int*)realloc(a, sizeof(int) * newcapacity);
if (temp == nullptr)
{
std::cerr << "Memory reallocation failed" << std::endl;
return; // 重新分配内存失败,直接返回
}
a = temp;
capacity = newcapacity;
}
a[top++] = x;
}
int Stack::Top()
{
if (top == 0)
{
return -1; // 栈为空,返回一个默认值
}
return a[top - 1]; // 返回栈顶元素
}
int main()
{
Stack st;
// st.Init();
st.push(1);
st.push(1);
cout << st.Top() << endl;
return 0;
}
因为没有初始化就是随机值,对随机值访问,崩溃。 用完销毁。引入构造函数
1.1.1 特性
定义:构造函数是特殊的成员函数,但是构造函数的主要任务并不是开空间创建对象,而是初始化对象。存放在公共代码区中。
特征如下:
1.函数名与类名相同。
2.无返回值(不需要写void)
3.对象实例化时编译器自动调用对应的构造函数。
4.构造函数可以重载。(可以写多个构造函数,提供多种初始化方式)
举例:日期类使用构造函数初始化
class Date
{
public:
Date()
{
_year = 1;
_month = 1;
_day = 1;
}
Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
}
/*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.Print();
//Date d1();是错误的,因为编译器不知道是函数声明,还是对象
Date d2(2023, 7, 20);
d2.Print();
return 0;
}
使用全缺省,但是全缺省和无参构造不能同时存在,编译器调用时会产生歧义。
Date(int year=1, int month=1, int day=1)
{
_year = year;
_month = month;
_day = day;
}
Date d3(2023);
d3.Print();
栈的构造函数
#pragma once
#include<stdlib.h>
#include <cstdlib>
#include <iostream>
#include<assert.h>
using namespace std;
class Stack
{
public:
Stack(size_t n=4) //刚开始确实不想开空间就是0,n初始多大都行
{
if (n == 0)
{
a = nullptr;
top = capacity = 0;
}
else
{
a = (int*)malloc(sizeof(int) * n); //如果你传了就不用缺省
if (a == nullptr)
{
perror("malloc fail");
exit(-1);
}
top = 0;
capacity = n;
}
}
//成员函数
//void Init()
//{
// a = nullptr;
// top = capacity = 0;
//}
void push(int x)
{
if (top == capacity)
{
int newcapacity = capacity == 0 ? 4 : capacity * 2;
int* temp = (int*)realloc(a, sizeof(int) * newcapacity);
if (temp == nullptr)
{
std::cerr << "Memory reallocation failed" << std::endl;
return; // 重新分配内存失败,直接返回
}
a = temp;
capacity = newcapacity;
}
a[top++] = x;
}
int Top()
{
if (top == 0)
{
return -1; // 栈为空,返回一个默认值
}
return a[top - 1]; // 返回栈顶元素
}
void Pop()
{
assert(top > 0);
top--;
}
void Destroy()
{
free(a);
a = nullptr;
top = capacity = 0;
}
bool Empty()
{
return top == 0;
}
private:
//成员变量
int* a;
int top;
int capacity;
};
int main()
{
Stack st1; //可能一次没有开足够大的空间
st1.push(1);
st1.push(2);
st1.push(3);
while (!st1.Empty())
{
cout << st1.Top() << " ";
st1.Pop();
}
cout << endl;
st1.Destroy();
Stack st2(1000); //用这个构造函数就不会有扩容,一次就开好
for (size_t i = 0; i < 1000; i++)
{
st2.push(i);
}
while (!st2.Empty())
{
cout << st2.Top() << " ";
st2.Pop();
}
cout << endl;
st2.Destroy();
return 0;
}
1.1.2 默认构造函数
编译器生成的默认构造特点:
1.我们不写才会生成午餐的默认构造,我们写了就不会生成了,注意:实例化对象必须调用构造函数
2.内置类型(int double char ,int*,Date*,只要是指针都是内置类型)的成员不会处理(C++11支持声明给缺省值)
private:
int _year=1; //声明给的缺省值
int _month=1;
int _day=1;
};
3.自定义类型(struct,class,union,..) 会处理,会去调用这个成员的默认构造函数。
例如:两个栈实现一个队列
#pragma once
#include<stdlib.h>
#include <cstdlib>
#include <iostream>
#include<assert.h>
using namespace std;
class Stack
{
public:
Stack(size_t n=4) //刚开始确实不想开空间就是0,n初始多大都行
{
if (n == 0)
{
a = nullptr;
top = capacity = 0;
}
else
{
a = (int*)malloc(sizeof(int) * n); //如果你传了就不用缺省
if (a == nullptr)
{
perror("malloc fail");
exit(-1);
}
top = 0;
capacity = n;
}
}
void push(int x)
{
if (top == capacity)
{
int newcapacity = capacity == 0 ? 4 : capacity * 2;
int* temp = (int*)realloc(a, sizeof(int) * newcapacity);
if (temp == nullptr)
{
std::cerr << "Memory reallocation failed" << std::endl;
return; // 重新分配内存失败,直接返回
}
a = temp;
capacity = newcapacity;
}
a[top++] = x;
}
int Top()
{
if (top == 0)
{
return -1; // 栈为空,返回一个默认值
}
return a[top - 1]; // 返回栈顶元素
}
void Pop()
{
assert(top > 0);
top--;
}
void Destroy()
{
free(a);
a = nullptr;
top = capacity = 0;
}
bool Empty()
{
return top == 0;
}
private:
//成员变量
int* a;
int top;
int capacity;
};
class MyQueue //MyQueue不需要写构造函数,调栈的
{
private:
Stack _pushst; //自定义类型
Stack _popst;
};
int main()
{
MyQueue mq;
return 0;
}
总结:一般情况下,我们都需要自己写构造函数,决定初始化方式,成员变量全是自定义类型,可以考虑不写构造函数。
4.无参的构造函数和全缺省的构造函数都被称为默认构造函数,并且默认构造函数有且只能有一个。(多个并存会存在调用二义性),一般写全缺省。注意:无参构造函数、全缺省构造函数、我们没写编译器默认生成的构造函数,都可以认为是默认构造函数。不传参就可以调用的就是默认构造函数。
class Date
{
public:
Date()
{
_year = 1;
_month = 1;
_day = 1;
}
Date(int year=1, int month=1, int day=1)
{
_year = year;
_month = month;
_day = day;
}
void Print()
{
cout << _year << "/" << _month << "/" << _day << endl;
}
private:
int _year;//声明不是定义,声明不开空间,定义开
int _month;
int _day;
};
1.2 析构函数
1.2.1 概念
与构造函数相反。析构函数不是完成对对象本身的销毁,局部对象销毁工作是由编译器完成的(对象在栈帧中,栈帧结束就跟着销毁)。而对象在销毁时会自动调用析构函数,完成对象中资源的清理工作。
1.2.1 特性
析构函数是特殊的成员函数,特征如下:
1.析构函数名是在类名前加上字符~
2.无参数无返回值类型
3.一个类只能有一个析构函数。若未显式定义,系统会自动生成默认的析构函数。对于内置类型成员不处理,自定义类型成员会调用这个成员的析构函数。注意:析构函数不能重载。
4.对象生命周期结束时,C++编译系统会自动调用析构函数。
举例;栈写析构函数,比如malloc,fopen,显示申请的的去释放,清理资源。
class Stack
{
public:
Stack(size_t n=4) //刚开始确实不想开空间就是0,n初始多大都行
{
if (n == 0)
{
a = nullptr;
top = capacity = 0;
}
else
{
a = (int*)malloc(sizeof(int) * n); //如果你传了就不用缺省
if (a == nullptr)
{
perror("malloc fail");
exit(-1);
}
top = 0;
capacity = n;
}
}
~Stack()
{
cout << "~Stack()" << endl;
free(a);
a = nullptr;
top = capacity = 0;
}
}