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.
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401