LAB8 Runtime Polymorphism
LAB8 Runtime Polymorphism
Name: abdurahman
roll no 21
Semester: 2nd
#include <iostream>
#include <cmath>
int main() {
Shape* shapes[10];
// Clean up
for (int i = 0; i < 10; ++i) {
delete shapes[i];
}
return 0;
}
Explanation:
Base Class Shape: Defines virtual functions for area and perimeter, making it an ab-
stract class.
Derived Class Shape2D: Adds a virtual function for calculating paint cost based on
area.
Derived Class Shape3D: Adds a virtual function for calculating volume.
Square and Circle Classes: Implement area and perimeter functions.
Cube and Sphere Classes: Implement area, perimeter (circumference for sphere), and
volume functions.
Conclusion: This lab demonstrates the use of runtime polymorphism with virtual functions.
By using a base class pointer, we can handle different derived class objects uniformly, show-
ing polymorphic behavior.