Virtual functions with inline code substitution
Virtual functions could be declared as an inline code, being the run time binding of the computer. An inline code does not affect much of the programming effectiveness. A compiler must get information about the functions, such as from where they have to be invoked.
The common form is:
Class base
{
private:
//data;
public:
};
// syntax :virtual inline return_type function_name(argruments);
virtual inline return_type function_name(argruments);
// description of inline code along with virtual function
# include<iostream.h>
class base
{
private:
int x;
float y;
public:
virtual inline void getdata( );
virtual inline void display( );
};
class derivedB : public base
{
private:
int rollno;
char name[20];
public:
};
void getdata ( );
void display ( );
void base :: getdata ( )
{
cout <<"enter an integer \n";
cin>> x;
cout<<"enter a real number \n";
cin<< y;
}
void base :: display( )
{
cout<<" numbers x = "<< x
cout<< "and y = " << y;
cout<< endl;
}
void derivedB :: getdata ( )
{
cout <<"enter roll number \n";
cin >> rollno;
cout<< "enter name of a student \n";
cin>> name;
}
void derivedB :: display ( )
{
cout << "roll number student's name \n";
cout << roll no << '\t' << name << endl;
}
void main ( )
{
base *ptr;
derivedB obj;
ptr = &obj;
ptr-> getdata ( );
ptr -> display ( );
}
Output of the above program is:
Enter roll number
89001
enter name of a student ganapathy
rollnumber student's name
89001 ganapathy