【C++心愿便利店】No.5---构造函数和析构函数

2024-02-26 1096阅读

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

文章目录

  • 前言
  • 一、类的6个默认成员函数
  • 二、构造函数
  • 三、析构函数

    前言

    【C++心愿便利店】No.5---构造函数和析构函数

    👧个人主页:@小沈YO.

    😚小编介绍:欢迎来到我的乱七八糟小星球🌝

    📋专栏:C++ 心愿便利店

    🔑本章内容:类和对象

    记得 评论📝 +点赞👍 +收藏😽 +关注💞哦~


    提示:以下是本篇文章正文内容,下面案例可供参考

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

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

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

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

    【C++心愿便利店】No.5---构造函数和析构函数

    二、构造函数

    如下Date类,没有初始化打印出来就会是随机值,同时对于栈没有初始化,就会报错

    那如果想能否在创建对象的同时,就将信息设置进去呢。因此,就有了构造函数。以Date类为例:

    #include
    using namespace std;
    class Date
    {
    public:
    	void Init(int year, int month, int day)
    	{
    		_year = year;
    		_month = month;
    		_day = day;
    	}
    	void Print()
    	{
    		cout 
    	Date d1;
    	d1.Print();//没有调用Init初始化函数
    	Date d2;
    	d2.Init(2022, 7, 6);//调用Init初始化函数
    	d2.Print();
    	return 0;
    }
    
    public:
    	构造函数
    	Date()
    	函数名与类名相同
    	且无返回值
    	{
    		cout 
    		_year = year;
    		_month = month;
    		_day = day;
    	}
    	void Print()
    	{
    		cout 
    	Date d1;
    	d1.Print();
    	return 0;
    }
    
    public:
    	1. 无参的构造函数
    	Date()
    	{}
    	也可以写成下面这种
    	Date()//函数名与类名相同且无返回值
    	{
    		cout 
    		_year = year;
    		_month = month;
    		_day = day;
    	}
    	3. 全缺省的构造函数
    	Date(int year=1, int month=1, int day=1)
    	无参和全缺省的不能同时存在会存在调用歧义
    	{
    		_year = year;
    		_month = month;
    		_day = day;
    	}
    	void Init(int year, int month, int day)
    	{
    		_year = year;
    		_month = month;
    		_day = day;
    	}
    	void Print()
    	{
    		cout 
    	Date d1;调用无参的构造函数
    	//Date func();
    	这也可以是一个函数声明所以为了区分不能加()
    	d1.Print();
    	Date d2(2023,8,28);调用带参的构造函数
    	d2.Print();
    	对于全缺省的构造函数使用更灵活可以传一个参数,两个等
    	Date d3(2023);
    	d3.Print();
    	Date d4(2023, 8);
    	d4.Print();
    	return 0;
    }
    
      public:
     /*
     // 如果用户显式定义了构造函数,编译器将不再生成
     Date(int year, int month, int day)(有参的构造函数)
     {
     _year = year;
     _month = month;
     _day = day;
     }
     */
     
     void Print()
     {
     cout 
     Date d1;
     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 = 2023;
    	int _month = 9;
    	int _day = 5;
    	// 自定义类型
    	Time _t;
    };
    int main()
    {
    	Date d;
    	return 0;
    }
    
    public:
    	Date()
    	{
    		这里_year没有给值而_month _day给了值打印出来是2023/2/1所以声明那给的是缺省值
    		
    		_month = 2;
    		_day = 1;
    	}
    	void Print()
    	{
    		cout 
    	Date d1;
    	d1.Print();
    	return 0;
    }
    
    public:
    	Date(int year = 1, int month = 1, int day = 1)
    	{
    		_year = year;
    		_month = month;
    		_day = day;
    	}
    	~Date()
    	{
    		cout 
    public:
    	Stack(size_t n=4)
    	{
    		cout 
    			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 Init()
    	{
    		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 
    				cout 
    		cout 
    		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()
    {
    	Date d1;
    	Stack st1;
    	Stack st2;//后定义的先析构
    	return 0;
    }
    
    public:
    	~Time()
    	{
    		cout 
    private:
    	// 基本类型(内置类型)
    	int _year = 1970;
    	int _month = 1;
    	int _day = 1;
    	// 自定义类型
    	Time _t;
    };
    int main()
    {
    	Date d;
    	return 0;
    }
    
VPS购买请点击我

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

目录[+]