Jump to content
  • Advertisement
Sign in to follow this  
hothead

need help with inheritence

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

It seems the more I try to debug this the worse it gets so I put it back in it's original state
#include<iostream>
using namespace std;

class cat
{
public:
    void meow();
};
    void meow()
    {
        cout<<"meow"<<endl;
    }
class bigcat : public cat
{
int meow();
};

int main()
{
    cat.meow()
    bigcat.meow();
    
    int x;
    cin>>x;
    return 0;
}
error: parse error before '.' line 20 or it could be before ';'

Share this post


Link to post
Share on other sites
Advertisement

#include<iostream>
using namespace std;

class cat
{
public:
void meow();
};
void cat::meow()
{
cout<<"meow"<<endl;
}
class bigcat : public cat
{
int meow();
};

int main()
{
cat.meow()
bigcat.meow();

int x;
cin>>x;
return 0;
}


You were mising the cat:: before the definition of meow

Share this post


Link to post
Share on other sites
why didn't it work? the only error i see is he's missing the definition to the bigcat::meow. that and its private. try this:


#include<iostream>
using namespace std;

class cat
{
public:
void meow();
};
void cat::meow()
{
cout<<"meow"<<endl;
}
class bigcat : public cat
{
public:
void meow(){cout << " big meow"<<endl;}
};

int main()
{
cat.meow()
bigcat.meow();

int x;
cin>>x;
return 0;
}



Share this post


Link to post
Share on other sites
Your code has glaring errors.


#include <iostream>
using namespace std;

class cat
{
public:
virtual void meow(); // note the use of the "virtual" keyword
};

// notice how to define a class method
void cat::meow()
{
cout << "meow" << endl;
}

class bigcat : public cat
{
public:
virtual void meow(); // note that the signature must remain
// the same, including return type
};

void bigcat::meow()
{
cout << "roar" << endl;
}

int main()
{
cat *cptr;
cat kitty;
bigcat tigger;

// this is how you employ polymorphic dispatch. if you're not
// doing something like this, then your inheritance is unnecessary
cptr = &kitty;
cptr->meow();
cptr = &tigger;
cptr->meow();

cout << "Press enter to exit" << endl;
cin.get();
return 0;
}

Share this post


Link to post
Share on other sites
if he's only trying to inherit the meow method, he doesn't need to redefine it - its not virtual, and its public. so it should be :

class cat
{
public:
void meow(void)
};
void cat::meow(void)
{
std::cout << "meow" << std::endl;
}
class bigcat : public cat
{
};
int main()
{
cat c;
bigcat d;
c.meow();
d.meow();

return 0;

}

<edit :: or the meow method can be changed in the inherited class like Oluseyi showed

Share this post


Link to post
Share on other sites
Quote:
Original post by graveyard filla
why didnt it work?
Because cat.meow() and bigcat.meow() are invalid nonstatic method invocations. Where's the object?

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!