Searching within an abstract data type (ADT)

D

deanfamily11

I have an ADT and I'd like to search for certain items in it. How can I
accomplish this?
 
J

Jacques Labuschagne

deanfamily11 said:
I have an ADT and I'd like to search for certain items in it. How can I
accomplish this?

Be more specific. Does the object have a container you need to search
through? Or are you talking about using reflection to find/invoke a
particular function? Or are you trying to use assumptions about the
object layout to find certain data members?

Jacques.
 
D

deanfamily11

Ok, here's an example and please note this is supposed to be an array of
class objects or variables, also note that >> indicates that those are
variable of that item in the array:
book[1]
title
author
price book[2]
title
author
price
book[3]
....

In this case, I'd like to be able to search through all of the items for a
given title supplied by the user.
 
J

Jacques Labuschagne

deanfamily11 said:
Ok, here's an example and please note this is supposed to be an array of
class objects or variables, also note that >> indicates that those are
variable of that item in the array:
book[1]
title
author
price
book[2]
title
author
price

book[3]
...

In this case, I'd like to be able to search through all of the items for a
given title supplied by the user.

OK, sounds simple so far. So what's the public interface of the Book
object? Something like this?

class Book{
private:
...
public:
const string& getTitle() const;
...
};

If so, then the solution is to step through the array and check the
value of Book::getTitle() for every element:

for (int i = 0; i < array_size; ++i){
if (array.getTitle() == user_title){
cout << "Found it!\n";
break;
}
}

Is there something wrong with this solution?

Jacques.
 
D

deanfamily

Your suggestion I think works, but I need to know how to define getBook in
the area to define the class.

Jacques Labuschagne said:
deanfamily11 said:
Ok, here's an example and please note this is supposed to be an array of
class objects or variables, also note that >> indicates that those are
variable of that item in the array:
book[1]
title
author
price
book[2]

title
author
price

book[3]
...

In this case, I'd like to be able to search through all of the items for
a given title supplied by the user.

OK, sounds simple so far. So what's the public interface of the Book
object? Something like this?

class Book{
private:
...
public:
const string& getTitle() const;
...
};

If so, then the solution is to step through the array and check the value
of Book::getTitle() for every element:

for (int i = 0; i < array_size; ++i){
if (array.getTitle() == user_title){
cout << "Found it!\n";
break;
}
}

Is there something wrong with this solution?

Jacques.
 
J

Jacques Labuschagne

deanfamily said:
Your suggestion I think works, but I need to know how to define getBook in
the area to define the class.

Why don't you post the code of your ADT so we have some context?

Jacques.
 
D

deanfamily

Ok, here it is for your reference, and also I need to do the search in ISBN
number:

class bookType
{
public:
//this part is for the book title
string printTitle() const;
void setTitle(string);
void checkTitle(string);

//this part is for the number of copies
int showNumCopies() const;
void setCopies(int);
void updateCopies(int);
int printNumCopies() const;

//this part is for the publisher
string showPublisher() const;
void setPublisher(string);
void updatePublisher(string);
string printPublisher() const;

//this part is for the ISBN
string showISBN() const;
void setISBN(string);
void updateISBN(string);
string printISBN() const;

//this part is for the price
double showPrice() const;
void setPrice(double);
void updatePrice(double);
double printPrice() const;

//this part is for the authors
string showAuthors() const;
void setAuthors(string, int);
void updateAuthors(string, int);
string printAuthors() const;

private:
string title;
string publisher;
string isbn;
double price;
int numInStock;
string authors[4];
};

string bookType::printTitle() const
{
return title;
}

void bookType::setTitle(string titleName)
{
title = titleName;
}

void bookType::checkTitle(string newTitle)
{
if(newTitle == title)
cout << "That title already exists." << endl;
}

int bookType::showNumCopies() const
{
return numInStock;
}

void bookType::setCopies(int copiesIn)
{
numInStock = copiesIn;
}

void bookType::updateCopies(int newNum)
{
numInStock = newNum;
}

int bookType::printNumCopies() const
{
return numInStock;
}

string bookType::showPublisher() const
{
return publisher;
}

void bookType::setPublisher(string publish)
{
publisher = publish;
}

void bookType::updatePublisher(string newPub)
{
publisher = newPub;
}

string bookType::printPublisher() const
{
return publisher;
}

string bookType::showISBN() const
{
return isbn;
}

void bookType::setISBN(string isbnIn)
{
isbn = isbnIn;
}

void bookType::updateISBN(string newIsbn)
{
isbn = newIsbn;
}

string bookType::printISBN() const
{
return isbn;
}

double bookType::showPrice() const
{
return price;
}

void bookType::setPrice(double priceIn)
{
price = priceIn;
}

void bookType::updatePrice(double newPrice)
{
price = newPrice;
}

double bookType::printPrice() const
{
return price;
}

string bookType::showAuthors() const
{
return authors[0];
return authors[1];
return authors[2];
return authors[3];
}

void bookType::setAuthors(string authorIn, int authNum)
{
authors[authNum] = authorIn;
}

void bookType::updateAuthors(string setAuthor, int authNum)
{
authors[authNum] = setAuthor;
}

string bookType::printAuthors() const
{
return authors[0];
return authors[1];
return authors[2];
return authors[3];
}
 
J

John Harrison

deanfamily said:
Ok, here it is for your reference, and also I need to do the search in ISBN
number:

OK that seems easy enough. See below
class bookType
{
public:
//this part is for the book title
string printTitle() const;

See the function you have called printTitle? It doesn't print anything
so it is badly named. In fact it is almost exactly the function getTitle
that Jacques imagined.

You could rewrite Jacques code like following

for (int i = 0; i < array_size; ++i){
if (array.printTitle() == user_title){
cout << "Found it!\n";
break;
}
}

No do you see how sily the name printTitle is? The above code works but
the name is confusing. So use Jacques' solution but rename the function
to getTitle, you will have to rename it everywhere that you are using it.

You have similar confusion all over the place. Look at this
void setCopies(int);
void updateCopies(int);
>
> void bookType::setCopies(int copiesIn)
> {
> numInStock = copiesIn;
> }
>
> void bookType::updateCopies(int newNum)
> {
> numInStock = newNum;
> }

The functions setCopies and updateCopies are *exactly* the same. Why did
you feel the need to have two functions. Just get rid of one of them.

The same problems with many others, they are either duplicates or have
inappropriate names.
string bookType::printAuthors() const
{
return authors[0];
return authors[1];
return authors[2];
return authors[3];
}

This just doesn't work, it will only return the first author (and again
it won't print anything) the three following return statements will be
ignored.

When I see these sort of naming problem I know the programmer is
confused. You shoul name a function after what it does, not how you use
it. getTitle because you the function get the title from the book, not
printTitle because you happed to use the function to print then title.

Assuming this is homework you will definitely lose marks for
inappropriate names for your functions, and also for having duplicate
functions.

john
 
D

deanfamily

The problem that I am mainly having with the code:
for (int i = 0; i < array_size; ++i){
if (array.printTitle() == user_title){
cout << "Found it!\n";
break;


is that == is for integers and it won't compile as a result. The
"user_title" and "printTitle" parts are strings. How do you change the code
so that it can be used for strings?

John Harrison said:
deanfamily said:
Ok, here it is for your reference, and also I need to do the search in
ISBN number:

OK that seems easy enough. See below
class bookType
{
public:
//this part is for the book title
string printTitle() const;

See the function you have called printTitle? It doesn't print anything so
it is badly named. In fact it is almost exactly the function getTitle that
Jacques imagined.

You could rewrite Jacques code like following

for (int i = 0; i < array_size; ++i){
if (array.printTitle() == user_title){
cout << "Found it!\n";
break;
}
}

No do you see how sily the name printTitle is? The above code works but
the name is confusing. So use Jacques' solution but rename the function to
getTitle, you will have to rename it everywhere that you are using it.

You have similar confusion all over the place. Look at this
void setCopies(int);
void updateCopies(int);
void bookType::setCopies(int copiesIn)
{
numInStock = copiesIn;
}

void bookType::updateCopies(int newNum)
{
numInStock = newNum;
}

The functions setCopies and updateCopies are *exactly* the same. Why did
you feel the need to have two functions. Just get rid of one of them.

The same problems with many others, they are either duplicates or have
inappropriate names.
string bookType::printAuthors() const
{
return authors[0];
return authors[1];
return authors[2];
return authors[3];
}

This just doesn't work, it will only return the first author (and again it
won't print anything) the three following return statements will be
ignored.

When I see these sort of naming problem I know the programmer is confused.
You shoul name a function after what it does, not how you use it. getTitle
because you the function get the title from the book, not printTitle
because you happed to use the function to print then title.

Assuming this is homework you will definitely lose marks for inappropriate
names for your functions, and also for having duplicate functions.

john
 
A

Alf P. Steinbach

* deanfamily:
[top- and middle-posting]

Please stop top-posting and middle-posting in this group.

Thanks.

- Alf


PS: Please also try the suggestions you've been given before protesting that
they won't work.
 

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,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top