栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++学习笔记(二)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++学习笔记(二)

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 );
	}
	
	//error

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

};
int main() {
	Mystring ys("buling!");
	Mystring ds(ys);
	ys.Print_Str();
	ds.Print_Str();
	return 0;
}
7.无参的对象定义方法:

Complex    c1;记住不要在后面加括号

8.用 return 返回一个有参的对象时应该写:return Complex(x,y),不用为其起一个c1名称 9.运算符重载(operator)
	Complex operator+(const Complex& c3) {
10.Complex复数类型  代码段
#include 
#include
#include
#include
using namespace std;
//复数类
class Complex {
private:
	int real;
	int image;
public:
	Complex() :real(0), image(0) {
		cout << "无参构造函数" << this << endl;
	}
	Complex(int x, int y) :real(x), image(y) {
		cout << "有参的构造函数" << this << endl;
	}
	Complex(const Complex& sc) {
		cout << "构造函数" << this << endl;
		real = sc.real;
		image = sc.image;
	}
	~Complex() {
		cout << "析构函数" << this<this     c3
	Complex operator+(const Complex& c3) {
		int x = real + c3.real;
		int y = image + c3.image;
		return Complex(x,y);
	}
};
int main() {
	
	Complex c2(2, 3);
	Complex c1;
	Complex c3(c2);
	c2.show();
	
	c3.show();
	c1 = c2+c3;
	c1.show();
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1010084.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号