A
alternativa
Hi,
I have a following problem: I need to create a set of classes which
together can make a data base.
Say we have:
class Student {
string sname;
int snumber;
Student *snextptr; //objects will be stored in a linked
list
public:
Student();
~Student();
SSearch); //!!!
};
class Book {
string bname;
int bnumber;
Book *bnextptr;
public:
Book();
~Book();
BSearch(); //!!!
};
function for searching will look as follows:
SomeClass* :: Search (string toFind, SomeClass *head) //&&&
{
SomeClass *current = head;
if (!head)
{
return NULL;
}
else
{
while (current && current -> someField != toFind ) //&&&
current = current -> next;
}
return current;
}
In my program I'd like to have a possibliity of searching for sname,
snumber, bname and bnumber, so I would have to create 4 functions with
almost the same body, differing only on two lines (with comments //&&&
). My question is: what to do to avoid repeating the same body of the
function? Is it possible to write one function Search which called with
a certain parameter would search for a certain field? Can one object
use a method from a different class (friend ???).
regards,
alt.
I have a following problem: I need to create a set of classes which
together can make a data base.
Say we have:
class Student {
string sname;
int snumber;
Student *snextptr; //objects will be stored in a linked
list
public:
Student();
~Student();
SSearch); //!!!
};
class Book {
string bname;
int bnumber;
Book *bnextptr;
public:
Book();
~Book();
BSearch(); //!!!
};
function for searching will look as follows:
SomeClass* :: Search (string toFind, SomeClass *head) //&&&
{
SomeClass *current = head;
if (!head)
{
return NULL;
}
else
{
while (current && current -> someField != toFind ) //&&&
current = current -> next;
}
return current;
}
In my program I'd like to have a possibliity of searching for sname,
snumber, bname and bnumber, so I would have to create 4 functions with
almost the same body, differing only on two lines (with comments //&&&
). My question is: what to do to avoid repeating the same body of the
function? Is it possible to write one function Search which called with
a certain parameter would search for a certain field? Can one object
use a method from a different class (friend ???).
regards,
alt.