Polymorphism and Virtual Methods




This video tutorial introduces you to the concept of polymorphism and virtual functions in c++.
You will learn what is polymorphism, what are virtual functions, what is the use of virtual functions, how to use virtual functions to implement polymorphism in object oriented programming in detail with example.

source code for this tutorial

#include <iostream>

using namespace std;

class Person{
public:
    virtual void introduce(){
    cout <<"hey from person"<<endl;
    }
};

class Student : public Person{
public:
    void introduce(){
    cout <<"hey from student"<<endl;
    }
};

class Farmer : public Person{
public:
    void introduce(){
    cout <<"hey from Farmer"<<endl;
    }
};

void whosThis(Person &p){
p.introduce();
}

int main()
{
    Farmer anil;
    Student alex;

    whosThis(anil);
    whosThis(alex);
    return 0;
}