this指针应用

作者在 2014-05-07 19:08:25 发布以下内容
#include <iostream>
#include <string>
using namespace std;
class Date
{
	int mo,da,yr;
	char *month;
public:
	Date(int m=0,int d=0,int y=0);
	~Date();
	Date& operator=(const Date&);
	void display() const;
};
Date::Date(int m,int d,int y)
{
	static char *mos[]=
	{
		"Januanry","February","March","April","May","June","July",
			"August","September","October","November","December"
	};
	mo=m,da=d,yr=y;
	if(m!=0)
	{
		month=new char[strlen(mos[m-1])+1];
		strcpy(month,mos[m-1]);
	}
	else month=0;
}
Date::~Date()
{
	delete [] month;
}
void Date::display() const
{
	if(month!=0)
		cout<<month<<''<<da<<","<<yr<<endl;
}
Date& Date::operator=(const Date& dt)
{
	if(this!=&dt)
	{
		mo=dt.mo;
		da=dt.da;
		yr=dt.yr;
		delete []month;
		if(dt.month !=0)
		{
			month=new char [strlen(dt.month)+1];
			strcpy(momth,dt.month);
		}
		else month=0;
	}
	return *this;
}
void main()
{
	Date birthday(4,19,1979);
	Date oldday,newday;
	oldday=mewday=birthday;
	birthday.display();
	oldday.display();
	newday.display();
}
//最近总是找不出error在哪
c++ | 阅读 1144 次
文章评论,共0条
游客请输入验证码
浏览36953次
最新评论