构造函数

构造函数以及析构函数

在写有关题目的时候当出现指针的时候格外小心,因为系统只对没有指针的情况下进行自动的构造,拷贝构造,析构等操作,而又指针的时候必须人为的避免因不同的指针指向同一个 内存区域而出现的内存泄露。因此我总结了一下构造函数在有指针,无指针等情况下的一些情况,,,当然有姐有还,分配了内存,就要把它释放掉,因此简单的提了一下析构函数。

本文思路

构造函数出现的原因

类是抽象的,因此不能在类内初始化数据成员,只能通过构造函数来初始化。

无参有指针

当在成员函数中有局部变量的时候需要开辟动态空间,尤其当数组的时候不知道开辟的大小(因为无参构造的时候不能有参数进来)可以把指针定义为NULL;

无参构造无指针

就可以不用管,就函数里面什么都可以不写。

有参构造有指针

有指针:总的原则 必须开辟空间切需理解的是在main函数内开辟的空间和在构造函数内开的空间是不一样的。

Int *a, double *a 等: 基本的语法 a=new int[size];
然后可以通过for语句来一个个赋值了。
若是char型,除了开辟空间以外(开辟空间的时候要注意,要开辟strlen(传进来的字符串名)+1的空间,因为strlen不会计算字符数组坐后面的‘/0’,因此必须加一来实现开辟空间。),还需要切记必须使用strcpy;
切传进构造函数的时候用的是指针,因为传进去的是指针。
如果不是强制要求是char数组的可以用string ,这样可以避免一些易错点(如分配空间,传的时候用指针,要用strcpy等。)

#include<iostream>
using namespace std;
#include<string.h>
class Time
{
private:
int hour,minute,second;
char *name;
string timename;
public:
Time(int _hour,int _minute,int _second,char *_name,string _timename):hour(_hour),minute(_minute),second(_second),timename(_timename)
{
name=new char[30];
strcpy(name,_name);
}	
void show()
{
cout<<hour<<" "<<minute<<" "<<second<<" "<<name<<" "<<timename<<endl;
}
~Time()
{
	delete[] name;    //一维数组的释放
}
};
int main()
{
int  _hour,_minute,_second;
char *_name;
string _timename;
_name=new char[30];
cin>>_hour>>_minute>>_second>>_name>>_timename;
Time time(_hour,_minute,_second,_name,_timename);
time.show();
} 

一维数组

#include<iostream>
#include<iostream>
using namespace std;
class Student
{
private:
	int size;
	int *score;
public:
	Student()
	{
		size = 0;
		score = NULL;
	}
	//Student(int _size, int _score[])
	Student(int _size,int *_score)
	{
		size = _size;
		score = new int[size];
		for (int i = 0; i<size; i++)
			score[i] = _score[i];
	}
	void show()
	{
		for (int i = 0; i<size; i++)
			cout << score[i] << " ";
	}
	~Student()
	{
		delete[]score;
	}
};
int main()
{
	int _size;
	int *_score;
	cin >> _size;
	_score = new int[_size];
	for (int i = 0; i<_size; i++)
		cin >> _score[i];
	Student student(_size, _score);
	student.show();
	system("pause");
}

注意点

1.内存分配

在主函数和有参构造函数内部都分配了空间,因为这两个空间是不一样的,(,,,,,,不知道怎么解释,,,,,)。

2.在构造函数内传参

可以是score[],*score,这两个的意思都是指向score数组的指针。

二维数组

#include<iostream>
using namespace std;
#include<stdlib.h>
class Matrix
{
	int size;
	int **array;
public:
	Matrix()
	{
		size = 0;
		array = NULL;
	}
	Matrix(int **a, int _size)
	{
		size = _size;
		array = new int*[size];                         //必须在构造函数内也要分配空间;
		for (int h = 0; h<size; h++)
			array[h] = new int[size];

		for (int i = 0; i < size; i++)
			for (int j = 0; j < size; j++)
				array[i][j] = a[i][j];
	}


	void show()
	{
		int i, j;
		for (i = 0; i < size; i++)
		{
			for (j = 0; j < size - 1; j++)
			{
				cout << array[i][j] << " ";
			}
			cout << array[i][j];
			cout << endl;
		}
	}
	~Matrix()
	{
		for (int i = 0; i < size; i++)
			delete array[i];
		delete[] array;
	}
};

int main()
{
	int **array;
	int t;
	cin >> t;   //Juzhen  个数; 
	int n;
	cin >> n;    //矩阵节数; 
	Matrix *matrix = (Matrix*) operator new(t * sizeof(Matrix));
	for (int i = 0; i < t; i++)
	{
		array = new int*[n];
		for (int h = 0; h< n; h++)
			array[h] = new int[n];

		for (int k = 0; k < n; k++)
			for (int j = 0; j < n; j++)
				cin >> array[k][j];

		new(&matrix[i])  Matrix(array, n);
	}
	for (int i = 0; i < t; i++)
		matrix[i].show();
	return 0;
}

注意点

内存分配

在主函数和构造函数内部都动态开辟空间,,,,(不知道怎么解释)。

在构造函数传进来的时候

Matrix(int **a, int _size) ,参数只可以是二级指针,而不能是a[][]这种。

析构函数

空间撤销是通过循环,先把行释放掉,然后在释放整个空间。

posted @ 2018-06-01 23:09  &Abdulla&  阅读(161)  评论(0编辑  收藏  举报