Creating methods for a data base

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.
 
O

oren.shm

alternativa said:
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...
 
K

Kaliven Lee

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.

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".
 

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

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top