Wednesday, November 2, 2022

Inheritance simple,multi level and code reusability

//Simple inheritance

#include <iostream>

using namespace std;

//base class

class Student

{

    protected:

        int sno;

        char sname[20];

};

// derived clasds

class Mpc:public Student

{

    private:

        int maths,phy,che;

    public:

        void get()

        {

            cout<<"Enter no and name?";

            cin>>sno>>sname;

            cout<<"Enter Maths,phy & che marks?";

            cin>>maths>>phy>>che;

        }

        void dis(){

            cout<<sno<<"\t\t"<<sname<<endl;

            cout<<maths<<"\t\t"<<phy<<"\t\t"<<che<<"\n";

        }

            

    

};


int main()

{

    Mpc obj;

    cout<<sizeof(obj)<<endl;

    obj.get();

    obj.dis();

    return 0;

}





// multi level inheritance

#include <iostream>

using namespace std;

//base class

class Student

{

    protected:

        int sno;

        char sname[20];

};

// derived class

class Lang:public Student{

    protected:

        int eng,tel;

};

// derived clasds

class Mpc:public Lang

{

    private:

        int maths,phy,che;

    public:

        void get()

        {

            cout<<"Enter no and name?";

            cin>>sno>>sname;

            cout<<"Enter eng and tel marks?";

            cin>>eng>>tel;

            cout<<"Enter Maths,phy & che marks?";

            cin>>maths>>phy>>che;

        }

        void dis(){

            cout<<sno<<"\t\t"<<sname<<endl;

            cout<<eng<<"\t\t"<<tel<<"\n";

            cout<<maths<<"\t\t"<<phy<<"\t\t"<<che<<"\n";

        }

};


int main()

{

    Mpc obj;

    cout<<sizeof(obj)<<endl;

    obj.get();

    obj.dis();

    return 0;

}


output :

Enter no and name?101 Scott

Enter eng and tel marks?66 77

Enter Maths,phy & che marks?88 99 90

101             Scott

66              77

88              99              90


-------------------------

// multi level multi line inheritance

#include <iostream>

using namespace std;

//base class

class Student

{

    protected:

        int sno;

        char sname[20];

};

// derived class

class Lang:public Student{

    protected:

        int eng,tel;

};

//derived class

class Pc:public Lang{

    protected:

        int phy,che;

};

// derived clasds

class Mpc:public Pc

{

    private:

        int maths;

    public:

        void get()

        {

            cout<<"Enter no and name?";

            cin>>sno>>sname;

            cout<<"Enter eng and tel marks?";

            cin>>eng>>tel;

            cout<<"Enter Maths,phy & che marks?";

            cin>>maths>>phy>>che;

        }

        void dis(){

            cout<<sno<<"\t\t"<<sname<<endl;

            cout<<eng<<"\t\t"<<tel<<"\n";

            cout<<maths<<"\t\t"<<phy<<"\t\t"<<che<<"\n";

        }

};

// derived clasds

class Bpc:public Pc

{

    private:

        int bot,zoo,phy,che;

    public:

        void get()

        {

            cout<<"Enter no and name?";

            cin>>sno>>sname;

            cout<<"Enter eng and tel marks?";

            cin>>eng>>tel;

            cout<<"Enter phy,che,bot & zoo marks?";

            cin>>phy>>che>>bot>>zoo;

        }

        void dis(){

            cout<<sno<<"\t\t"<<sname<<endl;

            cout<<eng<<"\t\t"<<tel<<"\n";

            cout<<bot<<"\t\t"<<phy<<"\t\t"<<che<<"\t\t"<<zoo<<"\n";

        }

};

int main()

{

    Mpc m;

    m.get();

    m.dis();

    Bpc b;

    b.get();

    b.dis();

    return 0;

}


Enter no and name?101 Babe

Enter eng and tel marks?45 56

Enter Maths,phy & che marks?56 67 78

101             Babe

45              56

56              67              78

Enter no and name?102 Scott

Enter eng and tel marks?78 76

Enter phy,che,bot & zoo marks?56 56 56 56

102             Scott

78              76

56              56              56              56


---------------------------------------------------------

// concept of reusability of code

#include <iostream>

using namespace std;

//base class

class Student

{

    protected:

        int sno;

        char sname[20];

    public:

        void get(){

            cout<<"Enter no and name?";

            cin>>sno>>sname;

        }

        void dis(){

            cout<<sno<<"\t\t"<<sname<<endl;

        }

};

// derived class

class Lang:public Student{

    protected:

        int eng,tel;

    public:

        void get(){

            Student::get();

            cout<<"Enter eng and tel marks?";

            cin>>eng>>tel;

        }

        void dis(){

            Student::dis();

            cout<<eng<<"\t\t"<<tel<<"\n";

        }

};

//derived class

class Pc:public Lang{

    protected:

        int phy,che;

    public:

        void get(){

            Lang::get();

            cout<<"Enter Phy and che marks?";

            cin>>phy>>che;

        }

        void dis(){

            Lang::dis();

            cout<<phy<<"\t\t"<<che<<"\n";

        }

};

// derived clasds

class Mpc:public Pc

{

    private:

        int maths;

    public:

        void get()

        {

            Pc::get();

            cout<<"Enter Maths marks?";

            cin>>maths;

        }

        void dis(){

            Pc::dis();

            cout<<maths<<"\n";

        }

};

// derived clasds

class Bpc:public Pc

{

    private:

        int bot,zoo;

    public:

        void get()

        {

            Pc::get();

            cout<<"Enter bot & zoo marks?";

            cin>>bot>>zoo;

        }

        void dis(){

            Pc::dis();

            cout<<bot<<"\t\t"<<zoo<<"\n";

        }

};

int main()

{

    Mpc m;

    m.get();

    m.dis();

    Bpc b;

    b.get();

    b.dis();

   

    return 0;

}


output:

Enter no and name?101 Babe

Enter eng and tel marks?55 55

Enter Phy and che marks?66 66

Enter Maths marks?78

101             Babe

55              55

66              66

78

Enter no and name?102 Tiger

Enter eng and tel marks?67 78

Enter Phy and che marks?78 89

Enter bot & zoo marks?78 78

102             Tiger

67              78

78              89

78              78