C++ Tutorial: Some Lab assignments on Inheritance

Question: Create a class called Musicians to contain three methods string ( ), wind ( ) and perc ( ).
Each of these methods should initialize a string array to contain the following instruments
–  veena, guitar, sitar, sarod and mandolin under string ( )
–  flute, clarinet saxophone, nadhaswaram and piccolo under wind ( )
–  tabla, mridangam, bangos, drums and tambour under perc ( )
It should also display the contents of the arrays that are initialized. Create a derived class called TypeIns
to contain a method called get ( ) and show ( ). The get ( ) method must display a  means as follows
Type of instruments to be displayed
a.    String instruments
b.    ind instruments
c.    Percussion instruments
The show ( ) method should display the relevant detail according to our choice. The base class variables must be accessible
only to its derived classes.

Solution:

#include<iostream>

#include<cstring>


using namespace std;


class Musicians

{

    protected:

        char instrument[15][15];

    public:

        void string()

        {

            strcpy(instrument[0], "Veena");

            strcpy(instrument[1], "Guitar");

            strcpy(instrument[2], "Sitar");

            strcpy(instrument[3], "Sarod");

            strcpy(instrument[4], "Mandolin");

            //cout<<instrument[0];

        }

        void wind()

        {

            strcpy(instrument[0], "Flute");

            strcpy(instrument[1], "Clarinet");

            strcpy(instrument[2], "Sexophone");

            strcpy(instrument[3], "Nadhaswaram");

            strcpy(instrument[4], "Picoolo");

        }

        void perc(void)

        {

            strcpy(instrument[0], "Tabla");

            strcpy(instrument[1], "Mridangam");

            strcpy(instrument[2], "Bangos");

            strcpy(instrument[3], "Drums");

            strcpy(instrument[4], "Tambour");

        }

        void show()

        {

            for(int i=0; i<5; i++)

            {

                cout<<instrument[i];

                cout<<" ";

            }

        }

};


class typIns : public Musicians

{

    public:

        void get()

        {

            cout<<"1. String Instrument"<<endl;

            cout<<"2. Ind Instrument"<<endl;

            cout<<"3. Percussion Instrument"<<endl;

        }

        void show(int choice)

        {

            switch(choice)

            {

                case 1:

                    string();

                    Musicians::show();

                    break;

                case 2:

                    wind();

                    Musicians::show();

                    break;

                case 3:

                    perc();

                    Musicians::show();

                    break;

            }

        }

};


int main()

{

    int x;

    typIns t;

    t.get();

    cout<<"Enter the Choice:";

    cin>>x;

    t.show(x);

    return 0;

}

 

Question: Write three derived classes inheriting functionality of base class person (should have a member function that ask to enter name and age) and with added unique features of student, and employee, and functionality to assign, change and delete records of student and employee. And make one member function for printing address of the objects of classes (base and derived) using this pointer. Create two objects of base class and derived classes each and print the addresses of individual objects. Using calculator, calculate the address space occupied by each object and verify this with address spaces printed by the program.

Solution:

#include<iostream>


using namespace std;


class person

{

    protected:

        char *name;

        int age;

    public:

        void getdata()

        {

            cout<<"Enter the name: ";

            cin>>name;

            cout<<"Enter the age: ";

            cin>>age;

        }

        void showdata()

        {

            cout<<"Name: "<<name<<endl;

            cout<<"Age: "<<age;


        }

        void print_address(void)

        {

            cout<<"\n The address of object is "<<this;

        }


};


class student: public person

{

    private:

        int ID;

    public:

        void getdata()

        {

            cout<<"Enter the information for student"<<endl;

            person::getdata();

            cout<<"Enter Student ID: ";

            cin>>ID;

        }

        void showdata()

        {

            cout<<"The information on student is"<<endl;

            person::showdata();

            cout<<"\nID : "<<ID;

        }

};


class employee: public person

{

        private:

            float salary;

        public:

            void getdata()

            {

                cout<<"Enter the information on employee "<<endl;

                person::getdata();

                cout<<"Enter Emplyee's salary: ";

                cin>>salary;

            }

            void showdata()

            {

                cout<<"\nThe information on Employee is ";

                person::showdata();

                cout<<"\nSalary: "<<salary<<endl;

            }


};



int main()

{

    student s;

    employee e;

    s.getdata();

    e.getdata();

    s.showdata();

    e.showdata();

    s.print_address();

    e.print_address();

    return 0;

}
Question:  Write base class that ask the user to enter a complex number and derived class adds the complex number of its own with the base. Finally make third class that is friend of derived and calculate the difference of base complex number and its own complex number.

 

Solution:
#include<iostream>

using namespace std;

class complex1

{

    protected:

        int real1, imag1;

    public:

        void get_data()

        {

            cout<<"Enter the complex number of base class: "<<endl;

            cout<<"Enter the real part: ";

            cin>>real1;

            cout<<"Enter the imaginary part: ";

            cin>>imag1;

        }

        void show_data()

        {

            cout<<real1<<" + i"<<imag1;

        }

};


class complex2: public complex1

{

    private:

        int real1, imag1;

    public:

        void get_data()

        {

            complex1::get_data();

            cout<<"\nEnter the complex number for derived class: "<<endl;

            cout<<"Enter real part: ";

            cin>>real1;

            cout<<"Enter imaginary part: ";

            cin>>imag1;

        }

        complex2 adds(void)

        {

            complex2 temp;

            temp.real1 = real1 + complex1::real1;

            temp.imag1 = imag1 + complex1::imag1;

            return temp;

        }

        void show_data()

        {

            cout<<real1<<" + j"<<imag1;

        }

        friend class complex3;

};


class complex3

{

    private:

        int real, imag;

    public:

        void get_data()

        {

            cout<<"\nEnter the complex for friend class: "<<endl;

            cout<<"Enter real part: ";

            cin>>real;

            cout<<"Enter imaginary part: ";

            cin>>imag;

        }

        complex3 subtract(complex2 c)

        {

            complex3 temp;

            temp.real = real - c.complex1::real1;

            temp.imag = imag - c.complex1::imag1;

            return temp;

        }

        void display()

        {

            cout<<real<<" + j"<<imag;

        }

};

int main()

{

    complex2 c,d;

    complex3 e, f;

    c.get_data();

    d = c.adds();

    cout<<"After addition the result is : "<<endl;

    d.show_data();

    cout<<endl;

    f.get_data();

    e = f.subtract(c);

    cout<<"After subtraction the result is : "<<endl;

    e.display();

    return 0;

}
SHARE C++ Tutorial: Some Lab assignments on Inheritance

You may also like...

1 Response

  1. Asma Khan says:

    In Question#2
    where is the code for …. " with added unique features of student, and employee, and functionality to assign, change and delete records of student and employee. " ???
    :/

Leave a Reply

Your email address will not be published.

Share