自从学了C++之后,小雅兰就有对象了!!!(类与对象)(中)——“C++”

news2024/9/20 22:35:03

各位CSDN的uu们好呀,好久没有更新小雅兰的C++专栏啦,话不多说,让我们进入类和对象的世界吧!!!


类的6个默认成员函数

构造函数

析构函数

拷贝构造函数


类的6个默认成员函数

如果一个类中什么成员都没有,简称为空类。

空类中真的什么都没有吗?并不是,任何类在什么都不写时,编译器会自动生成以下6个默认成员 函数。

默认成员函数:用户没有显式实现,编译器会生成的成员函数称为默认成员函数。

class Date {};


构造函数

概念

对于以下Date类:

#include<iostream>
using namespace std;
class Date
{
public:
	void Init(int year, int month, int day)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}

private:
	int _year;
	int _month;
	int _day;
};

int main()
{
	Date d1;
	d1.Init(2023, 8, 9);
	d1.Print();
	Date d2;
	d2.Init(2023, 8, 10);
	d2.Print();
	return 0;
}

对于Date类,可以通过 Init 公有方法给对象设置日期,但如果每次创建对象时都调用该方法设置信息,未免有点麻烦,那能否在对象创建时,就将信息设置进去呢?

构造函数是一个特殊的成员函数,名字与类名相同,创建类类型对象时由编译器自动调用,以保证每个数据成员都有 一个合适的初始值,并且在对象整个生命周期内只调用一次。

特性

构造函数是特殊的成员函数,需要注意的是,构造函数虽然名称叫构造,但是构造函数的主要任务并不是开空间创建对象,而是初始化对象。  

也就是说,构造函数类比于Init函数!!!

其特征如下:

  • 函数名与类名相同。
  • 无返回值(不需要写void)。
  • 对象实例化时编译器自动调用对应的构造函数。
  • 构造函数可以重载(本质就是写多个构造函数,提供多种初始化方式)。
class Date
{
public:
	//无参构造函数
	Date()
	{
		cout << "Date()" << endl;
		_year = 1;
		_month = 1;
		_day = 1;
	}
	//带参构造函数
	Date(int year, int month, int day)
	{
		cout << "Date(int year, int month, int day)" << endl;
		_year = year;
		_month = month;
		_day = day;
	}
	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	// 调用无参构造函数
	Date d1;
	d1.Print();
	// 调用带参的构造函数
	Date d2(2023, 8, 9);
	d2.Print();
	// 注意:如果通过无参构造函数创建对象时,对象后面不用跟括号,否则就成了函数声明
	// 以下代码的函数:声明了d3函数,该函数无参,返回一个日期类型的对象
	// warning C4930: “Date d3(void)”: 未调用原型函数(是否是有意用变量定义的?)
	Date d3();
	d3.Print();

	return 0;
}

 

//无参构造函数
    Date()
    {
        cout << "Date()" << endl;
        _year = 1;
        _month = 1;
        _day = 1;
    }
    //带参构造函数
    Date(int year, int month, int day)
    {
        cout << "Date(int year, int month, int day)" << endl;
        _year = year;
        _month = month;
        _day = day;
    } 

上述两个函数其实可以合并一下,写成全缺省参数的形式。

//带参构造函数
Date(int year = 1, int month = 1, int day = 1)
{
	cout << "Date(int year, int month, int day)" << endl;
	_year = year;
	_month = month;
	_day = day;
}

这样的写法也更灵活了,可以传一个参数,也可以传两个参数,也可以传三个参数,也可以不传参数。

 再看下面这个实例:

class Stack
{
public:
	Stack(size_t n = 4)
	{
		if (n == 0)
		{
			a = nullptr;
			top = capacity = 0;
		}
		else
		{
			a = (int*)malloc(sizeof(int) * n);
			if(a == nullptr)
			{
				perror("realloc fail");
				exit(-1);
			}

			top = 0;
			capacity = n;
		}
	}
	void Push(int x)
	{
		if (top == capacity)
		{
			size_t newcapacity = capacity == 0 ? 4 : capacity * 2;
			int* tmp = (int*)realloc(a, sizeof(int) * newcapacity);
			if (tmp == nullptr)
			{
				perror("realloc fail");
				exit(-1);
			}
			if (tmp == a)
			{
				cout << capacity << "原地扩容" << endl;
			}
			else
			{
				cout << capacity << "异地扩容" << endl;
			}

			a = tmp;
			capacity = newcapacity;
		}

		a[top++] = x;
	}

	int Top()
	{
		return a[top - 1];
	}

	void Pop()
	{
		assert(top > 0);
		--top;
	}

	void Destroy()
	{
		free(a);
		a = nullptr;
		top = capacity = 0;
	}

	bool Empty()
	{
		return top == 0;
	}
private:
	// 成员变量
	int* a;
	int top;
	int capacity;
};

int main()
{
	Stack st1;
	st1.Push(1);
	st1.Push(2);
	st1.Push(3);
	st1.Push(4);
	
	while (!st1.Empty())
	{
		cout << st1.Top() << " ";
		st1.Pop();
	}
	cout << endl;

	st1.Destroy();

	//Stack st2(1000);
	Stack st2;
	for (size_t i = 0; i < 1000; i++)
	{
		st2.Push(i);
	}

	while (!st2.Empty())
	{
		cout << st2.Top() << " ";
		st2.Pop();
	}
	cout << endl;

	st2.Destroy();
	return 0;
}

 

 

构造函数,是默认成员函数,不写,编译器会自动生成。

编译生成的默认构造的特点:

  • 我们不写才会生成,我们写了就不会生成了。
  • 内置类型的成员不会处理(C++11,声明支持给缺省值)。
  • 自定义类型的成员才会处理,会去调用这个成员的默认构造函数。

如果类中没有显式定义构造函数,则C++编译器会自动生成一个无参的默认构造函数,一旦 用户显式定义编译器将不再生成。

class Date
{
public:
	如果用户显式定义了构造函数,编译器将不再生成
	//Date(int year, int month, int day)
	//{
	//	_year = year;
	//	_month = month;
	//	_day = day;
	//}
	
	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}

private:
	int _year;
	int _month;
	int _day;
};

int main()
{
	// 将Date类中构造函数屏蔽后,代码可以通过编译,因为编译器生成了一个无参的默认构造函数
	// 将Date类中构造函数放开,代码编译失败,因为一旦显式定义任何构造函数,编译器将不再生成
	// 无参构造函数,放开后报错:error C2512: “Date”: 没有合适的默认构造函数可用
	Date d1;
	d1.Print();
	return 0;
}

 

所以,默认生成的构造函数一般没什么价值,但是在有一些场景下非常有价值,之前小雅兰写过一个题目,就是两个栈实现一个队列,既可以写构造函数,也可以不写。

 

// 两个栈实现一个队列
class MyQueue
{
private:
	Stack _pushst;
	Stack _popst;
};

总结:一般情况都需要我们自己写构造函数,决定初始化方式

成员变量全是自定义类型,可以考虑不写构造函数

关于编译器生成的默认成员函数,很多人会有疑惑:不实现构造函数的情况下,编译器会 生成默认的构造函数。但是看起来默认构造函数又没什么用?d对象调用了编译器生成的默 认构造函数,但是d对象_year/_month/_day,依旧是随机值。也就说在这里编译器生成的默认构造函数并没有什么用??

解答:C++把类型分成内置类型(基本类型)和自定义类型。内置类型就是语言提供的数据类 型,如:int/char...,自定义类型就是我们使用class/struct/union等自己定义的类型,看看 下面的程序,就会发现编译器生成默认的构造函数会对自定类型成员_t调用的它的默认构造函数。

int*和Date*(指针)都是内置类型!!!

class Time
{
public:
	Time()
	{
		cout << "Time()" << endl;
		_hour = 0;
		_minute = 0;
		_second = 0;
	}

private:
	int _hour;
	int _minute;
	int _second;
};

class Date
{
private:
	// 基本类型(内置类型)
	int _year;
	int _month;
	int _day;
	// 自定义类型
	Time _t;
};

int main()
{
	Date d;
	return 0;
}

 

注意:C++11 中针对内置类型成员不初始化的缺陷,又打了补丁,即:内置类型成员变量在 类中声明时可以给默认值。

class Time
{
public:
	Time()
	{
		cout << "Time()" << endl;
		_hour = 0;
		_minute = 0;
		_second = 0;
	}
private:
	int _hour;
	int _minute;
	int _second;
};

class Date
{
private:
	// 基本类型(内置类型)
	int _year = 1970;
	int _month = 1;
	int _day = 1;
	// 自定义类型
	Time _t;
};

int main()
{
	Date d;
	return 0;
}

无参的构造函数和全缺省的构造函数都称为默认构造函数,并且默认构造函数只能有一个。 注意:无参构造函数、全缺省构造函数、我们没写编译器默认生成的构造函数,都可以认为 是默认构造函数。 

不传参就可以调用的构造就是默认构造!!!

class Date
{
public:
	Date()
	{
		_year = 1900;
		_month = 1;
		_day = 1;
	}
	Date(int year = 1900, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}

private:
	int _year;
	int _month;
	int _day;
};

// 以下测试函数能通过编译吗?
void Test()
{
	Date d1;
}
int main()
{
	Test();
	return 0;
}

上述代码是无法通过编译的。

如果是把无参构造函数屏蔽掉或者是把全缺省构造函数屏蔽掉,就可以通过编译了!!!!


析构函数

概念

通过前面构造函数的学习,我们知道一个对象是怎么来的,那一个对象又是怎么没呢的?

析构函数:与构造函数功能相反,析构函数不是完成对对象本身的销毁,局部对象销毁工作是由 编译器完成的。而对象在销毁时会自动调用析构函数,完成对象中资源的清理工作。

也就是说,析构函数类比于Destroy函数!!!

特性

析构函数是特殊的成员函数,其特征如下:

  • 析构函数名是在类名前加上字符 ~。
  • 无参数无返回值类型。
  • 一个类只能有一个析构函数。若未显式定义,系统会自动生成默认的析构函数。注意:析构 函数不能重载。
  • 对象生命周期结束时,C++编译系统系统自动调用析构函数。

默认的析构函数跟默认构造函数类似:内置类型成员不会处理,自定义类型成员会调用这个成员的析构函数。

class Date
{
public:
	Date(int year = 1, int month = 1, int day = 1)
	{
		cout << "Date(int year = 1, int month = 1, int day = 1)" << endl;

		_year = year;
		_month = month;
		_day = day;
	}
	void Print()
	{
		cout << _year << "/" << _month << "/" << _day << endl;
	}

	~Date()
	{
		cout << "~Date()" << endl;
	}

private:
	int _year = 1;   // 声明给的缺省值
	int _month = 1;
	int _day = 1;
};

class Stack
{
public:
	Stack(size_t n = 4)
	{
		cout << "Stack(size_t n = 4)" << endl;

		if (n == 0)
		{
			a = nullptr;
			top = capacity = 0;
		}
		else
		{
			a = (int*)malloc(sizeof(int) * n);
			if (a == nullptr)
			{
				perror("realloc fail");
				exit(-1);
			}

			top = 0;
			capacity = n;
		}
	}

	~Stack()
	{
		cout << "~Stack()" << endl;
		free(a);
		a = nullptr;
		top = capacity = 0;
	}

	void Push(int x)
	{
		if (top == capacity)
		{
			size_t newcapacity = capacity == 0 ? 4 : capacity * 2;
			int* tmp = (int*)realloc(a, sizeof(int) * newcapacity);
			if (tmp == nullptr)
			{
				perror("realloc fail");
				exit(-1);
			}
			if (tmp == a)
			{
				cout << capacity << "原地扩容" << endl;
			}
			else
			{
				cout << capacity << "异地扩容" << endl;
			}

			a = tmp;
			capacity = newcapacity;
		}

		a[top++] = x;
	}

	int Top()
	{
		return a[top - 1];
	}

	void Pop()
	{
		assert(top > 0);
		--top;
	}

	void Destroy()
	{
		free(a);
		a = nullptr;
		top = capacity = 0;
	}

	bool Empty()
	{
		return top == 0;
	}
private:
	// 成员变量
	int* a;
	int top;
	int capacity;
};

// 两个栈实现一个队列
class MyQueue
{
private:
	Stack _pushst;
	Stack _popst;
};
int main()
{
	Date d1;
	Date d2;

	Stack st1;
	Stack st2;
	return 0;
}

 其实日期类不需要写析构函数!!!

 像栈这样的数据结构,就需要写析构函数!!!

 

关于编译器自动生成的析构函数,是否会完成一些事情呢?下面的程序我们会看到,编译器 生成的默认析构函数,对自定类型成员调用它的析构函数。

如果类中没有申请资源时,析构函数可以不写,直接使用编译器生成的默认析构函数,比如

Date类;有资源申请时,一定要写,否则会造成资源泄漏,比如Stack类。

class Time
{
public:
	~Time()
	{
		cout << "~Time()" << endl;
	}
private:
	int _hour;
	int _minute;
	int _second;
};

class Date
{
private:
	// 基本类型(内置类型)
	int _year = 1970;
	int _month = 1;
	int _day = 1;

	// 自定义类型
	Time _t;
};

int main()
{
	Date d;
	return 0;
}

// 程序运行结束后输出:~Time()
// 在main函数中根本没有直接创建Time类的对象,为什么最后会调用Time类的析构函数?
// 因为:main方法中创建了Date对象d,而d中包含4个成员变量,其中_year, _month, _day三个是
// 内置类型成员,销毁时不需要资源清理,最后系统直接将其内存回收即可;而_t是Time类对象,所以在
// d销毁时,要将其内部包含的Time类的_t对象销毁,所以要调用Time类的析构函数。但是:main函数
// 中不能直接调用Time类的析构函数,实际要释放的是Date类对象,所以编译器会调用Date类的析构函
// 数,而Date没有显式提供,则编译器会给Date类生成一个默认的析构函数,目的是在其内部调用Time
// 类的析构函数,即当Date对象销毁时,要保证其内部每个自定义对象都可以正确销毁
// main函数中并没有直接调用Time类析构函数,而是显式调用编译器为Date类生成的默认析构函数
// 注意:创建哪个类的对象则调用该类的析构函数,销毁那个类的对象则调用该类的析构函数

 


拷贝构造函数

概念

在现实生活中,可能存在一个与你一样的自己,我们称其为双胞胎。

那在创建对象时,可否创建一个与已存在对象一模一样的新对象呢?  

拷贝构造函数:只有单个形参,该形参是对本类类型对象的引用(一般常用const修饰),在用已存 在的类类型对象创建新对象时由编译器自动调用。

特征

拷贝构造函数也是特殊的成员函数,其特征如下:

  • 拷贝构造函数是构造函数的一个重载形式。
  • 拷贝构造函数的参数只有一个且必须是类类型对象(同类型的对象)的引用,使用传值方式编译器直接报错, 因为会引发无穷递归调用

class Date
{
public:
	Date(int year = 1900, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	// Date(const Date& d)// 正确写法
	Date(const Date d)// 错误写法:编译报错,会引发无穷递归
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
private:
	int _year;
	int _month;
	int _day;
};

int main()
{
	Date d1;
	Date d2(d1);
	return 0;
}

 

 

 

class Date
{
public:
	Date(int year = 1, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}

	Date(Date& d)
	{
		cout << "Date(Date& d)" << endl;

		_year = d._year;
		_month = d._month;
		_day = d._day;
	}

	void Print()
	{
		cout << _year << "/" << _month << "/" << _day << endl;
	}
	
private:
	// 内置类型
	int _year;
	int _month;
	int _day;
};

typedef int DataType;
class Stack
{
public:
	Stack(size_t capacity = 3)
	{
		_array = (DataType*)malloc(sizeof(DataType) * capacity);
		if (NULL == _array)
		{
			perror("malloc申请空间失败!!!");
			return;
		}

		_capacity = capacity;
		_size = 0;
	}

	Stack(Stack& s)
	{
		cout << "Stack(Stack& s)" << endl;
		// 深拷贝
		_array = (DataType*)malloc(sizeof(DataType) * s._capacity);
		if (NULL == _array)
		{
			perror("malloc申请空间失败!!!");
			return;
		}

		memcpy(_array, s._array, sizeof(DataType) * s._size);
		_size = s._size;
		_capacity = s._capacity;
	}

	void Push(DataType data)
	{
		_array[_size] = data;
		_size++;
	}

	~Stack()
	{
		cout << "~Stack()" << endl;
		free(_array);
		_array = nullptr;
		_size = _capacity = 0;
	}
private:
	// 内置类型
	DataType* _array;
	int _capacity;
	int _size;
};

void func1(Date d)
{
	d.Print();
}
// 期望呢,s要插入一些数据,s的改变,不影响s1
void func2(Stack s)
{
	s.Push(1);
	s.Push(2);
}

int main()
{
	Date d1(2023, 7, 21);
	func1(d1);

	Stack s1;
	func2(s1);

	Stack s2(s1);

	// 以下两个写法是等价的,都是拷贝构造
	Date d2(d1);
	Date d3 = d1;


	return 0;
}

 

 

 

若未显式定义,编译器会生成默认的拷贝构造函数。 默认的拷贝构造函数对象按内存存储按 字节序完成拷贝,这种拷贝叫做浅拷贝,或者值拷贝。

class Time
{
public:
	Time()
	{
		_hour = 1;
		_minute = 1;
		_second = 1;
	}
	Time(const Time& t)
	{
		_hour = t._hour;
		_minute = t._minute;
		_second = t._second;
		cout << "Time::Time(const Time&)" << endl;
	}
private:
	int _hour;
	int _minute;
	int _second;
};

class Date
{
private:
	// 基本类型(内置类型)
	int _year = 1970;
	int _month = 1;
	int _day = 1;

	// 自定义类型
	Time _t;
};

int main()
{
	Date d1;
	// 用已经存在的d1拷贝构造d2,此处会调用Date类的拷贝构造函数
	// 但Date类并没有显式定义拷贝构造函数,则编译器会给Date类生成一个默认的拷贝构造函数
	Date d2(d1);
	return 0;
}

 注意:在编译器生成的默认拷贝构造函数中,内置类型是按照字节方式直接拷贝的,而自定 义类型是调用其拷贝构造函数完成拷贝的。

我们不写,编译默认生成的拷贝构造,跟之前的构造函数特性不一样

  • 内置类型, 值拷贝
  • 自定义的类型,调用它的拷贝 

总结:Date不需要我们实现拷贝构造,默认生成就可以用

        Stack需要我们自己实现深拷贝的拷贝构造,默认生成会出问题

MyQueue对于默认生成的几个函数非常受用,人生赢家

class MyQueue
{
private:
    Stack _pushst;
    Stack _popst;
};

MyQueue mq1;
MyQueue mq2 = mq1;

 

编译器生成的默认拷贝构造函数已经可以完成字节序的值拷贝了,还需要自己显式实现吗? 当然像日期类这样的类是没必要的。那么下面的类呢?验证一下试试?

// 这里会发现下面的程序会崩溃掉?这里就需要我们以后学的深拷贝去解决。
typedef int DataType;
class Stack
{
public:
	Stack(size_t capacity = 10)
	{
		_array = (DataType*)malloc(capacity * sizeof(DataType));
		if (nullptr == _array)
		{
			perror("malloc申请空间失败");
			return;
		}
		_size = 0;
		_capacity = capacity;
	}
	void Push(const DataType& data)
	{
		// CheckCapacity();
		_array[_size] = data;
		_size++;
	}
	~Stack()
	{
		if (_array)
		{
			free(_array);
			_array = nullptr;
			_capacity = 0;
			_size = 0;
		}
	}

private:
	DataType* _array;
	size_t _size;
	size_t _capacity;
};

int main()
{
	Stack s1;
	s1.Push(1);
	s1.Push(2);
	s1.Push(3);
	s1.Push(4);
	Stack s2(s1);
	return 0;
}

 

注意:类中如果没有涉及资源申请时,拷贝构造函数是否写都可以;一旦涉及到资源申请 时,则拷贝构造函数是一定要写的,否则就是浅拷贝。  

拷贝构造函数典型调用场景:

  1. 使用已存在对象创建新对象
  2. 函数参数类型为类类型对象
  3. 函数返回值类型为类类型对象  
class Date
{
public:
	Date(int year, int minute, int day)
	{
		cout << "Date(int,int,int):" << this << endl;
	}
	Date(const Date& d)
	{
		cout << "Date(const Date& d):" << this << endl;
	}
	~Date()
	{
		cout << "~Date():" << this << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
Date Test(Date d)
{
	Date temp(d);
	return temp;
}
int main()
{
	Date d1(2022, 1, 13);
	Test(d1);
	return 0;
}

为了提高程序效率,一般对象传参时,尽量使用引用类型,返回时根据实际场景,能用引用尽量使用引用。  


所有源代码如下:

#include<iostream>
#include<assert.h>
using namespace std;
class Date
{
public:
    void Init(int year, int month, int day)
    {
        _year = year;
        _month = month;
        _day = day;
    }
    void Print()
    {
        cout << _year << "-" << _month << "-" << _day << endl;
    }

private:
    int _year;
    int _month;
    int _day;
};

int main()
{
    Date d1;
    d1.Init(2023, 8, 9);
    d1.Print();
    Date d2;
    d2.Init(2023, 8, 10);
    d2.Print();
    return 0;
}

 

class Date
{
public:
    无参构造函数
    //Date()
    //{
    //    cout << "Date()" << endl;
    //    _year = 1;
    //    _month = 1;
    //    _day = 1;
    //}
    带参构造函数
    //Date(int year, int month, int day)
    //{
    //    cout << "Date(int year, int month, int day)" << endl;
    //    _year = year;
    //    _month = month;
    //    _day = day;
    //}
    //带参构造函数
    Date(int year = 1, int month = 1, int day = 1)
    {
        cout << "Date(int year, int month, int day)" << endl;
        _year = year;
        _month = month;
        _day = day;
    }
    void Print()
    {
        cout << _year << "-" << _month << "-" << _day << endl;
    }
private:
    int _year;
    int _month;
    int _day;
};
int main()
{
    // 调用无参构造函数
    Date d1;
    d1.Print();
    // 调用带参的构造函数
    Date d2(2023, 8, 9);
    d2.Print();
    // 注意:如果通过无参构造函数创建对象时,对象后面不用跟括号,否则就成了函数声明
    // 以下代码的函数:声明了d3函数,该函数无参,返回一个日期类型的对象
    // warning C4930: “Date d3(void)”: 未调用原型函数(是否是有意用变量定义的?)
    //Date d3();
    //d3.Print();
    Date d3(2023);
    d3.Print();
    Date d4(2023, 8);
    d4.Print();
    return 0;
}

class Stack
{
public:
    Stack(size_t n = 4)
    {
        if (n == 0)
        {
            a = nullptr;
            top = capacity = 0;
        }
        else
        {
            a = (int*)malloc(sizeof(int) * n);
            if(a == nullptr)
            {
                perror("realloc fail");
                exit(-1);
            }

            top = 0;
            capacity = n;
        }
    }
    void Push(int x)
    {
        if (top == capacity)
        {
            size_t newcapacity = capacity == 0 ? 4 : capacity * 2;
            int* tmp = (int*)realloc(a, sizeof(int) * newcapacity);
            if (tmp == nullptr)
            {
                perror("realloc fail");
                exit(-1);
            }
            if (tmp == a)
            {
                cout << capacity << "原地扩容" << endl;
            }
            else
            {
                cout << capacity << "异地扩容" << endl;
            }

            a = tmp;
            capacity = newcapacity;
        }

        a[top++] = x;
    }

    int Top()
    {
        return a[top - 1];
    }

    void Pop()
    {
        assert(top > 0);
        --top;
    }

    void Destroy()
    {
        free(a);
        a = nullptr;
        top = capacity = 0;
    }

    bool Empty()
    {
        return top == 0;
    }
private:
    // 成员变量
    int* a;
    int top;
    int capacity;
};

int main()
{
    Stack st1;
    st1.Push(1);
    st1.Push(2);
    st1.Push(3);
    st1.Push(4);
    
    while (!st1.Empty())
    {
        cout << st1.Top() << " ";
        st1.Pop();
    }
    cout << endl;

    st1.Destroy();

    Stack st2(1000);
    //Stack st2;
    for (size_t i = 0; i < 1000; i++)
    {
        st2.Push(i);
    }

    while (!st2.Empty())
    {
        cout << st2.Top() << " ";
        st2.Pop();
    }
    cout << endl;

    st2.Destroy();
    return 0;
}

class Date
{
public:
    如果用户显式定义了构造函数,编译器将不再生成
    //Date(int year, int month, int day)
    //{
    //    _year = year;
    //    _month = month;
    //    _day = day;
    //}
    
    void Print()
    {
        cout << _year << "-" << _month << "-" << _day << endl;
    }

private:
    int _year;
    int _month;
    int _day;
};

int main()
{
    // 将Date类中构造函数屏蔽后,代码可以通过编译,因为编译器生成了一个无参的默认构造函数
    // 将Date类中构造函数放开,代码编译失败,因为一旦显式定义任何构造函数,编译器将不再生成
    // 无参构造函数,放开后报错:error C2512: “Date”: 没有合适的默认构造函数可用
    Date d1;
    d1.Print();
    return 0;
}

// 构造函数,也是默认成员函数,我们不写,编译器会自动生成
// 编译生成的默认构造的特点:
// 1、我们不写才会生成,我们写了任意一个构造函数就不会生成了
// 2、内置类型的成员不会处理(C++11,声明支持给缺省值)
// 3、自定义类型的成员才会处理,回去调用这个成员的默认构造函数

// 总结:一般情况都需要我们自己写构造函数,决定初始化方式
// 成员变量全是自定义类型,可以考虑不写构造函数

 

class Time
{
public:
    Time()
    {
        cout << "Time()" << endl;
        _hour = 0;
        _minute = 0;
        _second = 0;
    }

private:
    int _hour;
    int _minute;
    int _second;
};

class Date
{
private:
    // 基本类型(内置类型)
    int _year;
    int _month;
    int _day;
    // 自定义类型
    Time _t;
};

int main()
{
    Date d;
    return 0;
}

 

class Time
{
public:
    Time()
    {
        cout << "Time()" << endl;
        _hour = 0;
        _minute = 0;
        _second = 0;
    }
private:
    int _hour;
    int _minute;
    int _second;
};

class Date
{
private:
    // 基本类型(内置类型)
    int _year = 1970;
    int _month = 1;
    int _day = 1;
    // 自定义类型
    Time _t;
};

int main()
{
    Date d;
    return 0;
}

 

class Date
{
public:
    Date()
    {
        _year = 1900;
        _month = 1;
        _day = 1;
    }
    Date(int year = 1900, int month = 1, int day = 1)
    {
        _year = year;
        _month = month;
        _day = day;
    }

private:
    int _year;
    int _month;
    int _day;
};

// 以下测试函数能通过编译吗?
void Test()
{
    Date d1;
}
int main()
{
    Test();
    return 0;
}

 

class Date
{
public:
    Date(int year = 1, int month = 1, int day = 1)
    {
        cout << "Date(int year = 1, int month = 1, int day = 1)" << endl;

        _year = year;
        _month = month;
        _day = day;
    }
    void Print()
    {
        cout << _year << "/" << _month << "/" << _day << endl;
    }

    ~Date()
    {
        cout << "~Date()" << endl;
    }

private:
    int _year = 1;   // 声明给的缺省值
    int _month = 1;
    int _day = 1;
};

class Stack
{
public:
    Stack(size_t n = 4)
    {
        cout << "Stack(size_t n = 4)" << endl;

        if (n == 0)
        {
            a = nullptr;
            top = capacity = 0;
        }
        else
        {
            a = (int*)malloc(sizeof(int) * n);
            if (a == nullptr)
            {
                perror("realloc fail");
                exit(-1);
            }

            top = 0;
            capacity = n;
        }
    }

    ~Stack()
    {
        cout << "~Stack()" << endl;
        free(a);
        a = nullptr;
        top = capacity = 0;
    }

    void Push(int x)
    {
        if (top == capacity)
        {
            size_t newcapacity = capacity == 0 ? 4 : capacity * 2;
            int* tmp = (int*)realloc(a, sizeof(int) * newcapacity);
            if (tmp == nullptr)
            {
                perror("realloc fail");
                exit(-1);
            }
            if (tmp == a)
            {
                cout << capacity << "原地扩容" << endl;
            }
            else
            {
                cout << capacity << "异地扩容" << endl;
            }

            a = tmp;
            capacity = newcapacity;
        }

        a[top++] = x;
    }

    int Top()
    {
        return a[top - 1];
    }

    void Pop()
    {
        assert(top > 0);
        --top;
    }

    void Destroy()
    {
        free(a);
        a = nullptr;
        top = capacity = 0;
    }

    bool Empty()
    {
        return top == 0;
    }
private:
    // 成员变量
    int* a;
    int top;
    int capacity;
};

// 两个栈实现一个队列
class MyQueue
{
private:
    Stack _pushst;
    Stack _popst;
};
int main()
{
    Date d1;
    Date d2;

    Stack st1;
    Stack st2;
    return 0;
}

 

 

class Time
{
public:
    ~Time()
    {
        cout << "~Time()" << endl;
    }
private:
    int _hour;
    int _minute;
    int _second;
};

class Date
{
private:
    // 基本类型(内置类型)
    int _year = 1970;
    int _month = 1;
    int _day = 1;

    // 自定义类型
    Time _t;
};

int main()
{
    Date d;
    return 0;
}

// 程序运行结束后输出:~Time()
// 在main函数中根本没有直接创建Time类的对象,为什么最后会调用Time类的析构函数?
// 因为:main方法中创建了Date对象d,而d中包含4个成员变量,其中_year, _month, _day三个是
// 内置类型成员,销毁时不需要资源清理,最后系统直接将其内存回收即可;而_t是Time类对象,所以在
// d销毁时,要将其内部包含的Time类的_t对象销毁,所以要调用Time类的析构函数。但是:main函数
// 中不能直接调用Time类的析构函数,实际要释放的是Date类对象,所以编译器会调用Date类的析构函
// 数,而Date没有显式提供,则编译器会给Date类生成一个默认的析构函数,目的是在其内部调用Time
// 类的析构函数,即当Date对象销毁时,要保证其内部每个自定义对象都可以正确销毁
// main函数中并没有直接调用Time类析构函数,而是显式调用编译器为Date类生成的默认析构函数
// 注意:创建哪个类的对象则调用该类的析构函数,销毁那个类的对象则调用该类的析构函数
 

class Date
{
public:
    Date(int year = 1, int month = 1, int day = 1)
    {
        _year = year;
        _month = month;
        _day = day;
    }

    Date(Date& d)
    {
        cout << "Date(Date& d)" << endl;

        _year = d._year;
        _month = d._month;
        _day = d._day;
    }

    void Print()
    {
        cout << _year << "/" << _month << "/" << _day << endl;
    }
    
private:
    // 内置类型
    int _year;
    int _month;
    int _day;
};

typedef int DataType;
class Stack
{
public:
    Stack(size_t capacity = 3)
    {
        _array = (DataType*)malloc(sizeof(DataType) * capacity);
        if (NULL == _array)
        {
            perror("malloc申请空间失败!!!");
            return;
        }

        _capacity = capacity;
        _size = 0;
    }

    Stack(Stack& s)
    {
        cout << "Stack(Stack& s)" << endl;
        // 深拷贝
        _array = (DataType*)malloc(sizeof(DataType) * s._capacity);
        if (NULL == _array)
        {
            perror("malloc申请空间失败!!!");
            return;
        }

        memcpy(_array, s._array, sizeof(DataType) * s._size);
        _size = s._size;
        _capacity = s._capacity;
    }

    void Push(DataType data)
    {
        _array[_size] = data;
        _size++;
    }

    ~Stack()
    {
        cout << "~Stack()" << endl;
        free(_array);
        _array = nullptr;
        _size = _capacity = 0;
    }
private:
    // 内置类型
    DataType* _array;
    int _capacity;
    int _size;
};

void func1(Date d)
{
    d.Print();
}
// 期望呢,s要插入一些数据,s的改变,不影响s1
void func2(Stack s)
{
    s.Push(1);
    s.Push(2);
}

int main()
{
    Date d1(2023, 7, 21);
    func1(d1);

    Stack s1;
    func2(s1);

    Stack s2(s1);

    // 以下两个写法是等价的,都是拷贝构造
    Date d2(d1);
    Date d3 = d1;


    return 0;
}

class Date
{
public:
    Date(int year = 1900, int month = 1, int day = 1)
    {
        _year = year;
        _month = month;
        _day = day;
    }
    // Date(const Date& d)// 正确写法
    Date(const Date& d)// 错误写法:编译报错,会引发无穷递归
    {
        _year = d._year;
        _month = d._month;
        _day = d._day;
    }
private:
    int _year;
    int _month;
    int _day;
};

int main()
{
    Date d1;
    Date d2(d1);
    return 0;
}

 

class Time
{
public:
    Time()
    {
        _hour = 1;
        _minute = 1;
        _second = 1;
    }
    Time(const Time& t)
    {
        _hour = t._hour;
        _minute = t._minute;
        _second = t._second;
        cout << "Time::Time(const Time&)" << endl;
    }
private:
    int _hour;
    int _minute;
    int _second;
};

class Date
{
private:
    // 基本类型(内置类型)
    int _year = 1970;
    int _month = 1;
    int _day = 1;

    // 自定义类型
    Time _t;
};

int main()
{
    Date d1;
    // 用已经存在的d1拷贝构造d2,此处会调用Date类的拷贝构造函数
    // 但Date类并没有显式定义拷贝构造函数,则编译器会给Date类生成一个默认的拷贝构造函数
    Date d2(d1);
    return 0;
}

// 这里会发现下面的程序会崩溃掉?这里就需要我们以后学的深拷贝去解决。
typedef int DataType;
class Stack
{
public:
    Stack(size_t capacity = 10)
    {
        _array = (DataType*)malloc(capacity * sizeof(DataType));
        if (nullptr == _array)
        {
            perror("malloc申请空间失败");
            return;
        }
        _size = 0;
        _capacity = capacity;
    }
    void Push(const DataType& data)
    {
        // CheckCapacity();
        _array[_size] = data;
        _size++;
    }
    ~Stack()
    {
        if (_array)
        {
            free(_array);
            _array = nullptr;
            _capacity = 0;
            _size = 0;
        }
    }

private:
    DataType* _array;
    size_t _size;
    size_t _capacity;
};

int main()
{
    Stack s1;
    s1.Push(1);
    s1.Push(2);
    s1.Push(3);
    s1.Push(4);
    Stack s2(s1);
    return 0;
}

class Date
{
public:
    Date(int year, int minute, int day)
    {
        cout << "Date(int,int,int):" << this << endl;
    }
    Date(const Date& d)
    {
        cout << "Date(const Date& d):" << this << endl;
    }
    ~Date()
    {
        cout << "~Date():" << this << endl;
    }
private:
    int _year;
    int _month;
    int _day;
};
Date Test(Date d)
{
    Date temp(d);
    return temp;
}
int main()
{
    Date d1(2022, 1, 13);
    Test(d1);
    return 0;
}

 


 好啦,小雅兰今天的学习内容就到这里啦,还要继续加油噢!!!!

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/856171.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

C 语言中,「.」与「->」有什么区别?

使用“.”的话&#xff0c;只需要声明一个结构体。格式是结构体类型名结构体名。然后通过结构体名加上“.”再加上域名&#xff0c;就可以引用结构体的域了。因为结构体的内存是自动分配的&#xff0c;就像使用int a;一样。而使用“->”的话&#xff0c;需要声明一个结构体的…

【三维编辑】Seal-3D:基于NeRF的交互式像素级编辑

文章目录 摘要一、引言二、方法2.1.基于nerf的编辑问题概述2.2.编辑指导生成2.3.即时预览的两阶段学生训练 三、实验总结 项目主页: https://windingwind.github.io/seal-3d/ 代码&#xff1a;https://github.com/windingwind/seal-3d/ 论文: https://arxiv.org/pdf/2307.15131…

vue3 动态导入src/page目录下的所有子文件,并自动注册所有页面组件

main.js添加一下代码&#xff1a; const importAll (modules) > {Object.keys(modules).forEach((key) > {const component key.replace(/src/, /).replace(.vue, );const componentName key.split(/).slice(-2, -1)[0] -page;app.component(componentName, modules…

Vue2-简介、模板语法、数据绑定、MVVM、数据代理、事件处理

&#x1f954;&#xff1a;成功之后就能光明正大地回望所有苦难 VUE-Day1 Vue简介1、Vue是什么&#xff1f;2、谁开发的&#xff1f; 发展历程&#xff1f;3、Vue的特点4、容器和实例、实例中的el和data总结 Vue模板语法插值语法指令语法 数据绑定1.单向数据绑定&#xff08;v-…

SpringBoot入职学习

一、前言 公司入职&#xff0c;第一个事是把公司项目运行起来。然后在经过几天的颠沛流离&#xff0c;遇到一个事情。在创建yml文件的时候&#xff0c;需要设置自己的配置文件。当然还是先跑起来项目&#xff0c;就使用别人的yml文件。但是&#xff0c;到springboot配置那里卡…

视频抠像软件有哪些?简单好用视频抠像软件分享

在视频后期制作中&#xff0c;抠像通常用于将视频中的某个元素从其背景中分离出来。这种处理技术可以用于各种用途&#xff0c;比如创建特效、添加背景&#xff0c;或者将视频元素组合到新场景中。在电影、电视剧和广告等专业的影视制作中&#xff0c;抠像是一个常见的技术步骤…

cesium学习记录04-坐标系

一、地理坐标系和投影坐标系的关系 地理坐标系 (Geographic Coordinate System, GCS) 定义&#xff1a;地理坐标系是一个基于三维地球表面的坐标系统。它使用经度和纬度来表示地点的位置。 特点&#xff1a; 使用经纬度来定义位置。 基于特定的地球参考椭球体。 适用于全球范…

2023河南萌新联赛第(五)场:郑州轻工业大学 --亚托莉 -我挚爱的时光-

题目描述 亚托莉&#xff0c;-我挚爱的时光- 亚托莉自身机器可能有出了一点小故障&#xff0c;希望你能帮助她解决这个问题&#xff5e; 亚托莉内部的操作系统的是 Linux 操作系统&#xff0c;不同于 Windows 操作系统。在大多数情况下&#xff0c; Linux 操作系统一般是通过…

死磕Android性能优化,卡顿原因与优化方案

随着移动互联网的快速发展&#xff0c;Android应用的性能优化变得尤为重要。卡顿是用户体验中最常见的问题之一&#xff0c;它会导致应用的响应变慢、界面不流畅&#xff0c;甚至影响用户的使用体验。因此&#xff0c;我们需要深入了解卡顿问题的原因&#xff0c;并寻找相应的解…

(Python)Requests+Pytest+Allure接口自动化测试框架从0到1搭建

前言&#xff1a;本文主要介绍在企业使用Python搭建接口自动化测试框架&#xff0c;数据驱动读取excel表里的数据&#xff0c;和数据库方面的交互&#xff0c;包括关系型数据库Mysql和非关系型数据库MongDB&#xff0c;连接数据库&#xff0c;读取数据库中数据&#xff0c;最后…

刷题DAY18

题目一 LRU算法的实现 做一个key-value结构 假如说这个LRU的大小为3 那么就是当KEY-value没满的时候 直接顺序加入 当满了的时候 把最长时间没有使用的key-value替换掉 要求实现一个put 和 get行为 时间复杂度均为O(1) 用双向链表哈希表实现 哈希表可以用系统封装的双向链表…

node笔记——调用免费qq的smtp发送html格式邮箱

文章目录 ⭐前言⭐smtp授权码获取⭐nodemailer⭐postman验证接口⭐结束 ⭐前言 大家好&#xff0c;我是yma16&#xff0c;本文分享关于node调用免费qq的smtp发送邮箱。 node系列往期文章 node_windows环境变量配置 node_npm发布包 linux_配置node node_nvm安装配置 node笔记_h…

嵌入式开发的学习与未来展望:借助STM32 HAL库开创创新之路

引言&#xff1a; 嵌入式开发作为计算机科学领域的重要分支&#xff0c;为我们的日常生活和产业发展提供了无限的可能。STMicroelectronics的STM32系列芯片以其出色的性能和广泛的应用领域而备受关注。而STM32 HAL库作为嵌入式开发的高级库&#xff0c;为学习者提供了更高效、更…

Jmeter(六) - 从入门到精通 - 建立数据库测试计划(详解教程)

1.简介 在实际工作中&#xff0c;我们经常会听到数据库的性能和稳定性等等&#xff0c;这些有时候也需要测试工程师去评估和测试&#xff0c;因此这篇文章主要介绍了jmeter连接和创建数据库测试计划的过程,在文中通过示例和代码非常详细地介绍给大家&#xff0c;希望对各位小伙…

浅谈JVM中的即时编译器(Just-In-Time compiler, JIT)

Java虚拟机&#xff08;JVM&#xff09;中的即时编译器&#xff08;Just-In-Time compiler, JIT&#xff09;是一个非常重要的组件&#xff0c;它负责将字节码转换为本地机器代码。在不使用JIT的情况下&#xff0c;JVM通过解释字节码来执行程序&#xff0c;这意味着它会为每个字…

24届近5年上海理工大学自动化考研院校分析

今天学姐给大家带来的是上海理工大学控制考研分析 满满干货&#xff5e;还不快快点赞收藏 一、上海理工大学 学校简介 上海理工大学&#xff08;University of Shanghai for Science and Technology&#xff09;是一所以工学为主&#xff0c;工学、理学、经济学、管理学、文…

如何实现Excel中多级数据联动

摘要&#xff1a;本文由葡萄城技术团队于CSDN原创并首发。转载请注明出处&#xff1a;葡萄城官网&#xff0c;葡萄城为开发者提供专业的开发工具、解决方案和服务&#xff0c;赋能开发者。 前言 在类Excel表格应用中&#xff0c;常用的需求场景是根据单元格之间的数据联动&…

Leetcode-每日一题【剑指 Offer 13. 机器人的运动范围】

题目 地上有一个m行n列的方格&#xff0c;从坐标 [0,0] 到坐标 [m-1,n-1] 。一个机器人从坐标 [0, 0] 的格子开始移动&#xff0c;它每次可以向左、右、上、下移动一格&#xff08;不能移动到方格外&#xff09;&#xff0c;也不能进入行坐标和列坐标的数位之和大于k的格子。例…

css3背景渐变

1.线性渐变 <style>.box {width: 200px;height: 200px;border: 1px solid black;float: left;margin-left: 50px;}.box1 {background-image: linear-gradient(green, yellow, red);}/* 右上 */.box2 {background-image: linear-gradient(to right top, green, yellow, re…

Fortinet安全专家问答实录|如何防护暴力破解、撞库攻击

黑客攻防&#xff0c;一个看似神秘&#xff0c;但却必不可缺的领域。近期&#xff0c;全球网络与安全融合领域领导者Fortinet&#xff08;Nasdaq&#xff1a;FTNT&#xff09;&#xff0c;开启了Fortinet DEMO DAY系列实战攻防演练线上直播&#xff0c;让人人都能零距离观摩黑客…