Understanding The Concept Of Virtual Function, Virtual Base Class And RTTI - LBM4

Recent Post

Tuesday 17 December 2019

Understanding The Concept Of Virtual Function, Virtual Base Class And RTTI

C++


Virtual Function


The overridden function in the derived class can be invoked by means of a base class pointer if the function is declared virtual in the base class. Suppose a virtual function get( ) is defined in the base class Base and again it is defined in the derived class Derived.

We can use the base class pointer to invoke the get( ) function of the derived class.

Derived d;
Base *b;
b=&d;
b-> get( ) //it calls the get ( ) function of the derived class.
Virtual Destructors

When a base class pointer that is pointing to a derived class object is deleted, destructor of the derived class as well as destructors of all its base classes is invoked, if the destructor in the base class is declared as virtual.

Virtual Base Class


In this type of inheritance there may be ambiguity in the members of the derived class child because it is derived from two base classes, which are again derived from the same base class. Hence to avoid this ambiguity the class G – parent can be made virtual.

Runtime Type Information (RTTI)

The runtime type information is one of the features of C++ that exhibit runtime polymorphic behavior. In C++ we can find the type information of an object at runtime and change the type of the object at runtime. The operators dynamic_cast and typeid are used for runtime type information.
For example if Animal is a polymorphic base class and Dog and Cat are derived classes of base class Animal then

Animal *anmp;
Dog dg;
Cat ct;
anmp=&dg;
cout<< typeid(*anmp).name();
displays the information of the object pointed by anm pointer
Similarly
Cat *cpt;
cpt=dynamic_cast<Cat*>(panm);

The down cast is successful if panm is holding the address of objects of class Cat.

Write a program to create a class shape with functions to find area of the shapes and display the name of the shape and other essential component of the class. Create derived classes circle, rectangle and trapezoid each having overridden functions area and display. Write a suitable program to illustrate virtual functions and virtual destructor.
  #include <iostream>
using namespace std;
const float PI=3.1415;

class shape{
private:
public:
    ~shape(){
    cout<<"Shape Destructor called"<<endl;
    };

    virtual void display_name(){
    cout<<"Shape name"<<endl;
    }
    virtual void area(){
    cout<<"Area of the shape"<<endl;
    }
};
class circle:public shape{
private:
    float radius;
public:
    circle(float r=0){
    radius=r;
    }
    ~circle(){
    cout<<"Circle Destructor called"<<endl;
    }
    void display_name(){
    cout<<"Circle"<<endl;
    }
    void area(){
    cout<<"Area of the circle:"<<PI*radius*radius<<endl;
    }
    };

class rectangle:public shape{
private:
    float length,breadth;
public:
    rectangle(float l=0,float b=0){
        length=l;
        breadth=b;
    }
    ~rectangle(){
    cout<<"Rectangle Destructor called"<<endl;
    }
     void display_name(){
    cout<<"Rectangle"<<endl;
    }
    void area(){
    cout<<"Area of the rectangle:"<<length*breadth<<endl;
    }
};
class trapezoid:public shape{
private:
    float parr1,parr2,height;
public:
    trapezoid(float p1=0,float p2=0,float h=0){
        parr1=p1;
        parr2=p2;
        height=h;
    }
    ~trapezoid(){
        cout<<"Trapezoid Destructor called"<<endl;
    }
     void display_name(){
    cout<<"Trapezoid"<<endl;
    }
    void area(){
    cout<<"Area of the trapezoid:"<<1.0/2*(parr1+parr2)*height<<endl;
    }
};
int main(){
shape S1,*ps[4];
circle C1(4),*pc;
rectangle R1(10,20),*pr;
trapezoid T1(22,54,34),*pt;
ps[0]=&S1;
ps[1]=&C1;
ps[2]=&R1;
ps[3]=&T1;
for(int i=0;i<4;i++){
    ps[i]->display_name();
    ps[i]->area();
}
return 0;
}
Create a class Person and two derived classes Employee, and Student, inherited from class Person. Now create a class Manager which is derived from two base classes Employee and Student. Show the use of the virtual base class.
#include <iostream>
using namespace std;
class Person{
protected:
    int id,age;
public:
    void getdata(){
    cout<<"Enter id and age:"<<endl;
    cin>>id>>age;
    }
    void showdata(){
    cout<<"Id:"<<id<<endl;
    cout<<"Age:"<<age<<endl;
    }
};
class Student:public Person{
private:
    float roll;
public:
    void getdata(){
    Person::getdata();
    cout<<"Enter roll:"<<endl;
    cin>>roll;
    }
    void showdata(){
    Person::showdata();
    cout<<"Roll:"<<roll<<endl;
    }
};
class Employee:public Person{
private:
    float salary;
public:
    void getdata(){
    Person::getdata();
    cout<<"Enter salary:"<<endl;
    cin>>salary;
    }
    void showdata(){
    Person::showdata();
    cout<<"Id:"<<id<<endl;
    cout<<"Salary:"<<salary<<endl;
    }
};
class Manager:public Student,public Employee{
private:
    float overtime;
public:
    void getdata(){
    Student::getdata();
    Employee::getdata();
    cout<<"Enter overtime:"<<endl;
    cin>>overtime;
    }
    void showdata(){
    Student::showdata();
    Employee::showdata();
    cout<<"Overtime hours:"<<overtime<<endl;
    }
};
int main(){
Manager M;
M.getdata();
M.showdata();

return 0;
}

Write a program with Student as abstract class and create derive classes Engineering, Medicine and Science from base class Student. Create the objects of the derived classes and process them and access them using array of pointer of type base class Student.
#include <iostream>
using namespace std;
class student{
private:
public:
    virtual void institutes()=0;
};

class Engineering:public student{
private:
public:
virtual void institutes(){
cout<<"Eng:\n1.Pulchowk\n2.KU\n3.Thapathali"<<endl;
}
};

class Medicine:public student{
private:
public:
virtual void institutes(){
cout<<"Med:\n1.IOM \n2.BPKIHS\n3.KUSMS"<<endl;
}
};
class Science:public student{
private:
public:
virtual void institutes(){
cout<<"Sci:\n1.ASCOL \n2.TU\n3.KU"<<endl;
}
};

int main(){
student *s[3];
Engineering E;
Medicine M;
Science S1;
s[0]=&E;
s[1]=&M;
s[2]=&S1;
for(int i=0;i<3;i++){
    s[i]->institutes();
}
return 0;
}

Create a polymorphic class Vehicle and create other derived classes Bus, Car and Bike from Vehicle. With this program illustrate RTTI by the use of dynamic_cast and typeid operators.
#include <iostream>
#include <typeinfo>
using namespace std;
class Vehicle{
protected:
    int id;
public:
    void get_id(){
    cin>>id;
    }
    void show_id(){
    cout<<"ID:"<<id<<endl;
    }
    virtual void compname(){
    cout<<"Company names:"<<endl;
    }
};
class Bus:public Vehicle{
private:
public:
    void compname(){
    cout<<"Company names:Eicher\nTata"<<endl;
    }
};
class Car:public Vehicle{
private:
public:
    void compname(){
    cout<<"Company names:Honda\nHyundai"<<endl;
    }
};
class Bike:public Vehicle{
private:
public:
    void compname(){
    cout<<"Company names:Bajaj\nMitsubishi\n"<<endl;
    }
};
int main(){
Vehicle *v[4],sam;
Car C;
Bus B;cout<<typeid(*v[3]).name()<<endl;
Bike Bi;
v[0]=&C;
v[1]=&sam;
v[2]=&B;
v[3]=&Bi;
for(int i=0;i<3;i++){
    cout<<typeid(*v[i]).name()<<endl;
}
v[3]=dynamic_cast<Vehicle *>(v[2]);
cout<<typeid(*v[3]).name()<<endl;
v[0]=dynamic_cast<Vehicle *>(v[1]);
cout<<typeid(*v[0]).name()<<endl;
return 0;
}

No comments:

Post a Comment