J
John J
I requested help with some code in a previous thread, as requested in the
feedback, below are the .cpp and .h files for all three classes (Entry, Race
and Yacht). The three classes are all ossociated through pointers. Objects
are created and called in a main as follows: (I apologise for the excess of
white space, unfortunately this occurs when I cut and paste from my
compiler)
Race r1(1, "23/12/03");
Race r2(2, "24/12/03");
Yacht y1("The Bounty", "William Bligh");
Yacht y2("The Hilda", "Some bloke");
Yacht y3("The Mary Celeste", "Benjamin Briggs");
Yacht y4("Speedy", "Some other bloke");
r1.enter_race (&y1,3, "22:30");
r1.enter_race (&y2,2, "22:00");
r1.enter_race (&y3,1, "21:00");
r2.enter_race (&y4,1, "15:00");
cout << r1 << endl;
cout << r2 << endl;
r1.display_entries ();
r2.display_entries ();
r1.winner ();
The classes seem to compile and link correctly; however, the Race::winner
function does not work and I'm not sure on how to code the Yacht::add_entry
function, comments of what this should do are in the body of the add_entry
function, this also needs to be called from the Race::enter_race function,
as commented within this function.
I realise my solution below could be refined somewhat; however, I'm still
very much learning and would greatly appreciate help on getting this project
to work.
Thanks
//Entry.cpp
#include <iostream>
#include <string>
#include "Entry.h"
#include "Race.h"
#include "Yacht.h"
using namespace std;
//Construct an Entry
Entry::Entry (Race* r, Yacht* y, int pl, string ti)
{
which = r;
what = y;
place = pl;
time = ti;
}
//Access functions
int Entry::getPlace () const
{
return place;
}
string Entry::getTime () const
{
return time;
}
Race* Entry::getRace () const
{
return which;
}
Yacht* Entry::getYacht () const
{
return what;
}
//Overload of operator<< for output of an Entry
ostream& operator<< (ostream& out, const Entry& e)
{
Yacht* y = e.what;
Race* r = e.which;
out << endl << "Yacht : " << *y << endl
<< "Race : " ; r->Show();
out << "Finish Place : " << e.place << endl
<< "Finish Time : " << e.time << endl << endl;
return out;
}
//Entry.h
#ifndef ENTRY_H
#define ENTRY_H
#include <iostream> // for standard i/o
#include <string> // for string manipulation
using std:stream;
using std::string;
class Yacht;
class Race;
//Details of race entries
class Entry
{
friend ostream& operator<< (ostream&, const Entry&);
public:
//constructor
Entry (Race* r=NULL, Yacht* y=NULL, int pl=0, string ti="");
//access functions
int getPlace () const; //Yacht placing
string getTime () const; //Yacht finish time
Race* getRace () const; //Related race object
Yacht* getYacht () const; //Related yacht object
private:
//Attributes to implement the relationship
Race* which; //Which race
Yacht* what; //What yacht
//Attributes of the association
int place;
string time;
};
ostream& operator<< (ostream&, const Entry&);
#endif
//Race.cpp
#include <string>
#include "Race.h"
#include "Entry.h"
#include "Yacht.h"
using namespace std;
//Construct a race
Race::Race (int n, string d)
{
number = n;
date = d;
nEntries = 0;
for (int j=0; j<MAX_ENTRIES; j++)
entries[j] = NULL;
}
//Enter a race
void Race::enter_race (Yacht* y, int pl, string ti)
{
if (nEntries<MAX_ENTRIES)
{
Entry* e = new Entry (this,y,pl,ti);
entries[nEntries++] = e;
//y->add_entry; //need to call the Yacht::add_entry function here
}
else cout << "Too many entries" << endl;
}
//Find an entry object(s)
void Race::display_entries (ostream& out) const
{
if (nEntries == 0)
out << "No entries" << endl;
else
{
cout << "Details of Yachts entered into the requested race:" <<endl << endl;
for (int i=0; i<nEntries; i++)
out << *(entries);
}
}
//Find the winner
void Race::winner (ostream& out) const
{
//int i;
Entry* e = NULL;
if (nEntries == 0)
{
out << "No entries" << endl;
}
else
{
for (int i=0; i<nEntries; i++)
{
e = entries;
if (e)
if (e->getPlace() == 1)
{
out << e;
}
else
{
out << "No winner was found in this race" << endl;
}
}
}
}
//Access functions
int Race::getNumber (void) const
{
return number;
}
string Race::getDate (void) const
{
return date;
}
//Output functions
//Print Race info
void Race::Show (ostream& out) const
{
out << "Race Number : " << number << endl
<< "Race Date : " << date << endl;
}
ostream& operator<< (ostream& out, const Race& r)
{
r.Show (out);
if (r.nEntries == 0)
out << "No entries" << endl;
else
{
for (int i=0; i<r.nEntries; i++)
out << *(r.entries);
}
return out;
}
//Race.h
#ifndef RACE_H
#define RACE_H
#include <iostream> // for standard i/o
#include <string> // for string manipulation
using std:stream;
using std::cout;
#include "Entry.h"
#include "Yacht.h"
const int MAX_ENTRIES = 20;
//Details of races
class Race
{
friend ostream& operator<< (ostream&, const Race&);
public:
//constructor
Race ( int n=0, string d="");
void enter_race (Yacht*,int, string);
void Show (ostream& out=cout) const;
void display_entries (ostream& out=cout) const;
void winner (ostream& out=cout) const;
//member function prototypes
int getNumber() const;
string getDate() const;
private:
//Attributes of yachts
int number;
string date;
//Attributes for association
Entry* entries[MAX_ENTRIES];
int nEntries;
};
ostream& operator<< (ostream&, const Race&);
#endif
//Yacht.cpp
#include <iostream>
#include <string>
#include "Yacht.h"
using namespace std;
//Construct a yacht
Yacht::Yacht (string n, string c)
{
name = n;
captain = c;
}
//Enter a race
void Yacht::add_entry ()
{
//Add_entry just needs to be given a pointer to the
//just-created new entry as its sole argument, and
//it should just store that pointer in the next unused
//location in an array of pointers to entries (mirroring
//the array of pointers to entries in the Race class).
//I have not yet declared the array of pointers in the
//Yacht class - it needs to be added and I'm not sure on
//how to do this correctly
}
//Access functions
string Yacht::getName (void) const
{
return name;
}
string Yacht::getCaptain (void) const
{
return captain;
}
//Overload of operator<< for output of a yacht
ostream& operator<< (ostream& out, const Yacht& y)
{
out << y.name << " (Captain: " << y.captain << ")";
return out;
}
//Yacht.h
#ifndef YACHT_H
#define YACHT_H
#include <iostream> // for standard i/o
#include <string> // for string manipulation
#include "Entry.h"
#include "Race.h"
using namespace std;
//Details of yachts
class Yacht
{
friend ostream& operator<< (ostream&, const Yacht&);
void add_entry ();
public:
//constructor
Yacht (string n="", string c="");
//member function prototypes
string getName() const;
string getCaptain() const;
private:
//Attributes of yachts
string name;
string captain;
};
ostream& operator<< (ostream&, const Yacht&);
#endif
feedback, below are the .cpp and .h files for all three classes (Entry, Race
and Yacht). The three classes are all ossociated through pointers. Objects
are created and called in a main as follows: (I apologise for the excess of
white space, unfortunately this occurs when I cut and paste from my
compiler)
Race r1(1, "23/12/03");
Race r2(2, "24/12/03");
Yacht y1("The Bounty", "William Bligh");
Yacht y2("The Hilda", "Some bloke");
Yacht y3("The Mary Celeste", "Benjamin Briggs");
Yacht y4("Speedy", "Some other bloke");
r1.enter_race (&y1,3, "22:30");
r1.enter_race (&y2,2, "22:00");
r1.enter_race (&y3,1, "21:00");
r2.enter_race (&y4,1, "15:00");
cout << r1 << endl;
cout << r2 << endl;
r1.display_entries ();
r2.display_entries ();
r1.winner ();
The classes seem to compile and link correctly; however, the Race::winner
function does not work and I'm not sure on how to code the Yacht::add_entry
function, comments of what this should do are in the body of the add_entry
function, this also needs to be called from the Race::enter_race function,
as commented within this function.
I realise my solution below could be refined somewhat; however, I'm still
very much learning and would greatly appreciate help on getting this project
to work.
Thanks
//Entry.cpp
#include <iostream>
#include <string>
#include "Entry.h"
#include "Race.h"
#include "Yacht.h"
using namespace std;
//Construct an Entry
Entry::Entry (Race* r, Yacht* y, int pl, string ti)
{
which = r;
what = y;
place = pl;
time = ti;
}
//Access functions
int Entry::getPlace () const
{
return place;
}
string Entry::getTime () const
{
return time;
}
Race* Entry::getRace () const
{
return which;
}
Yacht* Entry::getYacht () const
{
return what;
}
//Overload of operator<< for output of an Entry
ostream& operator<< (ostream& out, const Entry& e)
{
Yacht* y = e.what;
Race* r = e.which;
out << endl << "Yacht : " << *y << endl
<< "Race : " ; r->Show();
out << "Finish Place : " << e.place << endl
<< "Finish Time : " << e.time << endl << endl;
return out;
}
//Entry.h
#ifndef ENTRY_H
#define ENTRY_H
#include <iostream> // for standard i/o
#include <string> // for string manipulation
using std:stream;
using std::string;
class Yacht;
class Race;
//Details of race entries
class Entry
{
friend ostream& operator<< (ostream&, const Entry&);
public:
//constructor
Entry (Race* r=NULL, Yacht* y=NULL, int pl=0, string ti="");
//access functions
int getPlace () const; //Yacht placing
string getTime () const; //Yacht finish time
Race* getRace () const; //Related race object
Yacht* getYacht () const; //Related yacht object
private:
//Attributes to implement the relationship
Race* which; //Which race
Yacht* what; //What yacht
//Attributes of the association
int place;
string time;
};
ostream& operator<< (ostream&, const Entry&);
#endif
//Race.cpp
#include <string>
#include "Race.h"
#include "Entry.h"
#include "Yacht.h"
using namespace std;
//Construct a race
Race::Race (int n, string d)
{
number = n;
date = d;
nEntries = 0;
for (int j=0; j<MAX_ENTRIES; j++)
entries[j] = NULL;
}
//Enter a race
void Race::enter_race (Yacht* y, int pl, string ti)
{
if (nEntries<MAX_ENTRIES)
{
Entry* e = new Entry (this,y,pl,ti);
entries[nEntries++] = e;
//y->add_entry; //need to call the Yacht::add_entry function here
}
else cout << "Too many entries" << endl;
}
//Find an entry object(s)
void Race::display_entries (ostream& out) const
{
if (nEntries == 0)
out << "No entries" << endl;
else
{
cout << "Details of Yachts entered into the requested race:" <<endl << endl;
for (int i=0; i<nEntries; i++)
out << *(entries);
}
}
//Find the winner
void Race::winner (ostream& out) const
{
//int i;
Entry* e = NULL;
if (nEntries == 0)
{
out << "No entries" << endl;
}
else
{
for (int i=0; i<nEntries; i++)
{
e = entries;
if (e)
if (e->getPlace() == 1)
{
out << e;
}
else
{
out << "No winner was found in this race" << endl;
}
}
}
}
//Access functions
int Race::getNumber (void) const
{
return number;
}
string Race::getDate (void) const
{
return date;
}
//Output functions
//Print Race info
void Race::Show (ostream& out) const
{
out << "Race Number : " << number << endl
<< "Race Date : " << date << endl;
}
ostream& operator<< (ostream& out, const Race& r)
{
r.Show (out);
if (r.nEntries == 0)
out << "No entries" << endl;
else
{
for (int i=0; i<r.nEntries; i++)
out << *(r.entries);
}
return out;
}
//Race.h
#ifndef RACE_H
#define RACE_H
#include <iostream> // for standard i/o
#include <string> // for string manipulation
using std:stream;
using std::cout;
#include "Entry.h"
#include "Yacht.h"
const int MAX_ENTRIES = 20;
//Details of races
class Race
{
friend ostream& operator<< (ostream&, const Race&);
public:
//constructor
Race ( int n=0, string d="");
void enter_race (Yacht*,int, string);
void Show (ostream& out=cout) const;
void display_entries (ostream& out=cout) const;
void winner (ostream& out=cout) const;
//member function prototypes
int getNumber() const;
string getDate() const;
private:
//Attributes of yachts
int number;
string date;
//Attributes for association
Entry* entries[MAX_ENTRIES];
int nEntries;
};
ostream& operator<< (ostream&, const Race&);
#endif
//Yacht.cpp
#include <iostream>
#include <string>
#include "Yacht.h"
using namespace std;
//Construct a yacht
Yacht::Yacht (string n, string c)
{
name = n;
captain = c;
}
//Enter a race
void Yacht::add_entry ()
{
//Add_entry just needs to be given a pointer to the
//just-created new entry as its sole argument, and
//it should just store that pointer in the next unused
//location in an array of pointers to entries (mirroring
//the array of pointers to entries in the Race class).
//I have not yet declared the array of pointers in the
//Yacht class - it needs to be added and I'm not sure on
//how to do this correctly
}
//Access functions
string Yacht::getName (void) const
{
return name;
}
string Yacht::getCaptain (void) const
{
return captain;
}
//Overload of operator<< for output of a yacht
ostream& operator<< (ostream& out, const Yacht& y)
{
out << y.name << " (Captain: " << y.captain << ")";
return out;
}
//Yacht.h
#ifndef YACHT_H
#define YACHT_H
#include <iostream> // for standard i/o
#include <string> // for string manipulation
#include "Entry.h"
#include "Race.h"
using namespace std;
//Details of yachts
class Yacht
{
friend ostream& operator<< (ostream&, const Yacht&);
void add_entry ();
public:
//constructor
Yacht (string n="", string c="");
//member function prototypes
string getName() const;
string getCaptain() const;
private:
//Attributes of yachts
string name;
string captain;
};
ostream& operator<< (ostream&, const Yacht&);
#endif