【C++练级之路】【Lv.3】类和对象(中)(没掌握类的6个默认成员函数,那你根本就没学过C++!)

2024-02-26 1457阅读

温馨提示:这篇文章已超过400天没有更新,请注意相关的内容是否还可用!

目录

  • 引言
  • 一、类的6个默认成员函数
  • 二、构造函数(constructor)
    • 2.1 引入
    • 2.2 概念
    • 2.3 特性
    • 三、析构函数(destructor)
      • 3.1 概念
      • 3.2 特性
      • 四、拷贝构造函数(copy constructor)
        • 4.1 概念
        • 4.2 特性
        • 五、构造、析构、拷贝构造函数总结对比
          • 5.1 构造函数
          • 5.2 析构函数
          • 5.3 拷贝构造函数
          • 六、赋值运算符重载
            • 6.1 运算符重载
            • 6.2 赋值运算符重载
            • 七、日期类的实现
              • date.h
              • date.cpp
              • 八、const成员函数
                • 8.1 概念
                • 8.2 使用方式
                • 8.3 日期类(const修饰版)
                • 九、取地址及const取地址操作符重载
                • 总结

                  欢迎各位小伙伴关注我的专栏,和我一起系统学习C++,共同探讨和进步哦!

                  学习专栏:

                  《进击的C++》

                  引言

                  在C++的学习中,类和对象章节的学习尤为重要,犹如坚固的地基,基础不牢,地动山摇;而默认成员函数的学习,在类和对象的学习里最为重要。所以要学好C++,学好默认成员函数是一道必经之路,这样后续才能很好的学习后续模板,继承,多态等知识。

                  一、类的6个默认成员函数

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

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

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

                  【C++练级之路】【Lv.3】类和对象(中)(没掌握类的6个默认成员函数,那你根本就没学过C++!)

                  二、构造函数(constructor)

                  2.1 引入

                  先来简易实现一个日期类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(2023, 12, 5);
                  	d1.Print();
                  	return 0;
                  }
                  
                  public:
                  	// 1.无参构造函数
                  	Date()
                  	{
                  		_year = 1;
                  		_month = 1;
                  		_day = 1;
                  	}
                  	// 2.带参构造函数
                  	Date(int year, int month, int day)
                  	{
                  		_year = year;
                  		_month = month;
                  		_day = day;
                  	}
                  	void Print()
                  	{
                  		cout 
                  	Date d1;//调用无参构造函数
                  	d1.Print();
                  	Date d2(2023, 12, 5);//调用带参构造函数
                  	d2.Print();
                  	//warning
                  	Date d3();//函数声明
                  	return 0;
                  }
                  
                  public:
                  	Date(int year = 1, int month = 1, int day = 1)//使用缺省参数
                  	{
                  		_year = year;
                  		_month = month;
                  		_day = day;
                  	}
                  	void Print()
                  	{
                  		cout 
                  	Date d1;
                  	d1.Print();
                  	Date d2(2023, 12, 5);
                  	d2.Print();
                  	return 0;
                  }
                  
                  public:
                  	void Print()
                  	{
                  		cout 
                  	Date d1;//调用默认构造函数
                  	d1.Print();
                  	return 0;
                  }
                  
                  public:
                  	Date()
                  	{
                  		_year = 1;
                  		_month = 1;
                  		_day = 1;
                  	}
                  	Date(int year = 1, int month = 1, int day = 1)
                  	{
                  		_year = year;
                  		_month = month;
                  		_day = day;
                  	}
                  	void Print()
                  	{
                  		cout 
                  	Date d1;//报错,对重载函数的调用不明确
                  	d1.Print();
                  	return 0;
                  }
                  
                  public:
                  	Stack(int capacity = 4)//构造函数
                  	{
                  		_a = (int*)malloc(sizeof(int) * capacity);
                  		if (_a == nullptr)
                  		{
                  			perror("malloc fail");
                  			return;
                  		}
                  		_top = 0;
                  		_capacity = capacity;
                  	}
                  	void Push(int x)
                  	{
                  		//CheckCapacity();
                  		_a[_top++] = x;
                  	}
                  	//...
                  	~Stack()//析构函数
                  	{
                  		free(_a);
                  		_a = nullptr;
                  		_top = _capacity = 0;
                  	}
                  private:
                  	int* _a;
                  	int _top;
                  	int _capacity;
                  };
                  int main()
                  {
                  	Stack st;
                  	st.Push(1);
                  	st.Push(2);
                  	return 0;
                  }
                  
                      ST pushst;
                      ST popst;
                  } MyQueue;
                   
                  MyQueue* myQueueCreate()
                  {
                      MyQueue* obj = (MyQueue*)malloc(sizeof(MyQueue));
                      if (obj == NULL)
                      {
                          perror("malloc fail");
                          return NULL;
                      }
                   
                      STInit(&obj-pushst);
                      STInit(&obj-popst);
                   
                      return obj;
                  }
                  
                  public:
                  	void Push()
                  	{}
                  	//...
                  private:
                  	Stack _pushst;
                  	Stack _popst;
                  };
                  
                  public:
                  	Date(int year = 1, int month = 1, int day = 1)
                  	{
                  		_year = year;
                  		_month = month;
                  		_day = day;
                  	}
                  	Date(const Date& d)//拷贝构造函数
                  	{
                  		_year = d._year;
                  		_month = d._month;
                  		_day = d._day;
                  	}
                  	void Print()
                  	{
                  		cout 
                  	Date d1;
                  	d1.Print();
                  	Date d2(2023, 12, 5);
                  	d2.Print();
                  	Date d3(d2);//拷贝构造
                  	d3.Print();
                  	return 0;
                  }
                  }
                  //传引用传参
                  void Func2(Date& d)
                  {}
                  int main()
                  {
                  	Date d;
                  	Func1(d);//传值调用
                  	Func2(d);//传引用调用
                  	return 0;
                  }
                  
                  	_year = d._year;
                  	_month = d._month;
                  	_day = d._day;
                  }
                  
                  public:
                  	Stack(int capacity = 10)//构造函数
                  	{
                  		_a = (int*)malloc(sizeof(int) * capacity);
                  		if (_a == nullptr)
                  		{
                  			perror("malloc fail");
                  			return;
                  		}
                  		_top = 0;
                  		_capacity = capacity;
                  	}
                  	void Push(int x)
                  	{
                  		//CheckCapacity();
                  		_a[_top++] = x;
                  	}
                  	//...
                  	~Stack()//析构函数
                  	{
                  		free(_a);
                  		_a = nullptr;
                  		_top = _capacity = 0;
                  	}
                  private:
                  	int* _a;
                  	int _top;
                  	int _capacity;
                  };
                  int main()
                  {
                  	Stack s1;
                  	s1.Push(1);
                  	s1.Push(2);
                  	s1.Push(3);
                  	s1.Push(4);
                  	Stack s2(s1);
                  	return 0;
                  }
                  
                  public:
                  	Stack(int capacity = 4)
                  	{
                  		_a = (DataType*)malloc(capacity * sizeof(DataType));
                  		if (_a == nullptr)
                  		{
                  			perror("malloc fail");
                  			exit(-1);
                  		}
                  		_top = 0;
                  		_capacity = capacity;
                  	}
                  	Stack(const Stack& st)//深拷贝
                  	{
                  		_a = (DataType*)malloc(st._capacity * sizeof(DataType));
                  		if (_a == nullptr)
                  		{
                  			perror("malloc fail");
                  			exit(-1);
                  		}
                  		_top = st._top;
                  		_capacity = st._capacity;
                  	}
                  	~Stack()
                  	{
                  		free(_a);
                  		_a = nullptr;
                  		_top = _capacity = 0;
                  	}
                  private:
                  	DataType* _a;
                  	int _top;
                  	int _capacity;
                  };
                  
                  public:
                  	Date(int year = 1, int month = 1, int day = 1)
                  	{
                  		_year = year;
                  		_month = month;
                  		_day = day;
                  	}
                  //private:
                  	int _year;
                  	int _month;
                  	int _day;
                  };
                  bool operator==(const Date& d1, const Date& d2)
                  {
                  	return d1._year == d2._year
                  		&& d1._month == d2._month
                  		&& d1._day == d2._day;
                  }
                  int main()
                  {
                  	Date d1;
                  	Date d2(d1);
                  	
                  	operator==(d1, d2);//一般不这样写
                  	d1 == d2;//等价于上述函数调用
                  	cout 
                  public:
                  	Date(int year = 1, int month = 1, int day = 1)
                  	{
                  		_year = year;
                  		_month = month;
                  		_day = day;
                  	}
                      // bool operator==(Date* this, const Date& d2)
                      // 这里需要注意的是,左操作数是this,指向调用函数的对象
                  	bool operator==(const Date& d)
                  	{
                  		return _year == d._year
                  			&& _month == d._month
                  			&& _day == d._day;
                  	}
                  private:
                  	int _year;
                  	int _month;
                  	int _day;
                  };
                  int main()
                  {
                  	Date d1;
                  	Date d2(d1);
                  	
                  	d1.operator==(d2);//一般不这样写
                  	d1 == d2;//等价于上述写法
                  	return 0;
                  }
                  
                  public:
                  	Date(int year = 1, int month = 1, int day = 1)
                  	{
                  		_year = year;
                  		_month = month;
                  		_day = day;
                  	}
                  	void operator=(const Date& d)
                  	{
                  		_year = d._year;
                  		_month = d._month;
                  		_day = d._day;
                  	}
                  private:
                  	int _year;
                  	int _month;
                  	int _day;
                  };
                  int main()
                  {
                  	Date d1(2022, 10, 24);
                  	Date d2(2023, 5, 3);
                  	d1 = d2;
                  	return 0;
                  }
                  
                  	if (this != &d)
                  	{
                  		_year = d._year;
                  		_month = d._month;
                  		_day = d._day;
                  	}
                  	return *this;
                  }
                  
                  	friend ostream& operator
                  		out 
                  		in  d._year  d._month  d._day;
                  		return in;
                  	}
                  public:
                  	Date(int year = 1, int month = 1, int day = 1);
                  	void Print();
                  	bool operator==(const Date& d);
                  	bool operator!=(const Date& d);
                  	bool operator
                  	int a[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
                  	if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))
                  	{
                  		return 29;
                  	}
                  	return a[month];
                  }
                  Date::Date(int year, int month, int day)
                  {
                  	if((month  0 && month 
                  		_year = year;
                  		_month = month;
                  		_day = day;
                  	}
                  	else
                  	{
                  		cout
                  	cout 
                  	return _year == d._year
                  		&& _month == d._month
                  		&& _day == d._day;
                  }
                  bool Date::operator!=(const Date& d)
                  {
                  	return !(*this == d);
                  }
                  bool Date::operator
                  	return _year 
VPS购买请点击我

免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

目录[+]