我无法弄清楚如何显示一个月而不必在我的开关中显示“1月” - 如果我尝试只是输入它我得到1号
我是否需要为dateDate创建内存;私人会员? 比我的拷贝构造函数中的副本?
抱歉格式化。我不得不移动东西,所以它会在这个页面上正确显示
这就是我所拥有的。我是否需要为私人会员日期创建内存?
class date
{
public:
typedef enum {INVALID, JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE,
JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER}
Month;
date(Month month, int day, int year);
date(const date& date); // copy constructor
date(void); // default constructor
~date(void);
friend ostream& operator<<(ostream& out, const date& d);
private:
Month month;
int day;
int year;
};
也
class stock
{
public:
stock(char const * const symbol, char const * const name, int sharePrice, date priceDate);
// sharePrice is given as a number of CENTS
stock(const stock& s); // copy constructor
stock(void); // default constructor
char const * const getSymbol(void) const;
stock& operator=(const stock& s);
stock& operator=(stock const * const s);
~stock(void);
// display column headers
static void displayHeaders(ostream& out); // display the headers when this instance is printed
// prints share price as DOLLARS
// (e.g. 2483 would print out as 24.83 and 200 would print out as 2.00)
friend ostream& operator<<(ostream& out, const stock& s);
friend class hashmap;
private:
int sharePrice;
char* name;
char* symbol;
date priceDate;
int removed;
};
现在的功能。
date::date(Month month, int day, int year)
{
this->day = day;
this->month = month;
this->year = year;
}
/**
* date: copy constructor
* in: date
* out: out
* return: none
**/
date::date(const date& date):day(date.day),month(date.month),year(date.year)
{
}
date::date()
{
day = 0;
year = 0;
month = INVALID;
}
date::~date(void)
{
}
ostream& operator<<(ostream& out, const date& d)
{
switch(d.month)
{
case 1: out << "January " << d.day <<", " << d.year;
break;
case 2: out << "Febuary "<< d.day <<", " << d.year;
break;
case 3: out << "March "<< d.day <<", " << d.year;
break;
case 4: out << "April "<< d.day <<", " << d.year;
break;
case 5: out << "May "<< d.day <<", " << d.year;
break;
case 6: out << "June "<< d.day <<", " << d.year;
break;
case 7: out << "July " << d.day <<", " << d.year;
break;
case 8: out << "august "<< d.day <<", " << d.year;
break;
case 9: out << "September "<< d.day <<", " << d.year;
break;
case 10: out << "October "<< d.day <<", " << d.year;
break;
case 11: out << "November "<< d.day <<", " << d.year;
break;
case 12: out << "december "<< d.day <<", " << d.year;
break;
}
return out;
}
答案 0 :(得分:4)
这样的事情:
const char* const MonthNames[] = { "", "January", "February", "March", "April", "May", "June" ... };
out << MonthNames[d.month] << ' ' << d.day << ", " << d.year;
答案 1 :(得分:3)
看看strftime。
答案 2 :(得分:1)
使用数组和枚举类型。
class date {
public:
enum Month {
JAN, FEB, MAR, APR, MAY, JUN,
JUL, AUG, SEP, OCT, NOV, DEC
};
date(int y, Month m, int d) : _y(y), _m(m), _d(d) {}
int year() const { return _y; }
Month month() const { return _m; }
int day() const { return _d; }
const std::string& month_name(Month m) const {
return months[m];
}
private:
int _y;
Month _m;
int _d;
static std::string months[];
friend std::ostream& operator <<(std::ostream &o, const date &d);
}
不要忘记为date::months
和流插入运算符定义存储:
std::string date::months[] = {
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
};
std::ostream& operator <<(std::ostream &o, const date &d)
{
o << d.day() << ' '
<< d.month_name(d.month()) << ' '
<< d.year();
return o;
}
样本用法:
int main()
{
date d(2009, date::NOV, 21);
std::cout << d << std::endl;
return 0;
}
答案 3 :(得分:0)
枚举只是数字。如果您希望枚举值与字符串对应,则需要创建某些排序的映射,例如:
const char * asString(Month month)
{
static const char * cMonths[] = { "January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December" };
return cMonths[static_cast<int>(month) - 1];
}
我是否需要为此创建内存 私人会员约会?
date priceDate;
此代码没问题。运行时将为您分配堆栈上所需的内存。