//Early and Late binding
#include<iostream>
usingnamespace std ;
class klm {
public :
void getdata(){
cout << "Enter name ";
cin >> name ;
cout << "Enter age ";
cin >> age ;
}
virtualvoid display();
char name[20];
int age ;
};
class mno : public klm {
public :
void getdata();
void display(){
cout << "\n Name of the person " << name ;
cout << "\n Age of the person " << age ;
}
};
int main() {
class klm *ref ;
class mno obj;
ref= &obj;
ref->getdata(); // static binding or early binding
ref->display(); // late binding or dynamic binding
return 0 ;
}
An error keeps coming
"undefined reference to vtable for klm "
any ideas ?
Seeing as you did not define display as a pure virtual function it is looking for the body of display in the klm class and isn't finding it.
You will want to change line 16 to virtualvoid display()=0;
which will make it a pure virtual function.
What this basically means is that the classes that inherit from klm will be able to create their own function based off of the display function but it can not be used directly in that class. (Or you could just add a blank body for it in the klm class but that's a waste).
thanks guys !!
well , I made the changes in the code and it worked .
It made some things clear that either a virtual function must have a body(can be a blank one too) and redefined in child class OR it must be declared as pure virtual function and defined in the child class later .