473,326 Members | 2,813 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,326 software developers and data experts.

storing classes derived from an abstract class in a ADT(vector, stack, list, whatever

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 instance of the overclass as an argument (there are multiple derived classes and I want to be able to store them all in the same vector)



If the overclass's print is called, how do i get it to call the derived class(es)'s print instead?



Also when i store the derived class as an overclass will i lose the "extra variables" in the derived class or will they remain but i wont be able to "get or set" them. Will this cause a memoryleak?
Oct 14 '07 #1
3 2637
weaknessforcats
9,208 Expert Mod 8TB
I think you may not understand how objects are composed.

First: A derived object contains a base object inside it.

Second: The virtual function means that you will use this derived object as a base object pointer or reference and that the compiler is to call the derived class method when you say to call the base class method. This is fundamental object-oriented programming.

Third: Using the object as a base class pointer or reference, you cannot call any derived class methods that are not declared in the base class. If you have this situation, you may need to use a Visitor (see the Visitor deign pattern in the C/C++ Articles forum).

Fourth: Any attempt to copy the derived object as a base object will result in the derived portion of the object being chopped off. This is call slicing and is always an error. If you have a sliced object and you call a base method that was overriden by the derived class and that method uses derived class data members, your program will crash trying to access data that isn't there.

So, yes, storing your derived object as a base object loses all of the derived object's data members.

Fifth: If you are using a virtual function, there should be no code for it in the base class. If there is, you will lose it when you override. That forces the derived method to call the base method to recover the overriden code. The problem is that it may not be clear whether you call the base method before (pre-condtion) or after (post-condition) the derived class code. This ambiguity os not desirable and indicates a design fault: You never override methods with code unless they are hooks.
Oct 14 '07 #2
I think you may not understand how objects are composed.

First: A derived object contains a base object inside it.

Second: The virtual function means that you will use this derived object as a base object pointer or reference and that the compiler is to call the derived class method when you say to call the base class method. This is fundamental object-oriented programming.

Third: Using the object as a base class pointer or reference, you cannot call any derived class methods that are not declared in the base class. If you have this situation, you may need to use a Visitor (see the Visitor deign pattern in the C/C++ Articles forum).

Fourth: Any attempt to copy the derived object as a base object will result in the derived portion of the object being chopped off. This is call slicing and is always an error. If you have a sliced object and you call a base method that was overriden by the derived class and that method uses derived class data members, your program will crash trying to access data that isn't there.

So, yes, storing your derived object as a base object loses all of the derived object's data members.

Fifth: If you are using a virtual function, there should be no code for it in the base class. If there is, you will lose it when you override. That forces the derived method to call the base method to recover the overriden code. The problem is that it may not be clear whether you call the base method before (pre-condtion) or after (post-condition) the derived class code. This ambiguity os not desirable and indicates a design fault: You never override methods with code unless they are hooks.

Nah, I don't think you understood the question, I already knew what you wrote, but this is a bit more advanced, but don't worry I got help from my teacher. It's a pointer thing, like i suspected


Peace out!
Oct 15 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
Nah, I don't think you understood the question, I already knew what you wrote, but this is a bit more advanced, but don't worry I got help from my teacher. It's a pointer thing, like i suspected
I did understand the question. However, be sure to post your answer.


And good luck!
Oct 15 '07 #4

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

Similar topics

0
by: dom's | last post by:
Hi, When changing the jdk of Iplanet Web Server 6.0 from jre 1.2.2 to jdk 1.3.1 i've gote the error popSeen: corrupt seen class stack But on a other UNIX computer, i don't have any error. does...
1
by: barnesc | last post by:
Hi again, Since my linear algebra library appears not to serve any practical need (I found cgkit, and that works better for me), I've gotten bored and went back to one of my other projects:...
2
by: Colin Basterfield | last post by:
Hi, I have a list which is derived from CollectionBase, and it contains a list of User objects, which I want to Serialize out to an XML file. Is there anywhere where I can find how to decode...
2
by: '[] WiRaN | last post by:
I all, I need a classroom equivalent the "Class List" of c++, necessary to recoup given of some hands tanks, Bruno
2
by: Colin Basterfield | last post by:
Hi, I have two lists, the first is the complete list, so call it PermList, the second is a subsection of that list, call this TempList, both are of the same type, naemly strongly typed and...
1
by: Kivak Wolf | last post by:
This is one GREAT resource made by Microsoft. http://beta.asp.net/QUICKSTART/util/classbrowser.aspx Check it out! Kivak Wolf
4
by: alisaee | last post by:
plz check what i have made wrong what is requierd her is to creat class queue and class stack and run the push,pop operation . #include<iostream.h> #include<conio.h> #include<stdio.h> class...
1
by: swiftalpha | last post by:
Aloha, Smallish problem: I know how to serialize objects and all, but this has me worried: Asume this class. Person is a nice simple object. public class PersonList : List<Person> {...
9
by: cheesecaker | last post by:
How would a set of related values be stored together in a MySQL db? For example, if I wanted to store a user's buddy list, then the way I can think of would be something like this: user |...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.