C++每日一题(3)(关于类以及构造函数的运用)

定义一个名为Month的类,它是用于表示月份的一个抽象数据类型。这个类要用int类型的一个成员变量来表示一个月(1表示1月,2表示2月,依此类推)。请包括以下所有成员函数:一个构造函数使用英文月份名称的前3个字母来设置月份名称,这3个字母要通过3个参数来接收:一个构造函数只接收一个int类型的参数,并用这个整数来设置月份(1代表1月,2代表2月,以此类推):一个默认构造函数:一个输入函数,它将月份作为整数来读取:一个输入函数,它将月份名称的前3个字母来读取:一个输出函数,它将月份作为月份名称的前3个字母来输出:以及一个成员函数,它将下一个月份作为Month类型的一个值来返回。

#include<iostream>
using namespace std;

class Month
{
public:
	Month(int n)
	{
		if((n>=12)&(n<=0))
			exit(1);
		else
			month=n;

	}
	Month()
	{}
	int month;
	char english_month[3];
	void get_english_month(char first,char second,char third);
	void input_number_month();
	void input_english_month();
	void output_english_month();


};
int main()
{
	Month month1(3),month2,month3;
	month1.output_english_month();
	month2.input_number_month();
	month2.output_english_month();
	month3.input_english_month();
	//cout<<month3.english_month[0]<<month3.english_month[1]<<month3.english_month[2]<<endl;
	month3.get_english_month(month3.english_month[0],month3.english_month[1],month3.english_month[2]);
	month3.output_english_month();
	return 0;
}


void Month::get_english_month(char first,char second,char third)
{
	//cout<<"haha"<<endl;
	if(((first=='j')|(first=='J'))&((second=='a')|(second=='A'))&((third=='n')|(third=='N')))
		month=1;
	else if(((first=='f')|(first=='F'))&((second=='e')|(second=='E'))&((third=='b')|(third=='B')))
		month=2;
	else if(((first=='m')|(first=='M'))&((second=='a')|(second=='A'))&((third=='r')|(third=='R')))
		month=3;
	else if(((first=='a')|(first=='A'))&((second=='p')|(second=='P'))&((third=='r')|(third=='R')))
		month=4;
	else if(((first=='m')|(first=='M'))&((second=='a')|(second=='A'))&((third=='y')|(third=='Y')))
		month=5;
	else if(((first=='j')|(first=='J'))&((second=='u')|(second=='U'))&((third=='n')|(third=='N')))
		month=6;
	else if(((first=='j')|(first=='J'))&((second=='u')|(second=='U'))&((third=='l')|(third=='L')))
		month=7;
	else if(((first=='a')|(first=='A'))&((second=='u')|(second=='U'))&((third=='g')|(third=='G')))
		month=8;
	else if(((first=='s')|(first=='S'))&((second=='e')|(second=='E'))&((third=='p')|(third=='P')))
		month=9;
	else if(((first=='o')|(first=='O'))&((second=='c')|(second=='C'))&((third=='t')|(third=='T')))
		month=10;
	else if(((first=='n')|(first=='N'))&((second=='o')|(second=='O'))&((third=='v')|(third=='V')))
		month=11;
	else if(((first=='d')|(first=='D'))&((second=='e')|(second=='E'))&((third=='c')|(third=='C')))
		month=12;
	else
	{
		cout<<"输入有误:"<<endl;
		exit(1);
	}
}
void Month::input_number_month()
{
	cout<<"输入一个月份的值:"<<endl;
	cin>>month;
}
void Month::input_english_month()
{
	cout<<"输入一个月份的缩写:"<<endl;
	cin>>english_month[0]>>english_month[1]>>english_month[2];
}
void Month::output_english_month()
{
	if(month==1)
		cout<<"Jan"<<endl;
	else if(month==2)
		cout<<"Feb"<<endl;
	else if(month==3)
		cout<<"Mar"<<endl;
	else if(month==4)
		cout<<"Apr"<<endl;
	else if(month==5)
		cout<<"May"<<endl;
	else if(month==6)
		cout<<"Jun"<<endl;
	else if(month==7)
		cout<<"Jul"<<endl;
	else if(month==8)
		cout<<"Aug"<<endl;
	else if(month==9)
		cout<<"Sep"<<endl;
	else if(month==10)
		cout<<"Oct"<<endl;
	else if(month==11)
		cout<<"Nov"<<endl;
	else if(month==12)
		cout<<"Dec"<<endl;
}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值