Jump to content
  • Advertisement
Sign in to follow this  
leet bix

Inheritance

This topic is 3410 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

Hey guys, I'm being confused by this, so I'm looking for a little help. Ok, so basically I have 3 different classes, Base, DerivedClass1 and DerivedClass2. Ok, so Base is virtual, like this;
class Base
{
public:
    virtual void SetValue(float fValue)= 0;
    virtual void DoSomeWork() = 0;

protected:
    float m_fValue;
private:
};

Now, what I want to do is this;
void Base(float fValue)
{
    m_fValue = fValue;
}

then, use inheritance to make Base::SetValue() public in derived class and have m_fValue protected in the derived class, then I want to be able to have DoSomeWork defined in each derived class, as they will have different functionality in their own derived class. So basically, I want it to end up something like this;
[source lang"cpp"]
// not sure which access specifier to use
class DerivedClass1 : public Base
{
public:
     void DoSomeWork(); // not sure if I need this here
     // SetValue would be public

private:
     // value would be private
}

void DerivedClass1::DoSomeWork()
{
     int value = 1 + 1;
}
the only difference between DerivedClass1 and DerivedClass2 would be the definition of DoSomeWork(), if anyone can help, I would be greatly appreciative.

Share this post


Link to post
Share on other sites
Advertisement
your Base is an Abstract class, this means that your DerivedClass1 needs to implement SetValue and DoSomeWork();

i think what you looking for is:


class Base:
{
public:
void SetValue(float f);

protected:
float m_fVale; //since its protected it can be accessed directly by the derived class
};

class DerivedClass1 : public Base
{
public:
//the SetValue will be visible since its public;
};



I hope am Right correct me if am wrong...

Share this post


Link to post
Share on other sites
Quote:
Original post by leet bix
So then I'd leave DoSomeWork() as a pure virtual in Base?


Yes.

Share this post


Link to post
Share on other sites
Awesome, one more thing that I'd like to clarify is if the following is true


class Base
{
public:
void SetValue(float fValue)= 0;
virtual void DoSomeWork() = 0;

protected:
float m_fValue;
private:
};

void Base::SetValue(float fValue)
{
m_fValue = fValue;
}

class DerivedClass1 : public Base
{
public:
void DoSomeWork();
}

int main()
{
DerivedClass1 MyClass;

MyClass.DoSomeWork(); // will just add 1 and 1
MyClass.SetValue(5); // will set MyClass.m_fValue to 5

return 0;
}


Share this post


Link to post
Share on other sites
Drop the '= 0' from SetValue -- that function is not virtual anymore, much less pure virtual.

You obviously also need to provide an implementation of DerivedClass1::DoSomeWork().

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!