473,396 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

design question in c++ classes

Hi,

i'm a student and got an assignment, and was wandering what is the best way to design it, or simply put - what would a skilled programmer do?

here goes: (it's a bit long, so sorry in advance and thank you)

i have to create 2 containers as templates one of Queue and the other of Priority Queue (which is a queue with sorted items)

Oueue has these Methods:
Push - push into back of queue
Pop - remove item from top of queue
Front - inspect item in front
Back - inspect item in back

pQueue has these Methods:
Push - push into a sorted place in the queue (the item i'll be inserting will have operator< to comare)
Pop - remove item from top of queue
Front - inspect item in front

that's it...

notice that pQueue does not have a Back method and is not allowed to have one such as public, moreover no other public methods are allowed to exist for any client of these container classes.

hese is what i did:
i created a BaseQueue that stores the data in linked list with Elm *head, *tail and the methods:
Push - into the back of the Queue
Pop - the same thing in both containers
Front - the same thing in both containers

and inherited public from this one into pQueue and Queue.

the problem was that in order to get into the middle/end of this queue i'll have to throw out all items and put them in a temp queue and then return them back to the original queue when i'm done. --- This was a big problem with pQueue because i has to play with the entier Queue to search for the right position to insert the value, (pop out until i find the right place) and Back at queue not much simpler...

So.... is this a good way of doing this? (even though it has a very bad performence considering i'm poping and pushing back everything just to insert one item in pqeueu and running all the list to get to back in queue)

Any other suggestion or better way? i couldn't think of any...

Thanks in advance...
Koren. :-)
Aug 9 '07 #1
2 1314
weaknessforcats
9,208 Expert Mod 8TB
Your desing is OK but this class is not:
i created a BaseQueue that stores the data in linked list with Elm *head, *tail and the methods:
Push - into the back of the Queue
Pop - the same thing in both containers
Front - the same thing in both containers
If the BaseQueue is a linked list, then it should have linked list methods like:
1) add to end/front
2) remove from end/front/middle
3) insert at front/end/specificed position

You derive your stack from this BaseQueue and implement the Push as a call to BaseQueue::AddToEnd().

By keeping BaseQueue as a linked list your problems disappear.

And while you are at it, I would rename BaseQueue to something like LinkedList to better describe what it is.
Aug 9 '07 #2
RRick
463 Expert 256MB
Koren,

Your original design had QueueP (QP) using only the methods from Queue (Q) to perform its sorting. That means that QP can be derived from Q. You're right, this has issues, especially with storage.

Then you created a base class that would have just enough functionality to keep Q and QP happy if they inherit the base class publicly. That works, but there is another alternative. QP can inherit Q as protected. Then QP defines public methods for just the functionality it needs to reveal. Now there is no need for a base class. Also, this design decouples the underlying structure of Q from QP. Now Q can change and QP doesn't care. Also, Q's push method is a good candidate for a virtual method.

W4cats suggested a design with some class having enough functionality for both objects. This can be a base class or it can be the Queue class. The extra functionality will have to be protected by Q (and available to QP). W4cats suggested a link list but even vectors/arrays will work. Now QP can get the base structure quickly and can optimize its own push method. The down side is that Q and QP are tightly coupled. Any changes to Q's structure might change QP's behavior.

What's best depends on your application. What kind of time frame you are dealing with? Is it throwaway code? Does this scale well? These are all issues that affect the design.
Aug 9 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Patchwork | last post by:
Hi Everyone, I have a design related question (in C++) that I am hoping someone can help me with. It is related to my previous post but since it was pointed out that I was more or less asking...
3
by: Omer van Kloeten | last post by:
The Top Level Design: The class Base is a factory class with a twist. It uses the Assembly/Type classes to extract all types that inherit from it and add them to the list of types that inherit...
13
by: Bryan Parkoff | last post by:
I have created three classes according to my own design. First class is called CMain. It is the Top Class. Second class and third class are called CMemory and CMPU. They are the sub-classes....
13
by: KV | last post by:
I'm new to OO Design, and I'm fixing to start writing my very first C# program. Given the complexity of OO programming, I would like to run something by this group and get general input. My...
0
by: Edward Diener | last post by:
In Borland's VCL it was possible to divide a component into design time and run time DLLs. The design time DLL would only be necessary when the programmer was setting a component's properties or...
17
by: tshad | last post by:
Many (if not most) have said that code-behind is best if working in teams - which does seem logical. How do you deal with the flow of the work? I have someone who is good at designing, but...
20
by: Brad Pears | last post by:
I am completely new to vb .net. I am using visual Studio 2005 to redo an Access 2000 application into a .net OO application using SQL Server 2000 - so a complete rewrite and re-thinking of how...
9
by: Grizlyk | last post by:
Somebody have offered std colors to C++ in the msg here: http://groups.google.com/group/comp.lang.c++/browse_frm/thread/2e5bb3d36ece543b/1acf6cd7e3ebdbcd#1acf6cd7e3ebdbcd The main objection to...
0
weaknessforcats
by: weaknessforcats | last post by:
Design Patterns: Visitor Introduction Polymorphism requires a class hierarchy where the interface to the hierarchy is in the base class. Virtual functions allow derived classes to override base...
10
by: vital | last post by:
Hi, I am designing the middle tier of a project. It has 6 classes and microsoft application data access block. The six classes are DBServices, Logger, ProjectServices ... etc. and all these...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.