Virtual function in C++.

Virtual function: Virtual functions, one of advanced features of Oop is one that does not really exist but it appears real in some parts of a program.

To make a member function virtual, the keyword virtual is used in the methods while it is declared in the class definition but not in the member function definition.

 

 

Let’s write the following code to print detail of student using virtual function.

 

 


#include<iostream.h>

#include<conio.h>

    class bca

{

    protected:

    char  name[20];

    char  clas[20];

    public:

    virtual void input()=0;

    virtual void display()=0;

};

    class mca:public bca

{

    protected :

    long int   rol;

    public:

    void input();

{
cout<<”enter your name:”;

    cin>>name;

    cout<<”enter your class:”;

    cin>>clas;

    cout<<”enter your roll number:”;

    cin>>rol;

}

    void display()

{

    cout<<”my name is:”<<name<<”\n my class is :”<<clas<<”\n my roll number is:”<<rol;

}

};

    void main()

{

    clrscr();

    mca a;

    a.input();

    a.display();

    getch();

}

 

 

 

Now run the program and see output:

 

 

Output:

 

output

 

Happy coding.

 

 

Tags: , , ,