tags:

views:

164

answers:

4

I am having trouble trying to figure out how i would display a month without having to display "January" like in my switch - if i try to just oputput it i get a number 1

Do i need to create memory for the date priceDate; private member? than copy in my copy constructor?

sorry about formatting. i had to move stuff over so it would display on this page properly

This is what i have. Do i need to create memory for the private member date?

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;
    };

also.

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;


   };

now the functions.

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;
  }
+3  A: 

Take a look at strftime.

Mark Byers
+4  A: 

Something like this:

const char* const MonthNames[] = { "", "January", "February", "March", "April", "May", "June" ... };

out << MonthNames[d.month] << ' ' << d.day << ", " << d.year;
Evän Vrooksövich
+1. But you could use "d.month - 1" and get rid of that empty string at the start of the array.
paxdiablo
..or name the first string "Invalid".
StackedCrooked
+1  A: 

Use an array and an enumerated type.

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);
}

Don't forget to define storage for date::months and a stream insertion operator:

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;
}

Sample usage:

int main()
{
  date d(2009, date::NOV, 21);

  std::cout << d << std::endl;

  return 0;
}
Greg Bacon
A: 

Enums are just numbers. If you want an enum value to correspond with a string, then you need to create a mapping of some sorts, for example:

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];
}


Do i need to create memory for the private member date?

date priceDate;

This code is ok. The runtime will allocate the required memory on the stack for you.

StackedCrooked