![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Feb 2008
Posts: 6
Rep Power: 0
![]() |
one class to implement a linked list of objects of another class
say you're managing a stock portfolio... of class Portfolio, and it is to contain a linked list of several of class Stock... or more like... Portfolio is supposed to contain a Node structure to contain each Stock and the pointer to the head list... is this how it is done?
c++ Syntax (Toggle Plain Text)
Does this node actually contain a full Stock object instance? Or does a node within the Portfolio class only contain the pointer? There was some template for doing things like this... but I forget how that's done... let me attempt a more generic example... c++ Syntax (Toggle Plain Text)
Note that NO inheritance is used here. Portfolio does not inherit anything from Stock or vice versa. |
|
|
|
|
|
#2 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 630
Rep Power: 4
![]() |
Re: one class to implement a linked list of objects of another class
The general form of a linked list would probably be something like this:
c++ Syntax (Toggle Plain Text)
Note that a node just has data and a pointer to the next node, while the list has only a pointer to the first node in the list.
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Feb 2008
Posts: 6
Rep Power: 0
![]() |
Re: one class to implement a linked list of objects of another class
Thank you for your response... but to be more clear I will say two things...
1. We haven't started on templates yet... 2. Here is the site that explains what I need to implement... I'm not clear on what it's asking. http://people.eecs.ku.edu/~jvalland/...structions.pdf It's like real life... you have a portfolio that has a list of stocks in it... I'm just trying to make sure we're all clear on this. But thanks for your response. But I will try to interpret your response... "class LinkedList" == Portfolio? "T data" == Stock, as T can be any type? is this right? oh and instead of "class Node", the assignment actually specified a structure... I guess that would be a little simpler than calling it "class Node". In fact... this assignment is so confusing I really have no clue whatsoever he wants... it gives an example input file of stocks and whether to buy or sell them... but this whole transaction class seems so unnecessary... couldn't you just buy and sell each individual stock, maintain a portfolio of what stocks have been touched, and sum up the money buy/sell totals? I guess the transaction list is just something to keep track of the buy and sell actions? Last edited by jason999x; Feb 16th, 2008 at 12:20 AM. |
|
|
|
|
|
#4 | |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 630
Rep Power: 4
![]() |
Re: one class to implement a linked list of objects of another class
I quickly read the assignment and it appears he wants you to read in stock data into a home-made linked-list and do operations on them based on some functions that you've been given the declarations for. It also appears that each Stock should carry it's own linked-list of transactions. So you have a linked-list of linked-lists.
Quote:
c++ Syntax (Toggle Plain Text)
There's a great tutorial by Narue (a moderator here) on linked lists here. I don't claim to know why the assignment requirements are what they are -- I just know how linked lists work. ![]()
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! |
|
|
|
|
|
|
#5 | |
|
Newbie
Join Date: Feb 2008
Posts: 6
Rep Power: 0
![]() |
Re: one class to implement a linked list of objects of another class
Ok I'm gonna make a guess...
c++ Syntax (Toggle Plain Text)
something like that? The assignment does say to use "structures"... I don't think I would define the class within a class as you seem to try to do... Okay he finally made something kind of clear... he may ask us to do this differently from the others... but here's what he says on http://people.eecs.ku.edu/~jvalland/...ab2_notes2.pdf Quote:
But I have some other question here... why does one use the address of an i/o stream instead of the iostream itself in the parameter? I'll show you his prototypes for two functions, buy and save, in the Portfolio class c++ Syntax (Toggle Plain Text)
The way we have been used to doing file i/o is doing it on the main.cpp, and then having other functions use that input data... all the instream variables were defined in main, usually. We (as in EECS at KU students) haven't yet dealt with i/o stream in parameters before... is the & convention? Actually what I should really be asking is will (ifstream&) vs. (ifstream) change how I would code the Buy and Sell functions dramatically? Last edited by jason999x; Feb 16th, 2008 at 11:23 AM. |
|
|
|
|
|
|
#6 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 630
Rep Power: 4
![]() |
Re: one class to implement a linked list of objects of another class
The & refers to a reference, which is a way of passing parameters to things such as functions.
I won't bother explaining references fully, as Narue does that nicely here. By writing a function that takes a std::ifstream object, you can specify the source of the input. For example: c++ Syntax (Toggle Plain Text)
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| linked list problems | bl00dninja | C++ | 6 | Feb 17th, 2008 10:30 AM |
| Linked List Of Objects | Ade | C++ | 10 | May 14th, 2007 10:25 AM |
| singly-linked list templaste class in C++ w/ example driver | bl00dninja | Show Off Your Open Source Projects | 0 | Sep 11th, 2006 1:05 AM |
| Singly Linked List Help | Firebar | Java | 3 | May 22nd, 2005 10:56 AM |
| airport Log program using 3D linked List : problem reading from file | gemini_shooter | C++ | 0 | Mar 2nd, 2005 4:12 PM |