need help with inheritence

Started by
6 comments, last by graveyard filla 19 years, 5 months ago
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 ';'
-----------------------------------Panic and anxiety Disorder HQ
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
that didn't work
-----------------------------------Panic and anxiety Disorder HQ
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;}
FTA, my 2D futuristic action MMORPG
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 methodvoid 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;}
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
- stormrunner
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?
well, it was more of a rhetorical question, but.. [smile]. heh, i actually didn't even catch that.
FTA, my 2D futuristic action MMORPG

This topic is closed to new replies.

Advertisement