• Click below to download Unit 3 programs in pdf file: -




Unit 3



/* 1. Create a class vehicle which stores the vehicle no and chassis no as a member. Define another class for scooter, which inherits the data members of the class vehicle and has a data member for a storing wheels and company. Define another class for which inherits the data member of the class vehicle and has a data member for storing price and company. Display the data from derived class. Use virtual function. */


#include<iostream>

using namespace std;


class vehicle

{

public:

    int vehicleno;

    int chassisno;

    virtual void get()

    {

            cout<<"\n\--vehicle--\n";

            cout<<"\nEnter the vehicle no:";

            cin>>vehicleno;

            cout<<"\nEnter chassis no:";

            cin>>chassisno;

    }

    virtual void show()

    {

        cout<<"\nValues of vehicle:"<<endl;

        cout<<"\n Vehicle No:"<<vehicleno<<endl<<"\n Chassis No:"<<chassisno<<endl;

    }

};

/**scooter**/

class scooter:public vehicle

{

private:

public:

    int wheels;

    string company;

    virtual void get()

    {

        cout<<"\n--scooter--\n";

        cout<<"\nEnter the wheels:";

        cin>>wheels;

        cout<<"\nEnter company:";

        cin>>company;

    }

    virtual void show()

    {

        cout<<"\nValues of Scooter:"<<endl;

        cout<<"\n Wheels:"<<wheels<<endl<<"\n Company:"<<company<<endl;

    }

};


class bike:public vehicle

{

private:

public:

    int price;

    string company;

    virtual void get()

    {

        cout<<"\n\--bike--\n";

        cout<<"\nEnter the price:";

        cin>>price;

        cout<<"\nEnter company:";

        cin>>company;

    }

    virtual void show()

    {

        cout<<"\nValues of Bike:"<<endl;

        cout<<"\n Price:"<<price<<endl<<"\n company:"<<company<<endl;

    }

};

int main()

{

    vehicle *v;

    vehicle v1;

    v=&v1;

    v->get();

    v->show();

    scooter s;

    v=&s;

    v->get();

    v->show();

    bike b;

    v=&b;

    v->get();

    v->show();

}


/* 2. Create a base class shape. Use this class to store two double type values that could be used to compute the area of figures.Derive two specific classes called triangle and rectangle from the base shape. Add to the base class, a member function get_data() to initialize the base class data members and another member function display_area() to compute and display the area of figures. Make display_area() as a virtual function and redefine this function in the derived class to suit their requirements. */



#include<iostream>

using namespace std;

class shape

{

public:

    double a1,a2;

    get_data()

    {

        cout<<"Enter the hight/length:";

        cin>>a1;

        cout<<"Enter the value of base/Width:";

        cin>>a2;

    }

    virtual void display_area()=0;

};

class triangle:public shape

{

    double A;

public:

    void display_area()

    {

        cout<<"\nTriangle:"<<endl;

        cout<<"hight/length:"<<a1<<endl<<"base/Width:"<<a2;

        A=a1*a2;

        A=A/2;

        cout<<"\nArea of triangle is:"<<A<<endl;

    }

};

class rectangle:public shape

{

    double A;

public:

   void display_area()

    {

        cout<<"\nRectangle:"<<endl;

        cout<<"hight/length:"<<a1<<endl<<"base/Width:"<<a2;

        A=a1*a2;

        cout<<"\nArea of rectangle is:"<<A<<endl;

    }

};

int main()

{

    shape *s;

    triangle t;

    s=&t;

    cout<<"Triangle:"<<endl;

    s->get_data();

    s->display_area();

    rectangle r;

    s=&r;

    cout<<"Rectangle:"<<endl;

    s->get_data();

    s->display_area();

}




/* 3 Write a program to demonstrate the use of pure virtual function. */


#include<iostream>

using namespace std;

class A

{

public:

    virtual void display()=0;

};

class B:public A

{

public:

    void display()

    {

        cout<<"BCA4GU";

    }

};

int main()

{

    A *ap;

    B b;

    ap=&b;

    ap->display();

}




/* 4 Create a class time with member data hour and minute. Overload ++ unary operator for class time for increment and -- unary operator for decrement in time object value. */


#include<iostream>

using namespace std;


class time

{

    int hour;

    int minute;

public:

void settime()

{

            cout<<"\nEnter Hour and Minutes : ";

            cin>>hour>>minute;

}


void printtime()

{

            cout<<"\n"<<hour<<" : "<<minute;

}


void operator ++()

{

    minute=minute+1;

    if(minute>=60)

    {

                minute=minute-60;

                hour=hour+1;

    }

    hour =hour+1;

}


void operator ++(int)

{

    minute=minute+1;

    if(minute>=60)

    {

                minute=minute-60;

                hour=hour+1;

    }

    hour =hour+1;

}


void operator --()

{

    hour=hour-1;

    if(minute>0)

                minute=minute-1;

    else

    {

                hour=hour-1;

                minute=59;

    }

}

    void operator --(int)

    {

        hour=hour-1;

        if(minute>0)

                    minute=minute-1;

        else

        {

                    hour=hour-1;

                    minute=59;

        }

    }

};


int main()

{

    time t1,t2;

    cout<<"Increment operator";


    t1.settime();

    t1++;

    t1.printtime();

    ++t1;

    t1.printtime();


    cout<<"\nDecrement operator";


    t2.settime();

    t2--;

    t2.printtime();

    --t2;

    t2.printtime();

}



/* 5 Create a class string with character array as a data member and write a program to add two strings with use of operator overloading concept. */


#include<iostream>

#include<string.h>

using namespace std;

class String

{

char s[50];

public:

    void get_string()

    {

        cin>>s;

    }

    void disp_string()

    {

        cout<<s;

    }

    String operator+(String a)

    {

        String n;

        strcat(s," ");

        strcat(s,a.s);

        strcpy(n.s,s);

        return n;

    }

};

int main()

{

    String s1,s2,s3,s4;


    cout<<"Enter your name : " ;s1.get_string();

    cout<<"Enter your surname : ";s2.get_string();


    s3=s1+s2;


    cout<<"Your full name is : ";s3.disp_string();

}



/* 6 Create a class distance which contains feet and inch as a data member. Overhead = =, <and> operator for the same class. Create necessary functions and constructors too. */


#include<iostream>

using namespace std;


class Distance

{

    float feet,inch;

public:

    void get()

    {

        cout<<"Enter the Feet:";

        cin>>feet;

        cout<<"Enter the Inch:";

        cin>>inch;

    }

    void disp()

    {

        cout<<"Enter the Feet:"<<feet;

        cout<<"Enter the Inch:"<<inch;

    }

    void operator == (Distance &s1)

    {

        cout<<feet<<endl<<s1.feet<<endl;

        if(feet==s1.feet && inch==s1.inch)

        cout<<"Both DISTANCE  SAME.";

        else

        cout<<"DISTANCE ARE NOT SAME.";

    }

};


int main()

{

    float a,b;

    Distance o1,o2;

    cout<<"FIRST DISTANCE:\n";o1.get();

    cout<<"SECOND DISTANCE:\n";o2.get();

    o1==o2;

}




/* 7 Create a class MATRIX of size mxn. Overload + and – operators for addition and subtraction of the MATRIX. */


#include<iostream>

using namespace std;


class matrix

{

    int r,c,m[30][30];

    int i,j;

public:

    matrix()

    {


    }

    matrix(int a,int b)

    {

        r=a;

        c=b;

    }

    void get();

    void disp();


    matrix operator +(matrix );

    matrix operator -(matrix);

};

void matrix::get()

{

    for(i=0;i<r;i++)

    {

        for(j=0;j<c;j++)

        {

            cout<<'['<<i<<','<<j<<"]:";

            cin>>m[i][j];

        }

    }

}

void matrix::disp()

{

    for(i=0;i<r;i++)

    {

        cout<<endl;

        for(j=0;j<c;j++)

        {

            cout<<m[i][j]<<"\t";

        }

    }

}

matrix matrix::operator +(matrix k)

{

    matrix add(r,c);

    for(i=0;i<r;i++)

    {

        for(j=0;j<c;j++)

        {

            add.m[i][j]=m[i][j]+k.m[i][j];

        }

    }

    return add;

}

matrix matrix::operator -(matrix k)

{

    matrix sub(r,c);

    for(i=0;i<r;i++)

    {

        for(j=0;j<c;j++)

        {

            sub.m[i][j]=m[i][j]-k.m[i][j];

        }

    }

    return sub;

}

int main()

{

    int a,b;

    cout<<"Enter the ROW SIZE of Matrix   :";

    cin>>a;

    cout<<"Enter the COLUMN SIZE of Matrix:";

    cin>>b;

    matrix o1(a,b);

    matrix o2(a,b);

    cout<<"Enter the Number of MATRIX A:\n";o1.get();

    cout<<"Enter the Number of MATRIX B:\n";o2.get();

    matrix o3;

    o3=o1+o2;

    cout<<"MATRIX [A+B]:";o3.disp();

    o3=o1-o2;

    cout<<"\nMATRIX [A-B]:";o3.disp();

}




/* 8 Define a class Coord, which has x and y coordinates as its data members. Overload ++ and -- operators for the Coord class. Create both its prefix and postfix forms */


#include<iostream>

using namespace std;

class coord

{

private:

    int x,y;

public:

    void getdata(int a,int b)

    {

        x=a;

        y=b;

    }

    void display()

    {

        cout<<"X="<<x<<endl;

        cout<<"Y="<<y<<endl;

    }

    coord operator ++();

    coord operator --();

};

coord coord::operator ++()

{

    coord c;

    c.x=++x;

    c.y=++y;

    return c;

}

coord coord::operator --()

{

    coord c;

    c.x=x--;

    c.y=y--;

    return c;

}

int main()

{

    coord c1,c2;

    c1.getdata(6,7);

    c2=++c1;

    cout<<"Prefix expression:"<<endl;

    c1.display();

    c2=--c1;

    c2=--c1;

    cout<<"Postfix expression:"<<endl;

    c1.display();

}




/* 9 Create one class called Rupees, which has one member data to store amount in rupee and create another class called Paise which has member data to store amount in paise. Write a program to convert one amount to another amount with use of type conversion. */


#include<iostream>

using namespace std;

class paisa;

class rupee

{

    int r;

public:

    rupee()

    {


    }

    void set()

    {

        cout<<"ENTER THE RUPEES :";cin>>r;

    }

    void disp()

    {

        cout<<"CONVERT TO RUPEES:"<<r;

    }

    int get_r()

    {

        return r;

    }

    rupee(paisa p1);

};

class paisa

{

    int p;

public:

    paisa()

    {


    }

    paisa(rupee r1)

    {

        p=r1.get_r()*100;

    }

    void set()

    {

        cout<<"\nENTER THE PAISA:";cin>>p;

    }

    void disp()

    {

        cout<<"CONVERT TO PAISA:"<<p;

    }

    int get_p()

    {

        return p;

    }

};


rupee::rupee(paisa p1)

{

    r=p1.get_p()/100;

}

int main()

{

        rupee r1;

        paisa p1;

        r1.set();

        p1=r1;

        p1.disp();

        p1.set();

        r1=p1;

        r1.disp();

}



/* 10 Create two classes Celsius and Fahrenheit to store temperature in terms of Celsius and Fahrenheit respectively. Include necessary functions to read and display the values. Define conversion mechanism to convert Celsius object to Fahrenheit object and vice versa. Show both types of conversions in main function. */

#include<iostream>

using namespace std;

class celsius;

class fehrenheit

{

    int f;

public:

    fehrenheit()

    {


    }

    void set()

    {

        cout<<"ENTER THE FEHRENHEIT:";

        cin>>f;

    }

    void disp()

    {

        cout<<"CONVERT TO FEHRENHEIT:"<<f;

    }

    int get_f()

    {

        return f;

    }

    fehrenheit(celsius c1);

   /* {

         f=(c1.get_c()*1.8)+32;

    }*/

};

class celsius:public fehrenheit

{

    int c;

public:

    celsius()

    {


    }

    celsius(fehrenheit f1)

    {

        c=(f1.get_f()-32)/1.8;

    }

    void set()

    {

        cout<<"\nENTER THE CELSIUS:";cin>>c;


    }

    void disp()

    {

        cout<<"CONVERT TO CELSIUS:"<<c;

    }

    int get_c()

    {

        return c;

    }

};

fehrenheit::fehrenheit(celsius c1)

{

    f=(c1.get_c()*1.8)+32;

}

int main()

{

    fehrenheit f1;

    celsius c1;


    f1.set();

    c1=f1;

    c1.disp();


    c1.set();

    f1=c1;

    f1.disp();


    return 0;

}