刚开始自学C++,请问为什么构造函数在类中定义过了,后面又在类外写了一遍(红色加粗部分)?
#include<iostream>using namespace std;
class Date
{
public:
Date();
Date(int y,int m,int d);
void showDate();
private:
int year;
int month;
int day;
};
Date::Date()
{
year=2000;
month=4;
day=28;
}
Date::Date(int y,int m,int d)
{
year=y;
month=m;
day=d;
}
inline void Date::showDate()
{
cout<<year<<"."<<month<<"."<<day<<endl;
}
int main()
{
Date date1;
cout<<"Date1 output:"<<endl;
date1.showDate();
Date date2(2002,11,14);
cout<<"Date2 output:"<<endl;
date2.showDate();
return 0;
}
不知道我到底表达清楚没,请见谅求解答