Archived

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

Having a problem with virtual methods

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

I got this code here that is giving me an unexpected end of file found error I cant seem to find a reason why. I am gonna use the source tags for the first time so I hope it works:
  

#include <iostream.h>
#include <stdlib.h>

class mammal
{

   public:
      mammal() { cout << "Mammal constructor \n";
                 setage(); };
      mammal(int age) { cout << "Mammal constructor \n";
                    setage(age); }; 
      virtual ~mammal() { cout << "Mammal destructor \n"; };
      virtual void speak() const { cout << "Mammal speak! \n"; };
      void move() { cout << "Mammal Moves! \n"; };
      void setage() { itsage = 1; };
      void setage(int age) {itsage = age; };
      void getage() { cout << itsage << endl; };
   
   protected:
      int itsage;

};

class dog : public mammal
{

   public:
      dog() { cout << "Dog constructor \n"; }; 
      virtual ~dog() { cout << "Dog destructor \n"; ); 
      void speak() { cout << "Woof! \n"; };
      void move() { cout << "dog moves! \n"; };

};

int main()
{
   mammal* fido = new dog;
   fido -> getage();
   fido -> speak();
   fido -> move();
   system("PAUSE");
   return 0;

}

  
I hope that works. I know that the program is quite juvenile but Im trying my hardest to create a progrm in a book without looking at it to seem if I actually can get the syntax right. But the book looks like this to and I still get this error. Thx, Jeff Desrosiers

Share this post


Link to post
Share on other sites
the 2 includes are actually on different lines not sure why that happened.


Thx,

Jeff Desrosiers

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Check out this line:

virtual ~dog() { cout << "Dog destructor \n"; );

See anything wrong with it?

--Jeff

Share this post


Link to post
Share on other sites
I might add that you have a bunch of redundant statement terminators (normal people call them "semicolons" ). When you do:
int setage() {itsage = 1;}; 

the last semicolon is unnecessary. (Just information.)

Share this post


Link to post
Share on other sites
Ah I see I tried this and it still worked thx a lot.



Thx,

Jeff Desrosiers

Share this post


Link to post
Share on other sites