如何对成员函数中的输出进行重载(参数类型不同)

来源:百度知道 编辑:UC知道 时间:2024/05/12 07:25:17
//设计一个日期类,以两种不同格式(1/1/2007和May 1,2007)输出
#include<iostream.h>
#include<stdio.h>

class Date
{
private:
char *month;
int month1;
int day;
long year;
public:
Date(int month1=1,int day=1,long year=2007)
{
this->month1=month1;
this->day=day;
this->year=year;
}
Date(char *month1,int day=1,long year=2007)
{
month=month1;
this->day=day;
this->year=year;
}

~Date()
{}

void show(int,int,long)
{
cout<<month1<<"/"<<day<<"/"<<year<<endl;
}
void show(char,int,long)
{
cout<<month<<" "<<day<<","<<year<<endl;
}

};

void main()
{
Date z1(4,27,2007),z2("April",27,2007);
z1.show();
z2

写错了吧,是show (char *,int,long)

void show(char,int,long)
{
cout<<month<<" "<<day<<","<<year<<endl;
}
改为:
void show(string,int,long)
{
cout<<month<<" "<<day<<","<<year<<endl;
}
还有,把头文件换成下面的.
#include<iostream>
#include<string>
using namespace std;
因为你的第二个show的第一个参数是字符串.而char 是字符.
你用类的成员函数的时候也要带参数..
int main()
{
Date z1(4,27,2007),z2("April",27,2007);
z1.show(4,27,2007);
z2.show("April",27,200);

return 0;
}