By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,968 Members | 1,691 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,968 IT Pros & Developers. It's quick & easy.

Creating methods for a data base

P: n/a
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.

Apr 17 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
alternativa wrote:
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?
You can use functionals: function objects.
these are classes with only one method (usually operator()). their
instance may be passed as an argument (much like function pointers)
for instance:
class snamePredicat
{
string sname;
snamePredicat(string valueToCompare):sname(valueToCompare){}//which is
//just like:sname=valueToCompare
bool operator()(const Student& s) {return sname==s.getSname();}
}

and the "while" will be:
while (current && predicat(current))

and the call to Search is:
snamePredicat predicat(toFind);//creating the instance for the function
object
Search(predicat, head);//passing it as a parameter

you can take a look at STL algorithms for some examples. this is a
common way for such tasks in an OO code...
BTW you may want to recheck your code since it will not compile, i
think (e.g. SomeClass* :: Search should be SomeClass::Search)

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 ???).


you can use friends. however, writing a getField() method (also called
a getter) is considered a better way and is sufficient for the task you
set...

Apr 17 '06 #2

P: n/a
or******@gmail.com wrote:
alternativa wrote:
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?
You can use functionals: function objects.
these are classes with only one method (usually operator()). their
instance may be passed as an argument (much like function pointers)
for instance:
class snamePredicat
{
string sname;
snamePredicat(string valueToCompare):sname(valueToCompare){}//which is
//just like:sname=valueToCompare
bool operator()(const Student& s) {return sname==s.getSname();}
}

and the "while" will be:
while (current && predicat(current))

and the call to Search is:
snamePredicat predicat(toFind);//creating the instance for the function
object
Search(predicat, head);//passing it as a parameter

you can take a look at STL algorithms for some examples. this is a
common way for such tasks in an OO code...
BTW you may want to recheck your code since it will not compile, i
think (e.g. SomeClass* :: Search should be SomeClass::Search)

I have some supplement for Oren's comment.
He gave a good direction for alternativa's issue.
however he left something aside. Alternativa wana a general function for
him to search a content with different kind of key word. So I suggest
Alternativa to use template based on the Oren's answer.

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 ???).


you can use friends. however, writing a getField() method (also called
a getter) is considered a better way and is sufficient for the task you
set...

If a object wana call a method of another class, the class must be
instantiated unless this method is a static method. And the method must
be public or your method should be accessed by your object friendly. So
u can use the way as below:
1. you can define a public static method for globle call.
2. see the design pattern "Compose".

Apr 17 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.