Programs to illustrate implementation of virtual function when using strings

In this program, we are going to see the Implementation of Virtual function using Strings  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 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;
    }
};

int main()
{
    Employee emp;
    Person *P = &emp;
    P->GetName();
    emp.print();
    return 0;
}

//.......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