0% found this document useful (0 votes)
40 views

Dara Lab 11

1. The document discusses two virtual function examples. The first creates shape classes Rectangle and Triangle derived from base class Shape with an overridden area function. Base pointers call derived functions. 2. The second creates animal classes Herbivore, Carnivore, Omnivore derived from base Animal with an overridden eat function taking a string. Base pointers call eat on each derived class object. 3. Virtual functions allow polymorphism so the correct overridden function is called based on the object type, avoiding ambiguity. Base pointers can access specific derived class functions using virtual functions and pointers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Dara Lab 11

1. The document discusses two virtual function examples. The first creates shape classes Rectangle and Triangle derived from base class Shape with an overridden area function. Base pointers call derived functions. 2. The second creates animal classes Herbivore, Carnivore, Omnivore derived from base Animal with an overridden eat function taking a string. Base pointers call eat on each derived class object. 3. Virtual functions allow polymorphism so the correct overridden function is called based on the object type, avoiding ambiguity. Base pointers can access specific derived class functions using virtual functions and pointers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

DATE: 28-NOV-23 STUDENT ID:S23BME007

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.
*********************************************************************************

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy