Program to illustrates Virtual Function Empty in the Base Class


#include <iostream.h>
#include <string.h>
/*----------------------Class Interfaces-------------------------*/
class vehicle                                 // base class
{
    protected :
        char make[20];
        int milage;
    public :
        vehicle(char*, int);

        virtual void display() { }        // empty virtual function
};
class two_wheeler : public vehicle
{
        char is_auto_clutch;
    public :
        two_wheeler(char*, int, char);

        void display();
};
class four_wheeler : public vehicle
{
        char is_ac;
    public :
        four_wheeler(char*, int, char);

        void display();
};
/*----------------------Class Implementations---------------------*/
// member function definitions for base class 'vehicle'
vehicle::vehicle(char *a, int b)
{
    strcpy(make, a);
    milage = b;
}
// member function definitions for base class 'two_wheeler'
two_wheeler::two_wheeler(char *a, int b, char c):vehicle(a,b)
{
    is_auto_clutch = c;
}
void two_wheeler::display()
{
    cout << "Two-wheeler Details - \n";
    cout << "Vehicle Make : " << make << "\n";
    cout << "Milage : " << milage << "\n";
    cout << "Is Auto Clutch available : " << is_auto_clutch << "\n";
}
// member function definitions for base class 'four_wheeler'
four_wheeler::four_wheeler(char *a, int b, char c):vehicle(a,b)
{
    is_ac = c;
}
void four_wheeler::display()
{
    cout << "Four-wheeler Details - \n";
    cout << "Vehicle Make : " << make << "\n";
    cout << "Milage : " << milage << "\n";
    cout << "Is A/C available : " << is_ac << "\n";
}
/*----------------------Class definitions ends here---------------*/
void main(void)
{
    vehicle *base;        // base class pointer

    two_wheeler bike("Hero Honda",60,'N');
    base = &bike;
    base->display();
    cout << "\n";

    four_wheeler car("Maruti",18,'Y');
    base = &car;
    base->display();
}

0 comments:

Post a Comment

 
 
 
 


Copyright © 2012 http://codeprecisely.blogspot.com. All rights reserved |Term of Use and Policies|