D
deanfamily11
I have an ADT and I'd like to search for certain items in it. How can I
accomplish this?
accomplish this?
deanfamily11 said:I have an ADT and I'd like to search for certain items in it. How can I
accomplish this?
book[3]title
author
price book[2]
title
author
price
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]
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.
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.
deanfamily said:Your suggestion I think works, but I need to know how to define getBook in
the area to define the class.
deanfamily said: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 setCopies(int);
void updateCopies(int);
>
> void bookType::setCopies(int copiesIn)
> {
> numInStock = copiesIn;
> }
>
> void bookType::updateCopies(int newNum)
> {
> numInStock = newNum;
> }
string bookType:rintAuthors() const
{
return authors[0];
return authors[1];
return authors[2];
return authors[3];
}
for (int i = 0; i < array_size; ++i){
if (array.printTitle() == user_title){
cout << "Found it!\n";
break;
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:rintAuthors() 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
[top- and middle-posting]
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.