Inheritance-Virtual Functions Example

 #include <iostream>

using namespace std;


class CWH{

protected:

float rating;

public:

CWH(float r){

cout<<"Hello"<<endl<<endl;

rating=r;

}

virtual void display(void)

{

    cout<<"Harry bhaiya beautiful"<<endl;

}

};


class CWHvideo:public CWH{

    protected:

    string name;

    float length;

    int views;

    public:

    CWHvideo(string n,float l,int v,float r):CWH(r){

       name=n;

       length=l;

       views=v;

    }

    void display(void)

    {

        cout<<"Name of our video channel is "<<name<<endl

        <<"Length of the video is "<<length<<endl<<

        "Total views on the video is "<<views<<endl<<

        "Total rating of this video channel is "<<rating<<endl<<endl;

    }

};


class CWHtext:public CWH{

    protected:

    string name;

    int words;

    int views;

    public:

    CWHtext(string n,int w,int v,float r):CWH(r){

       name=n;

       words=w;

       views=v;

    }

    void display(void)

    {

        cout<<"Name of our text channel is "<<name<<endl

        <<"Number of words in the text is "<<words<<endl<<

        "Total views on the text is "<<views<<endl<<

        "Total rating of this text channel is "<<rating<<endl<<endl;

    }

};


int main() {


CWHvideo derived_video("Code with Harry",5.46,9542,4.67);

CWHtext derived_text("Code with Harry",435,12365,4.85);

derived_video.display();

derived_text.display();

CWH *ptr_1;

CWH *ptr_2;

ptr_1=&derived_video;

ptr_2=&derived_text;

ptr_1->display();

ptr_2->display();

return 0;

}


Comments

Popular posts from this blog

Hackerrank-Accessing Inherited Functions problem-Inheritance

Inheritance- Code with Harry-Lecture 41 solution (Multiple inheritance)