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 <stddef.h>
#include<string.h>
#include<iostream>
#include<string>
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 <stddef.h>
#include<string.h>
#include<iostream>
#include<string>
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.无参的对象定义方法:
Complex c1;记住不要在后面加括号
8.用 return 返回一个有参的对象时应该写:return Complex(x,y),不用为其起一个c1名称
9.运算符重载(operator)
Complex operator+(const Complex& c3) {
10.Complex复数类型 代码段
#include <stddef.h>
#include<string.h>
#include<iostream>
#include<string>
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<<endl;
}
void show() {
cout << "real:" << real << " image" << image << endl;
}
//c1=c2.Add(c3) c2->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();
}