![]() |
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?
:
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... :
Note that NO inheritance is used here. Portfolio does not inherit anything from Stock or vice versa. |
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:
:
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. |
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? |
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:
:
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. ;) |
Re: one class to implement a linked list of objects of another class
Ok I'm gonna make a guess...
:
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 :
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? |
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: :
|
| All times are GMT -5. The time now is 7:40 PM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC