C++ Bjarne Stroustrup PPP-第9章钻取4-库特枚举错误
我正在努力解决比亚恩·斯特劳斯特鲁普斯《原则与实践》第二版的第4次练习。出于某种原因,它不会让我忘记我之前初始化的月份和年份成员。我知道使用枚举数还有其他选择,但我想知道如何使用它。有人能帮我吗?干杯C++ Bjarne Stroustrup PPP-第9章钻取4-库特枚举错误,c++,enums,C++,Enums,我正在努力解决比亚恩·斯特劳斯特鲁普斯《原则与实践》第二版的第4次练习。出于某种原因,它不会让我忘记我之前初始化的月份和年份成员。我知道使用枚举数还有其他选择,但我想知道如何使用它。有人能帮我吗?干杯 // Philipp Siedler // Bjarne Stroustrup's PPP // Chapter 9 Drill 4 #include "std_lib_facilities.h" class Year { static const int min = 1800;
// Philipp Siedler
// Bjarne Stroustrup's PPP
// Chapter 9 Drill 4
#include "std_lib_facilities.h"
class Year {
static const int min = 1800;
static const int max = 2200;
private:
int y;
public:
class Invalid {};
Year(int x) : y{ x } { if (x < min || max <= x) throw Invalid{}; };
int year() { return y; };
};
enum class Month {
jan = 1, feb, march, apr, may, jun, jul, aug, sep, oct, nov, dec
};
class Date {
private:
Year y;
Month m;
int d;
public:
Date(Year y, Month m, int d) : y(y), m(m), d(d) {};
Year year() { return Year{ y }; };
Month month() { return Month{ m }; };
int day() { return d; };
void add_day(int n);
};
void Date::add_day(int n) {
d += n;
}
int main()
try
{
Date today{ Year{ 1978 }, Month::jun, 25 };
Date tomorrow = today;
tomorrow.add_day(1);
cout << "Year: " << today.year() << " Month: " << today.month() << " Day: " << today.day() << "\n";
cout << "Year: " << tomorrow.year() << " Month: " << tomorrow.month() << " Day: " << tomorrow.day() << "\n";
keep_window_open();
}
catch (runtime_error e) {
cout << e.what() << '\n';
keep_window_open();
}
catch (...) {
cout << "Exiting" << '\n';
keep_window_open();
}
//Philipp Siedler
//比亚恩·斯特劳斯图普的PPP
//第9章练习4
#包括“std_lib_facilities.h”
学年{
静态常数int最小值=1800;
静态常数int max=2200;
私人:
int-y;
公众:
类无效{};
Year(int x):y{x}{if(x
// Philipp Siedler
// Bjarne Stroustrup's PPP
// Chapter 9 Drill 4
#include "std_lib_facilities.h"
class Date {
public:
enum Month {
jan = 1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec
};
Date(int y, Month m, int d);
void add_day(int n);
int year() const { return y; }
Month month() const { return m; }
int day() const { return d; }
private:
int y;
Month m;
int d;
};
Date::Date(int y0, Date::Month m0, int d0) {
d = d0;
m = m0;
y = y0;
}
void Date::add_day(int n) {
d += n;
}
static ostream & operator<< (ostream & os, const Date::Month & m) {
switch (m) {
case Date::jan: os << "January"; break;
case Date::feb: os << "February"; break;
case Date::mar: os << "March"; break;
case Date::apr: os << "April"; break;
case Date::may: os << "May"; break;
case Date::jun: os << "June"; break;
case Date::jul: os << "July"; break;
case Date::aug: os << "August"; break;
case Date::sep: os << "September"; break;
case Date::oct: os << "October"; break;
case Date::nov: os << "November"; break;
case Date::dec: os << "December"; break;
}
return os;
}
ostream & operator<< (ostream & os, const Date & dd) {
os << "Year: " << dd.year() << " Month: " << dd.month() << " Day: " << dd.day() << endl;
return os;
}
int main()
try
{
Date today{ 1978, Date::jun, 25 };
Date tomorrow = today;
tomorrow.add_day(1);
cout << today;
cout << tomorrow;
keep_window_open();
}
catch (runtime_error e) {
cout << e.what() << '\n';
keep_window_open();
}
catch (...) {
cout << "Exiting" << '\n';
keep_window_open();
}
//Philipp Siedler
//比亚恩·斯特劳斯图普的PPP
//第9章练习4
#包括“std_lib_facilities.h”
上课日期{
公众:
枚举月{
一月=1,二月,三月,四月,五月,六月,七月,八月,九月,十月,十一月,十二月
};
日期(整数y、月份m、整数d);
无效添加日(整数n);
int year()常量{return y;}
Month Month()常量{return m;}
int day()常量{return d;}
私人:
int-y;
月m;
int d;
};
日期::日期(整数y0,日期::月份m0,整数d0){
d=d0;
m=m0;
y=y0;
}
作废日期::添加日(整数n){
d+=n;
}
静态的Oracle和操作程序No.<代码> STD::FLASH ING?我对代码和C++是新的,没有触及STD的主题::Flash。Philipp Siedler。你必须重载操作员的错误:<代码> max @ JavaSun是的,谢谢。但是这不是我要问的问题。