Bunday holda, speak () hali ham sof virtual funktsiya hisoblanadi (garchi biz uni keyinroq aniqlagan bo'lsak-da) va Animal hali ham abstrakt ajdod-sinf hisoblanadi (shuning uchun bu sinf obyektlarini initsializatsiyalash mumkin emas). Animal sinfidan meros bo'lgan har qanday sinf speak() metodini qayta aniqlashi kerak yoki u ham abstrakt sinf hisoblanadi.
using namespace std;
class Animal{ //bu abstrakt sinf;
protected:
string m_name;
public:
Animal(string name)
: m_name(name)
{
}
string getName() { return m_name; }
virtual const char* speak() = 0; //sof virtual funksiya
};
const char* Animal::speak()
{
return "buzz"; // jimlik bo'yicha realizatsiya
}
class Dragonfly: public Animal
{
public:
Dragonfly(string name)
: Animal(name)
{
}
virtual const char* speak()
{
return Animal::speak(); // Animal sinfining jimlik bo'yicha
//amalga oshirilishi qo'llaniladi
}
};
int main()
{
Dragonfly dfly("Barbara");
cout << dfly.getName() << " " << dfly.speak() <<" deydi";
}
Dasturni bajarish natijasi:
Barbara buzz deydi