I'm trying to learn about constructors and instance of class etc and am having a few issues. So i have 3 class: Date, Time and Show.
Essentially what I want to do is insert "Show objects" into a vector and so I want to create instances of Date and Time in my Show class and I'm not sure how to do that. I know it's something to do with the "getDate" and "setDate" that I have in my show class which use the constructors from their respective classes. Please help!
Here is my code:
For my date class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
class Date {
public:
Date(); //default constructor
Date(int Day, int Month, int Year); //standard constructor
int getDay();
int getMonth();
int getYear();
void setDay(int m_day);
void setMonth(int m_month);
void setYear(int m_year);
private:
int day;
int month;
int year;
my Date cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Date::Date() {
day = 0;
month = 0;
year = 0000;
}
// Standard constructor
Date::Date(int day, int month, int year)
{
setDay(day);
setMonth(month);
setYear(year);
}
//setters and getters
my Time class is exactly the same as my Date class but with Hour, Minute and Second and my constructors are the same as the Date Time(int hour, int minute, int second, string AMPM); and
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Time::Time() {
hour = 0;
minute = 0;
second = 0;
}
//standard constructor
Time::Time(int hour, int minute, int second, string AMPM)
{
setHour(hour);
setMinute(minute);
setSecond(second);
setAMPM(AMPM);
}
I need help figuring out what to put in my getDate/getTime and setDate/setTime in my Show cpp because right now they are empty.
Should my void setDate() and void setTime() on lines 12 and 13 have some parameters? What are my Date getDate() and Time getTime() in line 7 and 8 getting?
Okay, thanks. Last question, if I am reading in a CSV file and taking the values and putting them in Show objects and then pushing those objects into a vector, how would it be done for the Date and Time objects? For example the line in the CSV is:
if I am reading in a CSV file and taking the values and putting them in Show objects
Line 3: You've read part of the line (up to a space) into csvLine. You haven't show the declaration of csvLine, but I'm going to assume it's a string.
You have a couple of ways to do this.
1) You can provide a Date constructor which takes a string. Your date constructor must then pick apart the date subfields. A stringstream works well for this.
1 2 3 4 5
Date::Date (const string & line)
{ stringstream ss (line);
char slash;
ss >> month >> slash >> day >> slash >> year;
}
2) The classic approach is to overload the >> operator.
1 2 3 4 5 6 7 8
// In Date's class declaration
friend istream & operator >> (istream & is, Date & date)
{ char slash;
is >> date.month >> slash >> date.day >> slash >> date.year;
return is;
}
Alright, I was able ti implement overloaded >> for both my time and date classes however, I was wondering, when I use ShowVector.push_back(csvLine) , is the date also being pushed into the vector as a show object because what i'm pushing in is csvLine.
I notice the setters for the Date and Time classes aren't used at all. Doesn't that just make them redundant?
Not necessarily. Granted, you don't need the setters right now. However, as you develop more programs, you may want to reuse your Date and Time classes in other programs. By putting your Date and Time classes in a library you can easily reuse them in other programs. You will find yourself enhancing your Date and Time classes and they will over time become quite robust.
Cool. Last question, I want to print out everything in my show vector after the ShowVector.push_back(show) to test and see what's inside the vector. How would I do that? I've put 2 ostreams in my Time and Date classes but now sure how to use them
You are overloading the extraction operator, not the insertion operator: ostream& operator<< (ostream& os, Date& date)
ostream& operator<< (ostream& os, Time& time)
In your class header files declare the << operator as friend so the functions can access your private data members.
http://en.cppreference.com/w/cpp/language/friend
If you have it chained up with std::ostream properly it will work. That is why you overload the insertion operator in your class, so you can output your class members as easily when using Plain Old Data.
Then why am I getting an invalid operands to binary expression ('ostream' (aka 'basic_ostream<char>') and 'Date') error everytime I try to do cout << date ?
Edit: Managed to fix that error. However, the question about how should I print out the entire vector still stands.