大家好,C++新手的问题
我刚学C++没多久,刚了解完类与对象,在做一个练习时,是关于输出时间的练习,下面是我的代码,但通过不了编译:#include<iostream>
using std::cout;
using std::cin;
using std::endl;
#include<string>
using std::string;
using std::getline;
class Date
{
public:
Date( int monthdate, int daydate, int yeardate ) //定义构造函数
{
setMonth( monthdate );
setDay( daydate );
setYear( yeardate );
}
void setMonth( int monthdate ) //month设置函数
{
if ( monthdate <= 12 ) //将 形参赋值给实参,if语句,判断作用
month = monthdate;
if ( monthdate >12 )
{
monthdate = 1;
month = monthdate;
cout << "Month \"" << monthdate << "exceeds maximun number(12)\n"
<< endl;
}
}
int getMonth( ) //month获取函数
{
return month; //返回数据成员month的值
}
void setDay( int daydate ) //day设置函数
{
day = daydate;
}
int getDay() //定义day获取函数
{
return day;
}
void setYear ( int yeardate ) //定义yaer设置函数
{
year = yeardate;
}
int getYear()
{
return year;
}
void displayMessage()
{
cout << "Your FL year is:" << getMonth() << "/" << getDay() << "/" << getYear()
<< "~~!" << endl;
} //后面由主函数直接调用,显示 “月/日/年”。
private:
int month, day, year; //三个数据成员
}; //完成类定义
int main()
{
int monthOfFl;
int dayOfFl;
int yearOfFl;
Date myDate; //创立对象myDate,编译器显示这行有问题,不明白?
cout << "initial date is: " << myDate. getMonth()<< "/"
<< myDate. getDay() << "/" << myDate. getYear <<endl;
cout << "enter the month!" << endl;
getline( cin, monthOfFl );
myDate.setMonth( monthOfFl );
cout << "enter the day!" << endl;
getline( cin, dayOfFl );
myDate.setDay( monthOfFl );
cout << "enter the year!" << endl;
getline( cin, yearOfFl );
myDate.setYear( yearOfFl );
cout << endl;
myDate.displayMessage();
system("pause");
return 0;
}
请大家指教,我用的是DEV