【C++初阶】—— 类和对象 (中)

慈云数据 2024-05-30 技术支持 48 0

📝个人主页🌹:EterNity_TiMe_

⏩收录专栏⏪:C++ “ 登神长阶 ”

🌹🌹期待您的关注 🌹🌹

在这里插入图片描述

在这里插入图片描述

类和对象

  • 1. 构造函数
    • 构造函数的概念
    • 构造函数的特征
    • 默认构造函数
    • 2. 析构函数
      • 析构函数的概念
      • 析构函数的特征
      • 默认析构函数
      • 3. 拷贝构造函数
        • 拷贝构造函数的概念
        • 拷贝构造函数的特征
        • 默认拷贝构造函数
        • 四. 总结

          前言:在参透了类的相关知识后,我们来进一步了解类的6个默认成员函数,本篇先让我们了解三个,剩下的将会在下一篇展开

          在这里插入图片描述

          如果你还没弄清类的基础知识建议先阅读这篇文章

          类的基础知识

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


          1. 构造函数

          构造函数的概念

          构造函数: 是一个特殊的成员函数,名字与类名相同,创建相同类型对象时由编译器自动调用,用来初始化对象

          构造函数可以重载

          例如:

          class Date
          {
          public:
          	// 无返回值,名字与类名相同,用来初始化对象
          	// 带参构造函数
          	Date(int year, int month, int day)
          	{
          		_year = year;
          		_month = month;
          		_day = day;
          	}
          	// 无参构造函数
              Date()
              {}
          private:
          	int _year;
          	int _month;
          	int _day;
          };
          int main()
          {
          	Date d1; // 调用无参构造函数
          	Date d2(2024,5,22); // 调用带参构造函数
          }
          

          注意: 如果通过无参构造函数创建对象时,对象后面不用跟括号,否则就成了函数声明

          构造函数的特征

          注意:

          • 构造函数是特殊的成员函数,与一般的函数不同
          • 构造函数的主要任务不是开空间创建对象,而是初始化对象。

            如果类中没有显式定义构造函数,系统就会自动生成一个默认构造函数

            class Date
            {
            public:
            	// 当我们没有写构造函数时,系统会直接生成默认构造
            	//Date(int year, int month, int day)
            	//{
            	//	_year = year;
            	//	_month = month;
            	//	_day = day;
            	//}
            	// 但如果我们显式定义了构造函数,编译器将不再生成
            private:
            	int _year;
            	int _month;
            	int _day;
            };
            int main()
            {
            	Date d1; 
            }
            
            • 将Date类中构造函数屏蔽后,代码可以通过编译,因为编译器生成了一个无参的默认构造函数
            • 将Date类中构造函数放开,代码编译失败,因为一旦我们自己定义任何构造函数,编译器将不再生成

              C++11 中针对内置类型成员不初始化的缺陷做出了改变,内置类型成员变量在类中声明时可以给默认值。

              class Date
              {
              public:
              	void Print()
               	{
              	 	cout 
              	Date d1; 
              	d1.Print(); //打印日期:2024,5,22
              }
              
              public:
              	pxt()
              	{
              		cout 
              private:
              	// 内置类型
              	int _year;
              	int _month;
              	int _day;
              	// 自定义类型
              	pxt _t;
              };
              int main()
              {
              	Date d;
              	return 0;
              }
              
              public:
               //当实例化对象时没有传参,系统不知道是调用全缺省函数还是无参的函数
                   Date()
                   {
               	 	_year = 2024;
              	 	_month = 5;
              	 	_day = 22;
              	 }
              	 Date(int year = 2024, int month = 5, int day = 22)
               	 {
              	 	_year = year;
              	 	_month = month;
               		_day = day;
              	 }
              private:
              	 int _year;
               	 int _month;
               	 int _day;
              };
              
              public:
              	
              	// 构造函数
              	Date()
              	{
              		cout 
              		cout 
              	Date d1; 
              }
              
              public:
              	Stack(size_t capacity = 3) // 构造函数
              	{
              		_array = (DataType*)malloc(sizeof(DataType) * capacity);
              		if (NULL == _array)
              		{
              			perror("malloc false");
              			return;
              		}
              		_capacity = capacity;
              		_size = 0;
              	}
              	//必须要我们手动写析构函数来释放
              	~Stack() // 析构函数
              	{
              		if (_array)
              		{
              			free(_array);
              			_array = NULL;
              			_capacity = 0;
              			_size = 0;
              		}
              	}
              private:
              	DataType* _array;
              	int _capacity;
              	int _size;
              };
              int main()
              {
              	Stack s;
              	return 0;
              }
              
              public:
              	~pxt()
              	{
              		cout 
              private:
              	// 内置类型
              	int _year;
              	int _month;
              	int _day;
              	// 自定义类型
              	pxt _t;
              };
              int main()
              {
              	Date d;
              	return 0;
              }
              
              public:
              	Date(int year = 2024, int month = 2, int day = 22)
              	{
              		_year = year;
              		_month = month;
              		_day = day;
              	}
              private:
              	int _year;
              	int _month;
              	int _day;
              };
              int main()
              {
              	Date d1(2024, 5, 20);
              	Date d2(d1); //用d1初始化d2
              	return 0;
              }
              
              public:
              	Stack(size_t capacity = 10)
              	{
              		_array = (DataType*)malloc(capacity * sizeof(DataType));
              		if (nullptr == _array)
              		{
              			perror("malloc false");
              			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;
              }
              
微信扫一扫加客服

微信扫一扫加客服

点击启动AI问答
Draggable Icon