C++类的构造函数和析构函数

今天在lintcode上刷题时,发现了一个热门的矩阵类问题。于是乎,我就开始了c++的类的探索之旅。我遇到的最大的问题(可怕的&&可笑的问题–构造函数竟然写错了。。。)。下面就是我的学习与发现啦。
类的构造函数是一类特殊的成员函数,它在创建类的新对象时,被调用。
类的构造函数有如下特征:
1、构造函数没有函数返回类型。
2、构造函数名和类名一样。
3、构造函数允许无参数版本和有参数版本。默认的构造函数是无参数版本。
有参数时,实现的是,某些成员变量的初始化。
我自己写的版本:

#include<iostream>
using namespace std;
class Rectangle {
public:
	Rectangle(int Width, int Height) {
		width = Width;
		height = Height;
	}
	int getArea(int width,int height) {
		return width * height;
	}
private:
	int width, height;
};
int main() {
	Rectangle a(2,3);
	cout<<a.getArea(2,3)<<endl;
	system("pause");
	return 0;
}

在我这段代码里,类的成员函数在类的内部声明定义了,避免了很多在外部定义的细节问题,等一下,我会贴上“在外部定义的代码”。

#include<iostream>
using namespace std;
class Rectangle {
public:
	Rectangle(int Width, int Height);
	int getArea(int width, int height);
private:
	int width, height;
};
Rectangle::Rectangle(int Width,int Height) {
	width = Width;
	height = Height;
}
int Rectangle::getArea(int width, int height) {
	return width * height;
}
int main() {
	Rectangle a(2,3);
	cout<<"矩阵的面积:"<<a.getArea(2,3)<<endl;
	system("pause");
	return 0;
}

这是有参数版本+外部定义的代码。
外部定义时,注意作用域的概念。类的成员函数的作用域是类内部。所以,普通成员函数的定义格式为:函数返回类型 类名::成员函数名(成员数参数1,参数2…){}
构造函数的定义格式为: 类名::构造函数名(参数1,参数2,,,){}

#include<iostream>
using namespace std;
class Rectangle {
public:
	Rectangle();
	void setvalue(int Width,int Height);
	int getArea(int width, int height);
private:
	int width, height;
};
Rectangle::Rectangle() {
	cout << "创建了一个实例(对象)" << endl;
}
void Rectangle::setvalue(int Width,int Height) {
	cout << "实例设值:" << endl;
	width = Width;
    height=Height;
	cout << "矩阵宽度:" << width << endl;
	cout << "矩阵高度:" << height << endl;
}
int Rectangle::getArea(int width, int height) {
	cout << "矩阵的面积:" << endl;
	cout << width * height << endl;
	return 0;
}
int main() {
	Rectangle a;
	a.setvalue(2, 3);
	a.getArea(2, 3);
	system("pause");
	return 0;
}

这是构造函数无参数版本。
如果构造函数无参数,就需要用一个成员函数,实现实例的数据传送。这个成员函数也实现了外部接口,使主函数可以访问类的私有成员变量。
还有一种初始化列表 初始化字段,不常用。我也就没怎么学习。(内心:真是太懒了)
析构函数也是类的一种特殊的成员函数。它在类的对象被删除时执行,有助于在跳出程序前,释放资源。
析构函数的特点:
1、函数无返回类型,无参数。
2、函数名和类名一样。
3、函数名前加一个波浪号~ 作为前缀。

#include<iostream>
using namespace std;
class Rectangle {
public:
	Rectangle();
	~Rectangle();
	void setvalue(int Width,int Height);
	int getArea(int width, int height);
private:
	int width, height;
};
Rectangle::Rectangle() {
	cout << "创建实例成功" << endl;
}
Rectangle::~Rectangle() {
	cout << "删除实例成功" << endl;
}
void Rectangle::setvalue(int Width,int Height) {
	cout << "实例设值:" << endl;
	width = Width;
    height=Height;
	cout << "矩阵宽度:" << width << endl;
	cout << "矩阵高度:" << height << endl;
}
int Rectangle::getArea(int width, int height) {
	cout << "矩阵的面积:" << endl;
	cout << width * height << endl;
	return 0;
}
int main() {
	Rectangle a;
	a.setvalue(2, 3);
	a.getArea(2, 3);
	//a.~Rectangle;
	a.~Rectangle();
	system("pause");
	return 0;
}

程序结果:
程序结果示意图
析构函数可以系统调用(其实构造函数也可以)但是,析构函数是在最后一句语句前被系统调用。

Rectangle::~Rectangle() {
	cout << "删除实例成功" << endl;
	system("pause");
}
......
......
int main() {
	Rectangle a;
	a.setvalue(2, 3);
	a.getArea(2, 3);
	//a.~Rectangle();
	return 0;
}

输出结果和上面的截图一样。
对于无参数的析构函数和构造函数调用时的形式:
构造函数:对象.构造函数名; or 对象.构造函数名();
析构函数:对象.~析构函数名();
注意细节。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值