Basic program to show virtual function overriding
In this we are going see a basic program on Virtual Function Overriding in C++ Programming Language.



The Code given below can be used in TURBO C++ Compilers: -

#include <iostream.h>
#include <conio.h>

class Person
{
public:
    virtual void GetName()
    {
       << cout  "Person Name";
    }
};

class Employee : public Person
{
public:
    char name[50];
    void GetName()
    {
        cout << "Enter Employee Name: ";
        cin >> name;
    }
    void print()
    {
        cout << "Employee's Name: " << name << endl;
    }
};

void main()
{
    clrscr();
    Employee emp;
    Person *P = &emp;
    P->GetName();
    emp.print();
    getch();
}

//.......Coded by SHREYA IDATE



The Code given below can be used in gcc/g++ Compilers: -

#include <iostream>
using namespace std;

class base
{
    public:
    virtual void print()
    {
        cout <<"print() of base class" << endl;
    }
};

class derived : public base
{
    public:
    void print()
    {
        cout << "print() of derived class" << endl;
    }
};

int main()
{
    derived obj;
    base *obj2 = &obj;
    obj2->print();
}

//.......Coded by SHREYA IDATE



#ENJOY CODING

Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post