Virtual Functions


Create a class vehicle which stores the vehicleno and chassisno 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
{
            int vno;
            long int chno;
public:
            virtual void printvehiclename()
            {
                        cout<<"\n It is vehicle class";
            }
            void setvehicledetails()
            {
                        cout<<"\n Enter vehicle no and chassis no";
                        cin>> vno>>chno;
            }
            void printvehicledetails()
            {
                        cout<<vno<<" "<<chno;
            }
};
class scooter:public vehicle
{
            int nowheels;
            char company[20];
public:
            void printvehiclename()
            {
                        cout<<"\n---------- It is vehicle Scooter-------";
            }
            void set_scooter_details()
            {
                        cout<<"\n Enter no of wheels and compay name";
                        cin>>nowheels>>company;
            }
    void print_scooter_details()
            {
                        cout<<"\n No of wheels are "<<nowheels;
                        cout<<"\n Company name is"<< company;
            }

};
class car:public vehicle
{
            long int price;
            char company[20];
public:
            void printvehiclename()
            {
                        cout<<"\n -------It is vehicle Car----------";
            }
            void set_car_details()
            {
                        cout<<"\n Enter price and compay name";
                        cin>>price>>company;
            }
    void print_car_details()
            {
                        cout<<"Price is "<<price;
                        cout<<"\n Company name is"<< company;
            }

};
void main()
{
            vehicle *p;
            scooter s1,*p1;
            car c1,*p2;
            p=&s1;
            p1=&s1;
            p->printvehiclename();
            p->setvehicledetails();
            p->printvehicledetails();
            p1->set_scooter_details();
            p1->print_scooter_details();
            p=&c1;
            p2=&c1;
            p->printvehiclename();
            p->setvehicledetails();
            p->printvehicledetails();
            p2->set_car_details();
            p2->print_car_details();
           
}


Create a base class shape. Use this class to store two doubletype 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 anddisplay 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
{
             double l,w;
public:
            virtual void display_area()
            {
                         cout<<"This function display the area of specified shape";
            }
            void get_data()
            {
                        cout<<"\n Enter value of l and w";
                        cin>>l>>w;
            }
            int returnl()
            {
                        return(l);
            }
            int returnw()
            {
                        return(w);
            }
};
class rectangle:public shape
{
public:
            void display_area()
            {
                        cout<<"\n -----Area of Rectangle----------";
                        int area=returnl()*returnw();
                        cout<<"\n Area is"<<area;
                        cout<<"\n -------------------------------";
            }
};

class triangle:public shape
{
public:
            void display_area()
            {
                        cout<<"\n -----Area of Triangle----------";
                        int area=(returnl()*returnw())/2;
                        cout<<"\n Area is"<<area;
                        cout<<"\n -------------------------------";
            }
};

void main()
{
            shape *p;
            rectangle r;
            triangle t;
            r.get_data();
            p=&r;
            p->display_area();

            t.get_data();
            p=&t;
            p->display_area();
}


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



#include<iostream>
using namespace std;
class base
{
public:
            void virtual demo_purevirtual_fun()=0;
};
class der1: public base
{
public:
            void virtual demo_purevirtual_fun()
            {
                        cout<<"\n This is derived class 1";
            }
};
class der2: public base
{
public:
            void virtual demo_purevirtual_fun()
            {
                        cout<<"\n This is derived class 2";
            }
};
void main()
{
            base *p;
            der1 d1;
            der2 d2;
            p=&d1;
            p->demo_purevirtual_fun();
            p=&d2;
            p->demo_purevirtual_fun();
}



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<<"\n Enter Hour and Minutes";
                        cin>>hour>>minute;
            }
            void printtime()
            {
                        cout<<"\n"<<hour<<" : "<<minute;
            }
            void operator ++() //prefix
            {
                        minute=minute+1;
                        if(minute>=60)
                        {
                                    minute=minute-60;
                                    hour=hour+1;
                        }
                        hour =hour+1;
            }
            void operator ++(int) //postfix
            {
                        minute=minute+1;
                        if(minute>=60)
                        {
                                    minute=minute-60;
                                    hour=hour+1;
                        }
                        hour =hour+1;
            }
            void operator --() //prefix
            {
                        hour=hour-1;
                        if(minute>0)
                                    minute=minute-1;
                        else
                        {
                                    hour=hour-1;
                                    minute=59;
                        }
            }
            void operator --(int) //postfix
            {
                        hour=hour-1;
                        if(minute>0)
                                    minute=minute-1;
                        else
                        {
                                    hour=hour-1;
                                    minute=59;
                        }
            }
};
void main()
{
            time t1,t2;
            cout<<"\n \n Increment operator";

            t1.settime();
            t1++;
            t1.printtime();
            ++t1;
            t1.printtime();
            cout<<"\n \n  Decrement operator";
            t2.settime();
            t2--;
            t2.printtime();
            --t2;
            t2.printtime();

}