Dara Lab 11
Dara Lab 11
LAB #11
VIRTUAL FUNCTIONS
1) (a) Create a class Shape which consist of virtual function area, derived the classes Rectangle
and Triangle from class Shape and redefine area function inside derived classes. Using base
class pointer to call derived classes functions.
SOURCE CODE:
#include<iostream>
using namespace std;
class Shape{
public:
double a,b;
virtual void tool(int a, int b){
cout<<"This is a shape class "<<endl;
}
};
class Rectangle:public Shape{
public:
void tool(int a, int b){
cout<<"Area of Rectangle is: "<<a*b<<endl;
}
};
class Triangle:public Shape{
public:
void tool(int a, int b){
cout<<"Area of Triangle is:"<<0.5*a*b<<endl;
}
};
int main (){
Shape *ptrShape1, *ptrShape2;
Rectangle rec;
Triangle tr;
ptrShape1 = &rec;
ptrShape1 -> tool(4,5);
ptrShape2 = &tr;
ptrShape2 -> tool(4,5);
return 0;
}
OUTPUT:
Area of Rectangle is: 20
Area of Triangle is:10
--------------------------------
(b) Consider a class named Animal having typical function name eat (virtual) that accepts a
parameter of type string. Three classes i.e. Herbivore, Carnivore and Omnivore have been
inherited from Animal class. All these classes i.e. Herbivore, Carnivore and Omnivore must
have their own (overridden) eat function. Write a main function to invoke derived class
member functions by base class pointer to derived class object.
SOURCE CODE:
#include<iostream>
using namespace std;
class Animal{
public:
virtual void eat (string food){
cout<<" An Animal Eats" << food<<endl;
}
};
class Herbivore:public Animal{
public:
void (string food){
cout<<" Herbivores Animals eats " << food<<endl;
}
};
class Carnivore:public Animal{
public:
void (string food){
cout<<" Carnivore Animals eats " << food<<endl;
}
};
class Omnivore:public Animal{
public:
void (string food){
cout<<" Omnivore Animals eats " << food<<endl;
}
};
int main(){
Animal *ptr,ani;
ptr=&a;
ptr->eat("Grass and Meat");
Herbivore h;
ptr=&h;
ptr->eat("Grass only");
Carnivore c;
ptr=&c;
ptr->eat("Meat only");
Omnivore o;
ptr=&o;
ptr->eat("Both Grass and Meat");
}
OUTPUT:
An Animal Eats Grass and Meat
Herbivores Animals eats Grass only
Carnivore Animals eats Meat only
Omnivore Animals eats Both Grass and Meat
CONCLUSION:
In this lab we studied virtual functions in this lab, and we discovered that they represent a kind of
run-time polymorphism. When using a member function with the same name in both the base class
and derived classes under multiple inheritance, the compiler may become confused and unable to
determine which specific class's function to access in the main function. The overloaded function in
the base class can be written with the term "virtual" to solve this difficulty, which is also known as
the "diamond problem." The address of the object of any derived class can be found at the value of a
pointer of the base class that is formed. Pointer and virtual keywords can be used to access a specific
function of any derived class.
*********************************************************************************