• Advertisement

Archived

This topic is now archived and is closed to further replies.

problem using objects from derived classes in C++

This topic is 5997 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, now, I know that this works in Delphi, and I assumed it would work in C++ too, because it is the same idea (OOP, I mean). But I haven''t been able to implement it in C++. Can you help? Here it goes: I have created 4 classes. One is a base class, the other three are derived from it. I need an array of objects, that are instances of these three classes. I know that an array can have only items of the same type, that''s why I have derived them from one base class. I thought, for each item in the array I will just call the appropriate constructor, like this: baseClass* myArray[10]; myArray[0] = new derivedClass1(); myArray[1] = new derivedClass2(); .. etc. Each of the classes has overrided methods, so when calling them, each of the objects would know, that it is to call its version of the method. Where is the error or is this kind of construction possible in C++? Thanks for help. Misko Frisko

Share this post


Link to post
Share on other sites
Advertisement
I think I know what you''re talking about, and if I do, yes that''s possible. Here''s an example:
  
#include <stdio.h>

class Base {
public:
virtual void Print(void) = 0;
};

class Der1 : public Base {
public:
virtual void Print(void) {
printf("Der1\n");
}
};

class Der2 : public Base {
public:
virtual void Print(void) {
printf("Der2\n");
}
};

int main(void) {
Base *Array[2];

Array[0] = new Der1;
Array[1] = new Der2;

Array[0]->Print();
Array[1]->Print();

delete Array[0];
delete Array[1];

return 0;
}

That should work (assuming I didn''t mess anything up) and display:

Der1
Der2


[Resist Windows XP''s Invasive Production Activation Technology!]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
inheritance only works correctly if you use pointers. You cannot use a Derived where you can use a Base. Thus if you try to put a Derived in a Base variable it becomes a Base, the rest of the object won''t fit in the variable. It also calls all the Base methods because it decides which one to call based on the variable, not the object that you want. On the other hand if you use pointers you can do everything you want. You can have Base pointers point to Derived objects just fine without things messing up at all. So like Null and Void said, use an array of pointers; not objects.

Share this post


Link to post
Share on other sites
Null and Void, the example, you have written works. But what if I want to have member variables in the classes. For example,

int a; //in Base class
int b; //in Der1
int c; //in Der2

As I understand it, Der1 should have two member variables (a, b) and Der2 (a, c). But if I want to assign values to them, the program won''t compile.

int main(void) {
Base *Array[2];

Array[0] = new Der1;
Array[1] = new Der2;

Array[0]->a = 5;
Array[0]->b = 10; //it says, that this variable is not a member of Base, but this is not a Base object, but a Der1 object

Array[1]->a = 50;
Array[1]->c = 100; //the same problem

delete Array[0];
delete Array[1];
return 0;
}

Thanks.

Share this post


Link to post
Share on other sites
quote:
Original post by Miso
Null and Void, the example, you have written works. But what if I want to have member variables in the classes. For example,

int a; //in Base class
int b; //in Der1
int c; //in Der2

As I understand it, Der1 should have two member variables (a, b) and Der2 (a, c). But if I want to assign values to them, the program won''t compile.

int main(void) {
Base *Array[2];

Array[0] = new Der1;
Array[1] = new Der2;

Array[0]->a = 5;
Array[0]->b = 10; //it says, that this variable is not a member of Base, but this is not a Base object, but a Der1 object
Okey-dokey, this is kinda confusing at first. *(Array[0])''s dynamic type is indeed Der1, but it''s static type is Base. The compiler treats pointers-to-Base as if they pointed to Base objects. Let''s face it; the compiler has no way of knowing which subclass Array[0] points to without actually running the code. It may seem obvious to us in this case, but in more complex cases it becomes more clear.

The member variables you declared in Der1 and Der2 still exist. For example:

Der1 d1;
d1.b = 10; // works; compiler knows that it''s a Der1

Der1* d1ptr = new Der1;
d1ptr->b = 10 // works; compiler knows that it points to Der1

Base* bptr = new Der1;
bptr->b = 10 // error; compiler has no way of knowing bptr''s dynamic type

Usually, member variables in Der1 are used by member functions in Der1 and are hidden from the user (information hiding).

Not sure if that was the best explanation, but I hope it helped a little bit.

---
blahpers

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
If you need to do that put them into two different arrays. You might also want to stop using inheritance for this problem since it isn''t what you want. Oh and you probably shouldn''t use two different arrays, use two std::vectors or something else and buy a book on STL.

Share this post


Link to post
Share on other sites
This is the hardest part of using and understanding inheritence:

Only use inheritence when you want to treat a derived class as a base class.

Never use inheritence to share code. If you do so, you end up with inheritence trees that are far too deep and convoluted.

You can't access derived-class members through a base-class pointer. Trying to do so indicates a bad design. There should only be one point in the program where the base class and the derived class are related to each other, and that's the construction of the derived class. After that, everything should happen through the base class. This is why you usually overload derived class constructors to assign members specific to the derived class. After construction, if you want to manipulate the class through the base-class pointer, you may ONLY use the public interface of the base class.

Edited by - Stoffel on September 24, 2001 1:06:09 PM

Share this post


Link to post
Share on other sites

  • Advertisement