Search code examples
c++linked-listvirtual

Virtual functions within link list's - multiple return types (object)


I have an assignment in uni to do, basically, we take in a bunch of data from many txt files. the data is structured in a way that it can be read, for this question ill cut it down to two files and two classes. an example of the data is this:

Faculty.txt ( PK, Name)

AT  Advanced Technology 
HESAS   Health, Sport and Science   

Department.txt (PK, Name, FK)

ET  Engineering and Technology  AT  
BE  Built Environment   AT  
CMS Computing and Mathematical Sciences AT  
SAS Science and Sport   HESAS   
CS  Care Sciences   HESAS   
PESD    Professional Education and Service Delivery HESAS   

The idea is to create report's using the data, the program is meant to use link lists, relationships and virtual functions. So in the code we basically have a list (im using FILO) of nodes-

-Out of context question-

(im not sure im getting the lectures provided node and list code concept we have been given - I say this because I cannot get my head around where node stores the memory address of the object. Unless im misunderstanding it and node is a child of each class it extends so it inherits everything from that class and add's its stuff on top? if that is the case why do we need to make a virtual function in the 1st place if we inherit all of the functionality - security?)

-that have pointers of objects stored inside them. now where im actually stuck is loading the data in - well iv loaded it all into lists fine, but i have only loaded the Foreign key's in as strings where as i need to basically check the string FK against the PK of the objects stored in the previous file iv loaded (the lecture went easy and gave us a easy 1-8 flow where 1 has no FK:)) and then when i find that object get its memory address and use that as the FK. the idea behind this is you can basically go std::cout << departmentObj->getForeignKey()->getIdentifier(); ill post some code from my test project (chopped down for testing) at this point since im not sure if im making any seance.

-- sorry guys the code block here is bugging, so it will have to be paste bin.

List.cpp http://pastebin.com/Le3fz5YF

List.h http://pastebin.com/5yJYDM8N

Node.cpp http://pastebin.com/Pgas8eju

Node.h http://pastebin.com/TZPrEA4Q

Fac.cpp http://pastebin.com/0EGeGhdq

dep.cpp http://pastebin.com/G2yk6jCg

Main.cpp http://pastebin.com/npiCC6wX

refrence loader http://pastebin.com/n6UdsYmW

so basically it comes down to returning the memory address of an desired object through the list. clearly im not getting how to access the node and list class correctly to do this. the way i would like to be able to do this is just simply have a nice line of code that i can use to replace the addnode lines in my loader (ill post that for reference) it would look like this:

departmentList->AddNode(new Department(holder[0], holder[1], facultyList->getPkObjAdd(holder[2])));

but ofc the problem lies with how to return that objects memory address. if it has to be a virtual function, how do i allow it to return multiple object types like faculty and department?

im not sure if im making much seance, i hope someone can help me out. Thanks for your time!! - quick edit, forgot dep and fac.cpp's


Solution

  • Hey thought id pop back and post the solution incase anyone wonders in future. since im handling data types where i know the return value i decided to use:

    void* getMe();

    void* as you may or may not know is apointer to mem address. you can then convert it using

    (Faculty*)facultyList->getPkMemAdd("PK");

    hope it helps someone!!