Search function

J

John J

I am in the process of writing 3 associated classes to store details of
yacht racing information. Below is a section of code from my Entry class. An
entry object is created as follows "Entry e1 (&r1, &y1,1,"22:30");"
The &r1 is a reference to an associated Race object (Race class) and &y1 is
a reference to an associated Yacht object (Yacht class). The 1 is a place
ie, 1 = first, and "22:30" is the finish time.

I wish to add a function to the Race class that will search all entry
objects (class below) and display all the Yachts that entered a specified
race; however, all my attempts at a search function have failed. I'd
appreciate some advice on this.



#include <iostream>
#include <string>

#include "Entry.h"

using namespace std;

//Construct an Entry

Entry::Entry (Race* r, Yacht* y, int pl, string ti)

{

which = r;

what = y;

place = 0;

time = "";

}

//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)

{

out << "Finish Place: " << e.place << endl

<< "Finish Time: " << e.time << endl

<< "Race Nr (date): " << *(e.which)

<< "Yacht: " << *(e.what) << endl;

return out;

}
 
M

Mike Wahler

John J said:
I am in the process of writing 3 associated classes to store details of
yacht racing information. Below is a section of code from my Entry class. An
entry object is created as follows "Entry e1 (&r1, &y1,1,"22:30");"
The &r1 is a reference to an associated Race object (Race class) and &y1 is
a reference to an associated Yacht object (Yacht class). The 1 is a place
ie, 1 = first, and "22:30" is the finish time.

I wish to add a function to the Race class that will search all entry
objects (class below) and display all the Yachts that entered a specified
race; however, all my attempts at a search function have failed. I'd
appreciate some advice on this.



#include <iostream>
#include <string>

#include "Entry.h"

using namespace std;

//Construct an Entry

Entry::Entry (Race* r, Yacht* y, int pl, string ti)

{

which = r;

what = y;

place = 0;

time = "";

}

//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)

{

out << "Finish Place: " << e.place << endl

<< "Finish Time: " << e.time << endl

<< "Race Nr (date): " << *(e.which)

<< "Yacht: " << *(e.what) << endl;

return out;

}

Race r1;
Entry e1 (&r1, &y1,1,"22:30");"

if(*e.getRace == r1)
cout << e << '\n';

-Mike
 
J

John Harrison

John J said:
I am in the process of writing 3 associated classes to store details of
yacht racing information. Below is a section of code from my Entry class. An
entry object is created as follows "Entry e1 (&r1, &y1,1,"22:30");"
The &r1 is a reference to an associated Race object (Race class) and &y1 is
a reference to an associated Yacht object (Yacht class). The 1 is a place
ie, 1 = first, and "22:30" is the finish time.

I wish to add a function to the Race class that will search all entry
objects (class below) and display all the Yachts that entered a specified
race; however, all my attempts at a search function have failed. I'd
appreciate some advice on this.

I don't think you've supplied enough information. The definition of the Race
class is essential, as is the relationship between Race and Entry. You've
already said that each Entry has a reference to a Race, presumably each Race
also has references to all the Entries for that race in some manner.

I actually think its questionable that Entry has a reference to Race,
creating circular references (Entry has reference to Race and each Race has
references to all its Entries) is something that shouldn't be done lightly,
in my opinion. Of course it all depends on how everything hangs together in
your final system but I'd be surprised if it is really necessary.

Probably the best thing is not to be shy and to post one of your failed
attempts. Seeing code tends to be much more informative than reading
descriptions of code.

john
 
A

AlesD

John said:
I am in the process of writing 3 associated classes to store details of
yacht racing information. Below is a section of code from my Entry class. An
entry object is created as follows "Entry e1 (&r1, &y1,1,"22:30");"
The &r1 is a reference to an associated Race object (Race class) and &y1 is
a reference to an associated Yacht object (Yacht class). The 1 is a place
ie, 1 = first, and "22:30" is the finish time.

I wish to add a function to the Race class that will search all entry
objects (class below) and display all the Yachts that entered a specified
race; however, all my attempts at a search function have failed. I'd
appreciate some advice on this.



#include <iostream>
#include <string>

#include "Entry.h"

using namespace std;

//Construct an Entry

Entry::Entry (Race* r, Yacht* y, int pl, string ti)

{

which = r;

what = y;

place = 0;

time = "";

}

//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)

{

out << "Finish Place: " << e.place << endl

<< "Finish Time: " << e.time << endl

<< "Race Nr (date): " << *(e.which)

<< "Yacht: " << *(e.what) << endl;

return out;

}

Hello,

first of all you didn't tell us where do you store all entries which
is essential. I'll suppose that you use some kind of container - say list.

std::list<Entry> entry_list;

Then you have to create function or class which will be able to compare
some race with some entry and print the entry if it belongs to the race.
I suggest using class which is able to hold the information as well.
It's definition might look like this:

class PrintRaceEntries
{
private:
const Race& race_to_look_for;
std::eek:stream& target_stream;
public:
// constructor
PrintRaceEntries(const& Race rtlf, std::eek:stream& ts)
: race_to_look_for(rtlf), target_stream(ts) {};

// this function prints it's parameter it's getRace() equals to Race
// supplied in constructor of this class
void operator(const Entry& entry_in_question) {
if (entry_in_question.getRace() == race_to_look_for)
target_stream << entry_in_question;
};
};


Following line will apply operator() of temporary created instance of
PrintRaceEntries to all items in entry_list.

std::for_each(entry_list.begin(), entry_list.end(),
PrintRaceEntries(some_race, std::cout));

Easy, isn't is!?

Of course you can create many functors to implement different behaviour.
There are also other usefull algorithms which might come handy.

Please refer to STL documentation.

Ales
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Unexpected Result 8
Associated classes 3
Class search function 1
Calling a function in another class 2
Search function 0
Chatbot 0
Class problem 5
best result function 15

Members online

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top