A Cat and a Dog

This is a discussion on A Cat and a Dog within the C++ Programming forums, part of the General Programming Boards category; If I have two objects like the dog and cat below and I wanted a instance of Cat to find ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    23

    Unhappy A Cat and a Dog

    If I have two objects like the dog and cat below and I wanted a instance of Cat to find out the age of an instance of Dog, how would I do that?

    A function like int AskDogAge(&Dog theDog); // How would this work?

    This is probably easy but I am very slow. Please help.

    class Cat {

    public: Cat(){}
    ~Cat(){}

    int GetAge() {return itsAge;}
    void SetAge(int age) {itsAge = age;}

    private:
    int itsAge;
    }

    class Dog {

    public: Dog(){}
    ~Dog(){}

    int GetAge() {return itsAge;}
    void SetAge(int age) {itsAge = age;}

    private:
    int itsAge;
    }

  2. #2
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    This could be a great example of inheritance but anyway - to the question:

    int Cat::getDogAge( const Dog& mutt ) const
    {
    return mutt.getAge( );
    }

    Hope that helps
    Last edited by endo; 07-22-2002 at 02:39 PM.

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    94
    Also, you could probably make Dog a friend of Cat, though this may be a little beyond where you're at. In your Cat class, try this:

    Code:
    class Cat
    {
        public:
        friend class Dog;
        ...
    }
    Class Dog needs to be made before class Cat, by the way, if you do this. Then, your Cat objects can access any Dog data. The other way mentioned, the one before this message, is porbably much simpler, however, so do that.

    Brendan
    Last edited by harry_p; 07-22-2002 at 09:30 AM.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    23

    Unhappy

    Thanks, those were both very helpful

    Now, what if I want Dog to respond by checking Cat's age and only answering if its above a certain limit?

    I have been trying to do this but if you compile Cat with a Dog reference in it or vice versa you get errors. There must be some obvious way to solve this problem. Otherwise all communication between objects is too heirarchical.

    This will not compile though:

    Help, again.


    class Cat {

    public: Cat(){}
    ~Cat(){}


    int GetAge() {return itsAge;}
    void SetAge(int age) {itsAge = age;}

    int getDogAge( const Dog& mutt ) const // Dog& is not recognised !!!
    {
    return mutt.getAge( );
    }



    private:
    int itsAge;
    }

    class Dog {

    public: Dog(){}
    ~Dog(){}

    int GetAge() {return itsAge;}
    void SetAge(int age) {itsAge = age;}

    int getCatAge( const Cat& mutt ) const
    {
    return mutt.getAge( );
    }



    private:
    int itsAge;
    }

  5. #5
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    My guess is that it doesnīt know what a Dog is. Problably isnīt dog declared yet (from the compiler view). Try to move Dog up before the declaration of Cat. Hope that helps.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    23

    Unhappy

    Yes, thats the problem though

    I want them to talk to eachother but only one can ask and the other one answer.

    What I am working towards is a kind of security set-up.

    Cat asks Dog its age.

    Dog asks Cat if Cat is old enough to be asking.

    Cat answers with its age.

    Dog tests age of Cat.

    Dog replies with age if Cat is old enough to be asking.

    Problem is what you pointed out. Either catt or dog won't know about the other because it has not been declared yet. There must be a way to do this though...

    A am baffled though

  7. #7
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    You can use forward declarations, I think its like this (though I've never used it):
    Code:
    class cat;
    class dog;
    
    cat
    {
    public:
       cat( );
       ~cat( );
    };
    
    dog
    {
    public
       dog( );
       ~dog( );
    };

  8. #8
    Registered User
    Join Date
    Jul 2002
    Posts
    94
    Okay let's put my short-term memory to test and try and get this figured out according to what you said you want done:

    Alright, you have Cat access Dog's age:

    Code:
    int Cat::GetDogAge(Dog &mutt)
    {
         return mutt.GetAge();
    }
    Now, you want Dog to make sure that Cat is old enough. So modify the function to this:

    Code:
    int Cat::GetDogAge(Dog &mutt)
    {
         if (mutt.GetCatAge(this) < 3)
         {
              // GetCatAge() takes a pointer to a Cat and returns it's age
              // If its age is less than 3, it's not old enough
              cout << "Cat isn't old enough!\n";
              return -1;
         }
         else
              return mutt.GetAge();
    }
    Hope that helps a little.

    Brendan

  9. #9
    Registered User
    Join Date
    Jul 2002
    Posts
    94
    One last thing. The GetCatAge() function in Dog needs to be defined AFTER the Cat class. For example:

    Code:
    class Dog
    {
         ...
    }
    
    class Cat
    {
         ...
    }
    
    int Dog::GetCatAge(Cat * c)
    {
         return c->GetAge();
    }
    I just created a program to test all of the things everyone has mentioned to you and it runs swimmingly . Just remember:

    - Make forward class declarations
    - Define the Dog::GetCatAge() function after the Cat class has been defined
    - Uh, I don't remember all the other stuff. Just read over all the posts.

    Brendan

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    23
    Thanks, I will have to wait to test all this but it makes at least some sense.

    What I was working towards was some way that objects which encounter eachother could find out stuff about each other without having prior warning of the other thing's existence. That make sense?

    so Cat might have a function:

    int Cat::AskAge(SomeClass& otherthing){

    return otherthing.GetAge();

    }

    //This means that the object met would have to pass its type to the Cat //immediately on meeting it.

    //So lets say its a

    Dog mutt;

    // Dog needs to have a function which passes its class to whatever it meets
    // or puts it in some global buffer?

    char typebuffer[100];

    //Function would be like this then

    void Dog::informOfType() {

    typebuffer = "Dog";
    }

    //then when objects meet they check global the global buffer(s) and then
    //use whatever string is there as their reference. Would that work?


    int Cat::AskAge(typebuffer& otherthing){

    return otherthing.GetAge();

    }

    //It would all have to be declared in advance though eh?

  11. #11
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    I might be wrong but I am pretty sure that buffer idea won't work. Your best bet would be to make the animals into a heirarchy using inheritance, but it doesn't seem to me that you've reached that yet.

    You could start with an animal class then derive cat and dog from that. Then you can use base class pointers to achieve your desired affect I think.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21