0% found this document useful (0 votes)
29 views6 pages

LAB8 Runtime Polymorphism

Uploaded by

abdulwhy99
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)
29 views6 pages

LAB8 Runtime Polymorphism

Uploaded by

abdulwhy99
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/ 6

LAB#8: Runtime Polymorphism

Name: abdurahman

roll no 21

Semester: 2nd

Presentation/Procedure Code Comments Response Total


& conclusion
TASKS
 All the shapes share some features for example each have some area and perimeter. Every
shape has dimensions. Shapes can be further divided into 2D and 3D shapes. Area is the
property of 2D Shapes and volume is the additional property of 3D Shapes. Square and
circle are two derived classes of 2D Shapes. Cube and sphere are the 3D shapes. Let us
assume that only 2D shapes can be painted and the paint cost depends on area and can be
calculated if cost/unit area is provided. Provide a design in OOP of the scenario discussed
above and exercise polymorphism in main by creating 10 pointers to object array. Make
virtual functions where required

#include <iostream>
#include <cmath>

using namespace std;

// Base class Shape


class Shape {
public:
virtual double area() const = 0;
virtual double perimeter() const = 0;
virtual ~Shape() {}
};

// Derived class for 2D shapes


class Shape2D : public Shape {
public:
virtual double paintCost(double costPerUnitArea) const {
return area() * costPerUnitArea;
}
virtual ~Shape2D() {}
};

// Derived class for 3D shapes


class Shape3D : public Shape {
public:
virtual double volume() const = 0;
virtual ~Shape3D() {}
};

// Square class derived from Shape2D


class Square : public Shape2D {
private:
double side;
public:
Square(double s) : side(s) {}
double area() const override {
return side * side;
}
double perimeter() const override {
return 4 * side;
}
};

// Circle class derived from Shape2D


class Circle : public Shape2D {
private:
double radius;
public:
Circle(double r) : radius(r) {}
double area() const override {
return M_PI * radius * radius;
}
double perimeter() const override {
return 2 * M_PI * radius;
}
};

// Cube class derived from Shape3D


class Cube : public Shape3D {
private:
double side;
public:
Cube(double s) : side(s) {}
double area() const override {
return 6 * side * side;
}
double perimeter() const override {
return 12 * side;
}
double volume() const override {
return side * side * side;
}
};

// Sphere class derived from Shape3D


class Sphere : public Shape3D {
private:
double radius;
public:
Sphere(double r) : radius(r) {}
double area() const override {
return 4 * M_PI * radius * radius;
}
double perimeter() const override {
return 2 * M_PI * radius;
}
double volume() const override {
return (4.0/3) * M_PI * radius * radius * radius;
}
};

int main() {
Shape* shapes[10];

// Create objects of 2D and 3D shapes


shapes[0] = new Square(5);
shapes[1] = new Circle(3);
shapes[2] = new Cube(4);
shapes[3] = new Sphere(2);
shapes[4] = new Square(6);
shapes[5] = new Circle(4);
shapes[6] = new Cube(5);
shapes[7] = new Sphere(3);
shapes[8] = new Square(7);
shapes[9] = new Circle(5);

// Display area, perimeter and paint cost for 2D shapes


for (int i = 0; i < 10; ++i) {
if (Shape2D* s2d = dynamic_cast<Shape2D*>(shapes[i])) {
cout << "2D Shape:" << endl;
cout << "Area: " << s2d->area() << endl;
cout << "Perimeter: " << s2d->perimeter() << endl;
cout << "Paint Cost (cost/unit area = 10): " << s2d->paintCost(10) << endl;
} else if (Shape3D* s3d = dynamic_cast<Shape3D*>(shapes[i])) {
cout << "3D Shape:" << endl;
cout << "Area: " << s3d->area() << endl;
cout << "Perimeter: " << s3d->perimeter() << endl;
cout << "Volume: " << s3d->volume() << endl;
}
cout << "---------------------" << endl;
}

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

You might also like