Instantiation and constructors in c++

Pages: 12
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);
}


and finally for my show class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  class Show {
  
public:
   Show(); //default constructor
   Show(const Date& date, const Time& time, string title, string genre); 

   Date getDate();
   Time getTime();
   string getName();
   string getGenre();

   void setDate();
   void setTime();
   void setName(string m_Name);
   void setGenre(string m_Genre);

private:
   Date date
   Time time;
   
   string title;
   string genre;


Show cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Show::Show()
{
    date = Date();
    time = Time();
    title = "Title";
    genre = "Genre";
}

Show::Show(const Date& date, const Time& time, string title, string genre)
{
     setDate(date);
     setTime(time);
     setTitle(title);
     setGenre(genre);
}
Last edited on
I'm not really clear what you're having a problem with.

I want to create instances of Date and Time in my Show class and I'm not sure how to do that

You appear to be doing that just fine. show.h lines 18-19 create date and time objects in your Show class.

A couple of problems I do see:
show.h, lines 12-13, your setters do not take an argument.

show.h line 18: Missing ;

If these are not your problems, please post the exact text of the messages you're getting.

show.cpp lines 3-4: These lines are not really necessary. Your date and time members will be initialized with their default constructors.


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?
I need help figuring out what to put in my getDate/getTime and setDate/setTime

getDate() and getTime() should be returning Show's date and time members respectively.

1
2
3
4
5
6
7
Date Show::getDate () const
{  return date; 
}

Time Show::getTime () const
{  return time;
}


setDate and setTime require a date and time as arguments, respectively.

1
2
3
4
5
6
7
void Show::setDate (const Date & dt)
{  date = dt;
}

void Show::setTime (const Time & tm)
{  time = tm;
}


Should my void setDate() and void setTime() on lines 12 and 13 have some parameters?

Yes.

What are my Date getDate() and Time getTime() in line 7 and 8 getting?

show.h Lines 7 and 8 are returning Show's date and time members respectively (by value).

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:

 
03/10/2016 09:10:10, Name, Genre


I am reading in the line like so
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Show show; //show object

getline(inFile, csvLine, ' ');
show.setDate();
        
getline(inFile, csvLine, ',');
show.setTime();

getline(inFile, csvLine, ',');
show.setName(csvLine);

getline(inFile, csvLine);
show.setGenre(csvLine);

ShowVector.push_back(csvLine)


Not sure how to set the date and time. What goes in the parameters?
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;
  }
 

Then, in place of line 3:
 
    infile >> date;



The same applies to your time class.
Last edited on
infile >> date;

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.
Last edited on
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.

You still haven't shown the declaration of csvLine, but as I said before, I'm assuming it is a std::string.

Assuming csvLine is a std::string, you're creating a vector of strings. Nothing more.

What you want to push is a Show object.
1
2
3
4
5
  vector<Show>  shows;
...
  Show show;
  //  Initialize show somehow
  shows.push_back (show);

Yes, csvLine is a string. So:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Show show; //show object

while (!inFile.eof()) {
inFile >> date;
        
inFile >> time;

getline(inFile, csvLine, ',');
show.setName(csvLine);

getline(inFile, csvLine);
show.setGenre(csvLine);

ShowVector.push_back(show)
}
Also, I notice the setters for the Date and Time classes arent used at all. Doesn't that just make them redundant?
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

1
2
3
4
5
6
7
ostream & operator >> (ostream & os, Date & date)
{
    
    os << date.day << "/" << date.month << "/" << date.year;
    return os;
}


1
2
3
4
5
6
ostream & operator >> (ostream & os, Time & time)
{
    
    os << time.hour << ":" << time.minute << ":"  << time.second <<;
    return os;
}
Last edited on
closed account (E0p9LyTq)
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
Oh, sorry. Typo.

And I already have those in my header.

friend ostream & operator << (ostream & os, Time & time);

friend ostream & operator << (ostream & os, Date & date);

Just wondering how to actually use it. Something like cout << date; wouldn't work, would it?
Something like cout << date; wouldn't work, would it?

That's exactly how they work.

edit: Just a note on your prototypes. Date and Time should be const.
Last edited on
closed account (E0p9LyTq)
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.
Last edited on
You're going to have to show more context. Please show the declaration and implementation of your overloaded << operators. Also where you do the cout.

I guess you didn't see the update before you replied. I fixed that issue. It was a small typo.

I'm trying to see what's in the vector by using show[i].getDate() and it's simply giving me
0/0/0
Last edited on
UPDATE:

I figured out by using

1
2
3
4
5
inFile >> date;
show.setDate(date);
        
inFile >> time;
show.setTime(time);


I was able to then use showVector[i].getDate() to print out the date
Last edited on
Pages: 12