473,782 Members | 2,494 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Searching within an abstract data type (ADT)

I have an ADT and I'd like to search for certain items in it. How can I
accomplish this?
Oct 10 '05 #1
9 2601
deanfamily11 wrote:
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.
Oct 10 '05 #2
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.

"Jacques Labuschagne" <ja*****@clawsh rimp.com> wrote in message
news:Mt******** *************@n ews.xtra.co.nz. .. deanfamily11 wrote:
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.

Oct 10 '05 #3
deanfamily11 wrote:
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[i].getTitle() == user_title){
cout << "Found it!\n";
break;
}
}

Is there something wrong with this solution?

Jacques.
Oct 10 '05 #4
Your suggestion I think works, but I need to know how to define getBook in
the area to define the class.

"Jacques Labuschagne" <ja*****@clawsh rimp.com> wrote in message
news:BP******** *************@n ews.xtra.co.nz. ..
deanfamily11 wrote:
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[i].getTitle() == user_title){
cout << "Found it!\n";
break;
}
}

Is there something wrong with this solution?

Jacques.

Oct 10 '05 #5
deanfamily wrote:
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.
Oct 10 '05 #6
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(stri ng);

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

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

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

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

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

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

string bookType::print Title() const
{
return title;
}

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

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

int bookType::showN umCopies() const
{
return numInStock;
}

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

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

int bookType::print NumCopies() const
{
return numInStock;
}

string bookType::showP ublisher() const
{
return publisher;
}

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

void bookType::updat ePublisher(stri ng newPub)
{
publisher = newPub;
}

string bookType::print Publisher() const
{
return publisher;
}

string bookType::showI SBN() const
{
return isbn;
}

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

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

string bookType::print ISBN() const
{
return isbn;
}

double bookType::showP rice() const
{
return price;
}

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

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

double bookType::print Price() const
{
return price;
}

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

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

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

string bookType::print Authors() const
{
return authors[0];
return authors[1];
return authors[2];
return authors[3];
}
"Jacques Labuschagne" <ja*****@clawsh rimp.com> wrote in message
news:OG******** *************@n ews.xtra.co.nz. ..
deanfamily wrote:
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.

Oct 10 '05 #7
deanfamily wrote:
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[i].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(in t);
void bookType::setCo pies(int copiesIn)
{
numInStock = copiesIn;
}

void bookType::updat eCopies(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::print Authors() 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
Oct 10 '05 #8
The problem that I am mainly having with the code:
for (int i = 0; i < array_size; ++i){
if (array[i].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" <jo************ *@hotmail.com> wrote in message
news:wf******** *********@newsf e6-win.ntli.net... deanfamily wrote:
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[i].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(in t);


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

void bookType::updat eCopies(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::print Authors() 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

Oct 11 '05 #9
* 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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 11 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2762
by: SunScreen | last post by:
Hi all, I would like to help with the following: // my conventions: // BC = Base Class // DC = Derived Class class BC // this ADT base class or interface {
7
3643
by: Dave | last post by:
I have a system that basically stores a database within a database (I'm sure lots have you have done this before in some form or another). At the end of the day, I'm storing the actual data generically in a column of type nvarchar(4000), but I want to add support for unlimited text. I want to do this in a smart fashion. Right now I am leaning towards putting 2 nullable Value fields: ValueLong ntext nullable ValueShort nvarchar(4000)...
12
1631
by: Mars | last post by:
I searched and found quite many, but (nearly) all are about C++....... Any recommendation for such reference for pure C???? Thx~~~~ Mars.
7
4474
by: jason | last post by:
In the microsoft starter kit Time Tracker application, the data access layer code consist of three cs files. DataAccessHelper.cs DataAcess.cs SQLDataAccessLayer.cs DataAcccessHelper appears to be checking that the correct data type is used DataAcess sets an abstract class and methods
3
22193
by: EquinoX | last post by:
I am currently studying about this ADT but I don't get the concept at all. Can someone please explain to me what this actually is?? Also what is collection, bag, and List ADT?? in simple terms? Thanks
3
2975
by: kalar | last post by:
I have an exersice and must make an abstarct data type (ADT) , but i have no idea what is ADT ? Linked list and ADT have any relationship? I want a simple tutorial about ADT. I google it but i don't find something simple for beginning.Thanks.
3
2667
by: ramsisiv | last post by:
I know that it is possible but, if the abstract class has a virtual function print, that prints info about the objekt and the derived classes each have theire own version of print, will a print funktion in the ADT that prints the info in the current objekt by using its print function use the print function from the superclass or from the derived class(es)? I'm not shore if virtual helps me here sense the nodes in the ADT take a pointer to an...
2
3415
by: alwaali | last post by:
Hi I need help please This is my project and i need a help to solve it with you A page of text is to be read and analyzed to determine number of occurrences and locations of different words. The results of the analysis are to be stored in a suitable data structure. The main task in this project is to design a suitable ADT (call it WAnalysis) to store the results and enable the following operations to be performed as fast as possible:...
0
9639
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9479
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10311
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10146
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8967
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5509
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3639
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2874
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.