【无标题】

1.拷贝构造函数

1.1拷贝构造函数是构造函数的一种重载形式

1.2不能使用常数 只能使用引用并加const(consy Complex &cdd)

2.构造函数中字符串的拷贝用strcpy_s(&目的字符串,长度,&源字符串)

3.三种引用方案

值传递

引用传递

常引用传递

4.局部对象只能以值的类型返回&&返回类型不允许是引用类型

#除非生存期不受函数影响

5.实现栈的深拷贝

	//Mystack深拷贝函数
	Mystack(const Mystack& s) {
		_size = s._size;
		_top = s._top;
		data = new int[_size];
		memmove(data, s.data, sizeof(int) * (_top + 1));
	}
#include 
#include
#include
#include
using namespace std;
class Mystack {
	enum { total_size = 10, nsize = 2 };
private:
	int* data;
	int _size;
	int _top;
	//增容
	bool Capacity(int newSize) {
		int* newdata = new int[newSize];
		memmove(newdata, data, _size * sizeof(int));
		delete[]data;//不可省略
		data = newdata;
		_size = newSize;
		return true;
	}

public:
	Mystack() :data(nullptr), _size(total_size), _top(-1) {
		data = new int[total_size];
	}
	Mystack(int t) :data(nullptr), _size(t), _top(-1) {
		data = new int[total_size];
	}
	~Mystack() {
		delete[]data;
		data = nullptr;
		_size = 0;
		_top = -1;
	}
	//Mystack深拷贝函数
	Mystack(const Mystack& s) {
		_size = s._size;
		_top = s._top;
		data = new int[_size];
		memmove(data, s.data, sizeof(int) * (_top + 1));
	}
	//判满  判空  入栈  出栈  获取栈元素  获取栈顶元素  
	bool Full() {
		return (_top + 1) >= _size;
	}
	bool Empty() {
		return (_top == -1);
	}
	bool Push(int val) {
		if (Full() && !Capacity(nsize * _size))  return false;
		_top += 1;
		this->data[_top] = val;
		cout << val << "已入栈" << endl;
		return true;
	}
	bool Pop() {
		if (Empty()) return false;
		_top -= 1;
		return true;
	}
	bool  Get_Topvel(int& a) {
		if (Empty()) return false;
		a = this->data[_top];
		return true;
	}
	bool Pop_val(int& a) {
		if (Empty()) return false;
		a = this->data[_top];
		cout << a << "已出栈" << endl;
		_top -= 1;
	}
};
int main() {
	Mystack s;
	int c;
	for (int i = 0; i < 10; i++) {
		s.Push(i);
	}
	Mystack s1(s);
	cout << "s1" << endl;
	while (!s1.Empty()) {
		
		s1.Pop_val(c);
	}

}

6.实现字符串的深拷贝

#include 
#include
#include
#include
using namespace std;
//字符串类
class Mystring {
private:
	char* str;
public://有参 析构 拷贝
	Mystring(const char *p=nullptr):str(nullptr) {
		if (p != nullptr) {
			int len = strlen(p) + 1;
			str = new char[len];
			strcpy_s(str, len, p);
		}
	}
	~Mystring() {
		if (str != nullptr) {
			delete[]str;
		}
		str = nullptr;
	}
	Mystring(const Mystring &s) {
		int n = strlen(s.str) + 1;
		this->str = new char[n + 1];
		strcpy_s(str, n , s.str );
	}
	/*Mystring(const Mystring& s) {
		str = s.str;
	}*/
	//error

	//打印
	void Print_Str() {
		cout << str << endl;
	}

};
int main() {
	Mystring ys("buling!");
	Mystring ds(ys);
	ys.Print_Str();
	ds.Print_Str();
	return 0;
}

7.

8.

9.

10.

你可能感兴趣的:(大数据)