Hi,
Just a short question. I was wondering if one has a base class for example bird and a derived class pigeon, if i instantiate an object of each class, B1 and P1.
Is it possible to pass the address of B1 or P1 into a function
such as DoSomething(&B1) and DoSomething(&P1) (I need to have DoSomething handle both birds and pigeons!)
and then in the function have P1->Amethod() or B1->Amethod where Amethod is defined in the base class bird and should work for both bird and pigeon.

Thanks for the help, it's kind of difficult to explain so try and do your best!

Recommended Answers

All 4 Replies

The answer is yes. If the function is virtual, you can redefine it in Pigeon to do something different:

#include <iostream>

class Bird {
public:
    virtual void Amethod() {
        std::cout<<"Amethod from Bird\n";
    }
};

class Pigeon: public Bird {
    virtual void Amethod() {
        std::cout<<"Amethod from Pigeon\n";
    }
};

void DoSomething ( Bird *p )
{
    p->Amethod();
}

int main()
{
    Bird B1;
    Pigeon P1;

    DoSomething ( &B1 );
    DoSomething ( &P1 );
}

Or you can leave it non-virtual and it'll do the same thing for both classes:

#include <iostream>

class Bird {
public:
    void Amethod() {
        std::cout<<"Amethod from Bird\n";
    }
};

class Pigeon: public Bird {
};

void DoSomething ( Bird *p )
{
    p->Amethod();
}

int main()
{
    Bird B1;
    Pigeon P1;

    DoSomething ( &B1 );
    DoSomething ( &P1 );
}

Either way, you're relying on polymorphism to allow the DoSomething function to take pointers to objects of both classes.

What can I say! That is the best response I've ever gotten in a forum!...Thanks a million, that really clarified it for me. I thought I could use polymorphism but thought the function had to be a method of an object. Didn't know you could do this with a standalone function.
Thanks again!

You've misread Narue's code. You can't do this with a standalone function. It must be a member of a class.

In the first example, Amethod() was made virtual in the base class (Bird) and overloaded in the descendant class (Pidgeon). Since it is virtual (or dynamic) when you say p->Amethod() the Pidgeon's Amethod() gets called if p is a Pidgeon; and the Bird's Amethod() gets called if p is a Bird.

In the second example, Amethod() is static (or not dynamic), so even if you give Pidgeon an Amethod() member function, since p is listed as a "pointer to Bird" the Bird's Amethod() is always called. That is the reason why Narue didn't bother to give Pidgeon an Amethod() method (member function).

This is the difference between Object Oriented (the dynamic way) and Object Based (the static way).

Hope this helps.

Oh right, I think I get it now. Currently working on a project where I need to do something along these lines. This has helped a great deal, I'll let you know how I get on.
It's the second example I'll be using (static binding).
Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.