Skip to main content

Virtual Function in C++

 



#include<bits/stdc++.h>

using namespace std;
class Aakash{
    protected:
    string Name;
    int roll;
    public:
    Aakash(string s, int r){
        Name = s;
        roll = r;
    }
  virtual void display()=0;      // abstract base class mean pure virtual class and pure virtual class means
                                    //   minimum one funtion of base class should be virtual
};
class AK1: public Aakash{
    int plan;
    public:
    AK1(string s, int r , int p): Aakash(s,r){
        plan=p;
    }
    void display(){
        cout<<"The Name is: "<<Name<<endl;
        cout<<"The Roll No is: "<<roll<<endl;
        cout<<"The No's of Plan is: "<<plan<<endl<<endl;
    }
};
class AK2: public Aakash{
    int plan2;
    public:
    AK2(string s, int r , int p2): Aakash(s,r){
        plan2=p2;
    }
    void display(){
        cout<<"The Name is: "<<Name<<endl;
        cout<<"The Roll No is: "<<roll<<endl;
        cout<<"The No's of Plan is: "<<plan2<<endl;
    }
};
int main()
{   string Name;
    int roll;
    int plan,plan2;
    Name = "Aakash";
    roll = 4025;
    plan = 24;
    plan = 24;
    AK1 vk(Name,roll,plan);
   // vk.display();


     Name = "Karan";
    roll = 4042;
    plan = 25;
    plan = 24;
    AK2 kv(Name,roll,plan);
   // kv.display();

    Aakash * ok[2];
    ok[0] = &vk;
    ok[1] = &kv;
     ok[0]->display();
     ok[1]->display();
   
    return 0;
}


Comments