C++奇迹之旅:构造函数和析构函数

04-26 1716阅读

C++奇迹之旅:构造函数和析构函数

文章目录

  • 📝类的6个默认成员函数
  • 🌠 构造函数
    • 🌉 概念
    • 🌉特性
    • 🌉三种默认构造函数
    • 🌠 析构函数
    • 🌠 特性
    • 🚩总结

      📝类的6个默认成员函数

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

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

      class Date {};
      

      🌠 构造函数

      🌉 概念

      对于以下Date类:

      class Date
      {
      public:
      	void Init(int year, int month, int day)
      	{
      		_year = year;
      		_month = month;
      		_day = day;
      	}
      	void Print()
      	{
      		cout 
      	Date d1;
      	d1.Init(2006, 6, 27);
      	d1.Print();
      	Date d2;
      	d2.Init(2022, 7, 6);
      	d2.Print();
      	return 0;
      }
      
      public:
      	//无参构造函数
      	Date()
      	{}
      	void print()
      	{
      		cout 
      	Date d1;//调用无参构造函数
      	
      	d1.print();
      	return 0;
      }
      
      public:
      	//带参构造函数
      	Date(int year, int month, int day)
      	{
      		_year = year;
      		_month = month;
      		_day = day;
      	}
      	void print()
      	{
      		cout 
      	Date d2(2024, 4, 15);//调用带参构造函数
      						//带参是要直接在后面加括号的
      	d2.print();
      	return 0;
      }
      
      public:
      	//如果用户显式定义了构造函数,编译器将不再生成
      	Date(int year, int month, int day)
      	{
      		_year = year;
      		_month = month;
      		_day = day;
      	}
      	void print()
      	{
      		cout 
      	Date d2(2024, 4, 15);//调用带参构造函数
      						//带参是要直接在后面加括号的
      	d2.print();
      	return 0;
      }
      
      public:
      	A()
      	{
      		_a = 0;
      		cout 
      public:
      	// 我们没写,有没有构造函数?有-编译器自动生成
      	// 内置类型/基本类型 int/char/double.../指针 
      	// 自定义类型       class/struct...
      	// 编译器自动生成构造函数,对于内置类型成员变量,没有规定要不要做处理!(有些编译器会处理)
      	//                       对于自定义类型成员变量才会调用他的无参构造
      	void Print()
      	{
      		cout 
      	Date d1;
      	d1.Print();
      	return 0;
      }
      
      public:
      	Time()
      	{
      		cout 
      private:
      	// 基本类型(内置类型)
      	int _year;
      	int _month;
      	int _day;
      	// 自定义类型
      	Time _t;
      };
      int main()
      {
      	Date d;
      	return 0;
      }
      
      public:
      	Time()
      	{
      		cout 
      private:
      	// 基本类型(内置类型)
      	int _year = 1970;
      	int _month = 1;
      	int _day = 1;
      	// 自定义类型
      	Time _t;
      };
      int main()
      {
      	Date d;
      	return 0;
      }
      
      public:
      	Time(int hour = 1, int minute = 1, int second = 1)
      	{
      		cout 
      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;
      };
      // 以下测试函数能通过编译吗?
      int Test()
      {
      	Date d1;
      	return 0;
      }
      
       _year = year;
       _month = month;
       _day = day;
       }
      
      public:
      	~Time()
      	{
      		cout 
      private:
      	// 基本类型(内置类型)
      	int _year = 1970;
      	int _month = 1;
      	int _day = 1;
      	// 自定义类型
      	Time _t;
      };
      int main()
      {
      	Date d;
      	return 0;
      }
      
      public:
      	Stack(size_t capacity = 3)
      	{
      		cout 
      			perror("malloc申请空间失败!!!");
      			return;
      		}
      		_capacity = capacity;
      		_size = 0;
      	}
      	void Push(DataType data)
      	{
      		// CheckCapacity();
      		_array[_size] = data;
      		_size++;
      	}
      	// 其他方法...
      	~Stack()
      	{
      		cout 
      			free(_array);
      			_array = NULL;
      			_capacity = 0;
      			_size = 0;
      		}
      	}
      private:
      	DataType* _array;
      	int _capacity;
      	int _size;
      };
      class MyQueue 
      {
      private:
      	Stack _st1;
      	Stack _st2;
      	int _size = 0;
      };
      int main()
      {
      	//Stack st;
      	MyQueue q;
      	return 0;
      }
      
VPS购买请点击我

文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

目录[+]