Skip to main content
gamedev.net
🔒 Locked

C++ simple inheritance question

Started by Bleakcabal 8 replies 450+ views
Original Post
Bleakcabal
Bleakcabal
Hi, I have the following classes class Sound { protected : int i; public : bool Load(); bool Play(); bool Close(); }; class Wave : public Sound { // stuff }; class Mp3 : public Sound { // stuff }; I tought with inheritance a could define bool Wave::Load() { //code } bool Mp3::Load() { //code } But it says that Load is not declared in Wave or Mp3, with my small and new notions of inheritance I tought this was possible. Is it ? If so where is my erro and if not what would you suggest, creating a load and play for every derived class ?
WHO DO THEYTHINK THEY'REFOOLING : YOU ?
Zipster
Zipster
Yes, load must be virtual. It must also be defined in those derived classes as well.
Bleakcabal
Bleakcabal
But if I make virtual woundlt that means that it is created only once, thus if I create an instance of Wave and Mp3 and want to code in these functions to be different then I could not as with virtual there would be only one function with that name ?? Or does virtual only applies inside the "scope" of a class and its inherited classes ?

Then overloading must be the way to go ? Unless I am mistaken in the above paragraph.
WHO DO THEYTHINK THEY'REFOOLING : YOU ?
Promiscuous Robot
Promiscuous Robot
It was probably a simple mistake on the AP''s part, but when you redeclare Load() in the Wave class you don''t put virtual again.

Like so:

class Sound
{
public:
//blah
virtual bool Load ();
// ...
};

class Wave : public Sound
{
//blah
bool Load (); // ''Load'' is overridden here
// ...
};

Also, since I''m guessing that Sound is the abstract base class (ie. you never actually construct an Sound object--you only construct Wave''s or Mp3''s depending on the source of the sound) then you would best declare Load() in Sound like this:

virtual bool Load () = 0;

This will force you to override Load() in Wave and Mp3 or any other derived class. I''m sorry if you know all this, it just seems like you didn''t.


--------------------------
I guess this is where most people put a famous quote...
"Everything is funnier with monkey''s" - Unknown
--------------------------I guess this is where most people put a famous quote..."Everything is funnier with monkey''s" - Unknown
Bleakcabal
Bleakcabal
No I didnt know and im very glad you told me thank you, now my program works ! You were right I never would have use the load funciton from sound. That fix my problem. And also I didnt know that I would not have to declare a sound object just a wave one, I tough it was necessary to do both.

I always tought that a derived class had every inheritors functions included in it without redifining them.

Thx for you help everybody.
WHO DO THEYTHINK THEY'REFOOLING : YOU ?
Stoffel
Stoffel
Simple inheritence tutorial (using structs so everything''s public:

    
struct Base
{
void func () { cout << "Base"; }
};

struct Derived1 : public Base
{
void func () { cout << "Derived1"; }
};

struct Derived2 : public Base
{
void func () { cout << "Derived2"; }
};

//program

{
Base b;
Derived1 d1;
Derived2 d2;
Base* p;
b.func (); // calls Base::func, of course
p = &d1 // assign base pointer to derived1 struct

p->func (); // calls Base::func because it''s not virtual

p = &d2 // assign base pointer to derived2 struct

p->func (); // also calls Base::func for same reason

// p->func, since func is not virtual, is a statically-bound
// function call. There''s no virtual look-up table, so
// the compiler hard-codes a call to Base::func when it sees

// p->func

// so, output of this function would be "BaseBaseBase"
}

// now let''s try it with virtual functions:


struct Base
{
virtual void func () { cout << "Base"; }
};

struct Derived1 : public Base
{
// virtual keyword not needed here, but considered polite.
virtual void func () { cout << "Derived1"; }
};

struct Derived2 : public Base
{
// ditto above comment

virtual void func () { cout << "Derived2"; }
};

// program

{
Base b;
Derived1 d1;
Derived2 d2;
Base* p;
b.func (); // calls Base::func, of course
p = &d1 // assign base pointer to derived1 struct

p->func (); // calls Derived1::func through vtable

p = &d2 // assign base pointer to derived2 struct

p->func (); // calls Derived2::func through vtable

// the calls to func through the base-pointer are dynamically

// bound, meaning the compiler doesn''t know ahead of time
// which function it''s going to be calling. It checks which

// function table to call through the vtable, which is set
// up when you describe a function as virtual and filled-in

// when you assign a pointer to a derived class.

// the output of this function is "BaseDerived1Derived2"

}


Hope this helps.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.