I'm trying to run this program all together without it being separated into header files and cpp files. Run the program, it just says there's syntax errors. Another problem I'm getting is when the program actually runs, the second event's data arguments don't do anything at all...
#include <iostream>
#include <string>
using namespace std;
class Date
{
// this is a simple Date class to demonstrate the use
// of composition inheritance. this class will be
// part of the definition of the Event class
public:
void setDate(int = 1, int = 1, int = 2000); //mutator function
void getDate(int &n, int &d, int &y);//accessor function
void printDate(); // display function
Date (int month, int day, int year); //regular constructors
Date();//default constructor
private:
//three private data members
int month;
int day;
int year;
};
class Event
{ // Class that "has-a" Time and "has-a" Date object as part
// of it's definition to demonstrate composition inheritance
public:
void setEventData(string EventName, int hour, int minute, int month, int day, int year);
void printEventData();
Event(string eventName = "Generic Event", int hour = 0, int minute = 0,
int month = 1, int day = 1, int year = 2000); //member initialization list
private:
string eventName;
Time eventTime; // a Time object is now a private data member of the class Event
Date eventDate; // a Date object is now a private data member of the class Event
};
class Time
{
// this is a simple time class to demonstrate the use
// of composition inheritance. this class will be
// part of the definition of the Event class
public:
void setTime(int hr, int min);// mutator function
void getTime(int &hr, int &min); //accessor function
void printTime(); // display function
Time(int hr, int min); // regular constructor
Time(); //default constructor
private:
//two private data members
int hour;
int minute;
};
//regular constructor implementation
Date::Date(int m, int d, int yr) {month = m; day =d; year = yr;}
//default constructor implementation
Date::Date() { month = day = 1; year = 2000;}
void Date::setDate( int month, int day, int year )
{
month = 1;
day = 1;
year = 1;
}
void Date::getDate(int &m, int &d, int &y)
{
m = month;
d = day;
y = year;
}
void Date::printDate()
{
cout << month << "/" << day << "/" << year;
}
Time::Time( int hr, int min) { hour = hr; minute = min; }
Time::Time() { hour = minute = 0; }
void Time::setTime( int hr, int min )
{
hour = hr;
minute = min;
}
void Time::getTime( int &hr, int &min )
{
hr = hour;
min = minute;
}
void Time::printTime()
{
if ( hour < 10 )
cout << "0";
cout << hour << ":";
if (minute < 10 )
cout << "0";
cout << minute;
}
Event::Event(string name, int hour, int minute, int month, int day, int year)
:eventTime(hour, minute), eventDate(month,day,year) // member initialization list
{
eventName = name;
}
void Event::setEventData(string name, int hr, int min, int mon, int day, int yr)
{
eventName = name;
eventTime.setTime(hr, min);
//we could use the Time object eventTime to directly access the public function setTime() that is declared inside the Time class
eventDate.setDate(mon, day, yr);
//we could use the Date object eventDate to directly access the public function setDate() that is declared inside the Date class
}
void Event::printEventData()
{
cout << eventName << " occurs ";
eventDate.printDate();
//we could use the Date object eventDate to directly access the public function printDate() that is declared inside the Date class
cout << " at ";
eventTime.printTime();
//we could use the Time object eventTime to directly access the public function printDate() that is declared inside the Time class
}
int main()
{
Event event("New Year's Day", 0, 1, 1, 1, 2008);
event.printEventData();
cout << endl;
//Event event2("Valentine's Day", 12, 15, 2, 14, 2010);
event.setEventData("Valentine's Day", 12, 15, 2, 14, 2010);
event.printEventData();
cout << endl;
return 0;
}