In: Computer Science
URGENT:
USING C++:
You are given a main.cpp and a Schedule.h file. You are to write a Schedule.cpp file that implements the missing pieces from Schedule.h.
// ************************************
// ************* main.cpp ************
// ************************************
#include "Schedule.h"
void main()
{
Schedule s1(10);
s1.addEntry(1,20,"Feed Cat");
s1.addEntry(2,40,"Feed Dog");
s1.addEntry(2,50,"Walk Dog");
s1.addEntry(4,0, "Fix Dinner");
s1.addEntry(5,15,"Eat Dinner");
s1.printSchedule();
Schedule s2(s1); // Note this uses the copy constructor
cout << endl << "Output from s2 " << endl;
s2.printSchedule();
}
// **********************************************
// ************* Schedule.h ******************
// **********************************************
#include <iostream>
#include <string>
using namespace std;
class Entry{
private:
int m_hour;
int m_minute;
string m_todo;
public:
Entry()
{
m_hour = -1;
m_minute = -1;
m_todo = "N/A";
}
void setData(int hour, int minute, const char *td);
void setData(Entry & e);
//This routine prints a line that would look something like:
// 12:30 Take a Walk
void printEntry();
};
class Schedule
{
private:
// Array of Entry
Entry * m_entryArr;
// Max size of the Array
int m_maxEntries;
// Number of entries filled in with setData (starts out = 0)
int m_actualEntryCount;
public:
// This Constructor will initialize 2 ints and define the m_entryArr
Schedule(int maxEntries);
// Copy Constructor – it is done for you
Schedule::Schedule(Schedule & s) {
m_actualEntryCount = s.m_actualEntryCount;
m_maxEntries = s.m_maxEntries;
m_entryArr = new Entry[m_maxEntries];
for (int i=0; i < m_actualEntryCount; i++)
{
m_entryArr[i].setData(s.m_entryArr[i]);
}
}
~Schedule();
// addEntry returns true if it succeeds, false if we are out of space
// Note that addEntry will merely call setData to fill in the next entry
// in the array and increment the m_actualEntryCount
bool addEntry(int hour, int minute, const char *todo);
// This routine prints out all entries (i.e. as specified by m_actualEntryCount)
void printSchedule();
};
First Complete the getter(that is setData Method) for Entry class because we cannot access private data members of class variables in another class as Schedule use that.
void setData(int hour, int minute, const char *td)
{
this->m_hour=hour;
this->m_minute=minute;
this->m_todo=td;
}
void setData(Entry & e)
{
this->m_hour=e.m_hour;
this->m_minute=e.m_minute;
this->m_todo=e.m_todo;
}
print the entry data memebers as show hr:min "todo"
// 12:30 Take a Walk
void printEntry()
{
cout<<this->m_hour<<":"<<this->m_minute<<"
"<<this->m_todo<<"\n";
}
Whole Code:
class Entry
{
private:
int m_hour;
int m_minute;
string m_todo;
public:
Entry()
{
m_hour = -1;
m_minute = -1;
m_todo = "";
}
void setData(int hour, int minute, const char *td)
{
this->m_hour=hour;
this->m_minute=minute;
this->m_todo=td;
}
void setData(Entry & e)
{
this->m_hour=e.m_hour;
this->m_minute=e.m_minute;
this->m_todo=e.m_todo;
}
//This routine prints a line that would look something like:
// 12:30 Take a Walk
void printEntry()
{
cout<<this->m_hour<<":"<<this->m_minute<<"
"<<this->m_todo<<"\n";
}
};
class Schedule
{
private:
// Array of Entry
Entry * m_entryArr;
// Max size of the Array
int m_maxEntries;
// Number of entries filled in with setData (starts out = 0)
int m_actualEntryCount;
public:
// This Constructor will initialize 2 ints and define the
m_entryArr
Schedule(int maxEntries)
{
this->m_maxEntries=m_maxEntries;
this->m_actualEntryCount=0;
m_entryArr=new Entry[m_maxEntries];
}
// Copy Constructor – it is done for you
Schedule(Schedule & s)
{
m_actualEntryCount = s.m_actualEntryCount;
m_maxEntries = s.m_maxEntries;
m_entryArr = new Entry[m_maxEntries];
for (int i=0; i < m_actualEntryCount; i++)
{
m_entryArr[i].setData(s.m_entryArr[i]);
}
}
// addEntry returns true if it succeeds, false if we are out of
space
// Note that addEntry will merely call setData to fill in the next
entry
// in the array and increment the m_actualEntryCount
bool addEntry(int hour, int minute, const char *todo)
{
if(m_actualEntryCount==m_maxEntries-1)
{
return false;
}
m_entryArr[m_actualEntryCount].setData(hour,minute,todo);
m_actualEntryCount++;
return true;
}
// This routine prints out all entries (i.e. as specified by
m_actualEntryCount)
void printSchedule()
{
for (int i=0; i < m_actualEntryCount; i++)
{
m_entryArr[i].printEntry();
}
}
~Schedule()
{
delete [] m_entryArr;
}
};
class Entry { private: int m_hour; int m_minute; string m_todo; public: Entry() { m_hour = -1; m_minute = -1; m_todo = ""; } void setData(int hour, int minute, const char *td) { this->m_hour=hour; this->m_minute=minute; this->m_todo=td; } void setData(Entry & e) { this->m_hour=e.m_hour; this->m_minute=e.m_minute; this->m_todo=e.m_todo; } //This routine prints a line that would look something like: // 12:30 Take a Walk void printEntry() { cout<<this->m_hour<<":"<<this->m_minute<<" "<<this->m_todo<<"\n"; } }; class Schedule { private: // Array of Entry Entry * m_entryArr; // Max size of the Array int m_maxEntries; // Number of entries filled in with setData (starts out = 0) int m_actualEntryCount; public: // This Constructor will initialize 2 ints and define the m_entryArr Schedule(int maxEntries) { this->m_maxEntries=m_maxEntries; this->m_actualEntryCount=0; m_entryArr=new Entry[m_maxEntries]; } // Copy Constructor – it is done for you Schedule(Schedule & s) { m_actualEntryCount = s.m_actualEntryCount; m_maxEntries = s.m_maxEntries; m_entryArr = new Entry[m_maxEntries]; for (int i=0; i < m_actualEntryCount; i++) { m_entryArr[i].setData(s.m_entryArr[i]); } } // addEntry returns true if it succeeds, false if we are out of space // Note that addEntry will merely call setData to fill in the next entry // in the array and increment the m_actualEntryCount bool addEntry(int hour, int minute, const char *todo) { if(m_actualEntryCount==m_maxEntries-1) { return false; } m_entryArr[m_actualEntryCount].setData(hour,minute,todo); m_actualEntryCount++; return true; } // This routine prints out all entries (i.e. as specified by m_actualEntryCount) void printSchedule() { for (int i=0; i < m_actualEntryCount; i++) { m_entryArr[i].printEntry(); } } ~Schedule() { delete [] m_entryArr; } };