构造函数与析构函数

#include<iostream>
#include<cstring>
using namespace std;
//定义类  Timedate 
class Timedate{
	private://数据成员
		int year,month,day;
		int hour,minute,second;
	public://函数成员 
	void setdate(int y,int m,int d,int h,int min,int s){//设置日期时间 :类内定义函数 
		year=y;month=m;day=d;
		hour=h;minute=min;second=s;
	}
	void showdate();
	Timedate(int y=0,int m=0,int d=0){//定义有三个默认值的构造函数 
		year=y;month=m;day=d;
		cout<<"********gouzao333********"<<endl;
		cout<<"year:"<<year<<'\t'<<"month:"<<month<<'\t'<<"day:"<<day<<'\t'<<endl;
		}
		Timedate(int y,int m,int d,int h,int min,int s){//定义6个参数的构造函数 
		year=y;month=m;day=d;
		hour=h;minute=min;second=s;
		cout<<"********gouzao666********"<<endl;
		cout<<"year:"<<year<<'\t'<<"month:"<<month<<'\t'<<"day:"<<day<<'\t'<<endl;
		cout<<"hour:"<<hour<<'\t'<<"minute:"<<minute<<'\t'<<"second:"<<second<<'\t'<<endl;
		}
	~Timedate(){//定义析构函数 
		cout<<"********xigou********"<<endl;
		cout<<month<<endl;
	}		
};
void Timedate::showdate(){//输出日期时间 :类外定义函数 
		cout<<"year:"<<year<<'\t'<<"month:"<<month<<'\t'<<"day:"<<day<<'\t'<<endl;
		cout<<"hour:"<<hour<<'\t'<<"minute:"<<minute<<'\t'<<"second:"<<second<<'\t'<<endl;}
int main(){
	Timedate t; //新对象建立,调用有默认值的构造函数 
	t.setdate(2023,4,15,15,48,35);//调用setdate函数 
	t.showdate();//调用showdate函数 
	Timedate t2(2023,3,6,4,6,9);//调用有6个参数的构造函数 
	Timedate t1(2023,9,16);//调用有默认值的构造函数 
	Timedate t3(11,4); //调用有默认值的构造函数
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Camellia.332

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值