Is there a way not to declare a virtually declared function in a superclass?
Sure. But here is the difference:
Code:
#include <iostream>
using namespace std;
class Dog
{
public:
void identify()
{
cout<<"I'm a dog."<<endl;
}
};
class GermanShepherd : public Dog
{
void identify()
{
cout<<"I'm a German Shepherd."<<endl;
}
};
int main()
{
Dog* pDog = 0;
Dog aDog;
pDog = &aDog;
pDog->identify(); //I'm a dog.
GermanShepherd Max;
pDog = &Max;
pDog->identify(); //I'm a dog.
return 0;
}
Code:
#include <iostream>
using namespace std;
class Dog
{
public:
virtual void identify()
{
cout<<"I'm a dog."<<endl;
}
};
class GermanShepherd : public Dog
{
void identify()
{
cout<<"I'm a German Shepherd."<<endl;
}
};
int main()
{
Dog* pDog = 0;
Dog aDog;
pDog = &aDog;
pDog->identify(); //I'm a dog.
GermanShepherd Max;
pDog = &Max;
pDog->identify(); //I'm a German Shepherd.
return 0;
}