Lab 10 (FA20-BEE-224
Lab 10 (FA20-BEE-224
Lab report 10
Submitted by:-
Muhammad Abdullah
Registration No:-
FA20-BEE-224
Submitted to:-
Dr. Umar Khan
Dated: 12/05/2024
Lab Tasks:-
C++ program to calculate the area of a square and a circle using Abstract class and pure
Virtual function.
Program:-
#include <iostream> This class serves as the base class for various
shapes and defines an interface for calculating
// Abstract class Shape their areas.
class Shape { It contains a pure virtual function calculate
public: Area(), which means it has no implementation
// Pure virtual function for calculating area and must be overridden by derived classes.
virtual float calculateArea() = 0; Concrete Class Square:
};
Square is a subclass of Shape and represents a
// Concrete class Square inheriting from square shape.
Shape It includes a private member side, which
class Square : public Shape { denotes the length of one side of the square.
private: The calculateArea() function is overridden
float side; here to calculate the area of the square using
the formula: side * side.
public: Concrete Class Circle:
// Constructor
Square(float s) : side(s) {} Similar to Square, Circle is a subclass of
Shape and represents a circular shape.
// Implementation of calculateArea for It contains a private member radius, which
Square represents the radius of the circle.
float calculateArea() override { The calculateArea () function is overridden
return side * side; here to calculate the area of the circle using
} the formula: π * radius * radius.
}; main() Function:
// Concrete class Circle inheriting from Shape The main() function is where the program
class Circle : public Shape { execution begins.
private: It prompts the user to input the dimensions of
float radius; a square (side length) and a circle (radius).
Objects of the Square and Circle classes are
public: instantiated with the provided dimensions.
// Constructor The calculateArea() method of each object is
Circle(float r) : radius(r) {} called to compute its area.
Finally, the calculated areas of the square and
// Implementation of calculateArea for circle are displayed to the user.
Circle User Interaction:
float calculateArea() override {
return 3.14159 * radius * radius; The program interacts with the user by
} prompting for input values (side length for the
}; square and radius for the circle) using
standard input (cin).
int main() { It then displays the computed areas of the
float side, radius; square and circle to the user via standard
std::cout << "Enter the side of the square: output (cout).
";
std::cin >> side;
return 0;
}
Code:-
Output:-
Task:-2
Program:- Description:-
#include <iostream> An abstract class serving as the base class for
various shapes.
// Abstract class Shape Contains a pure virtual function calculateArea
class Shape { (), which is used to calculate the area of
public: different shapes.
// Pure virtual function for calculating area
virtual float calculateArea() = 0; Concrete Class Square:
};
Inherits from the Shape class.
// Concrete class Square inheriting from Shape Represents a square shape.
class Square : public Shape { Includes a private member side, representing
private: the length of one side of the square.
float side; Implements the calculateArea () function to
calculate the area of the square using the
public: formula: side * side.
// Constructor
Concrete Class Circle:
Square(float s) : side(s) {}
Also inherits from the Shape class.
// Implementation of calculateArea for Square
Represents a circular shape.
float calculateArea() override {
Contains a private member radius,
return side * side;
representing the radius of the circle.
}
Implements the calculateArea () function to
};
calculate the area of the circle using the
// Concrete class Circle inheriting from Shape
formula: π * radius * radius.
class Circle : public Shape { main() Function:
private:
float radius; The entry point of the program.
Prompts the user to input the dimensions of a
public: square (side length) and a circle (radius).
// Constructor Creates objects of the Square and Circle
Circle(float r) : radius(r) {} classes with the provided dimensions.
Calls the calculateArea () method of each
// Implementation of calculateArea for Circle object to compute its area.
float calculateArea() override { Displays the calculated areas of the square
return 3.14159 * radius * radius; and circle to the user.
}
};
int main() {
float side, radius;
std::cout << "Enter the side of the square: ";
std::cin >> side;
return 0;
}
Code:-
Output:-
Task 3:-
Program:- Description:-
#include <iostream> A new derived class Rectangle is introduced,
inheriting from the abstract class Shape.
// Abstract class Shape It adds its own data members length and
class Shape { width.
protected: Implements the calculateArea() function to
// Access specifier changed to 'protected' to calculate the area of the rectangle using the
allow derived classes access formula: length * width.
float area; // Data member to store the area Adds a member function displayDimensions()
to display the dimensions of the rectangle.
public: main() Function:
// Constructor
Shape() : area(0.0) {} Prompts the user to input the dimensions of a
rectangle (length and width).
// Function to display the area
void displayArea() { Creates an object of the Rectangle class with
std::cout << "Area: " << area << std::endl; the provided dimensions.
}
Calls the calculateArea() method of the
// Pure virtual function for calculating area Rectangle object to compute its area.
virtual void calculateArea() = 0; Demonstrates the usage of member functions
};
displayDimensions() and displayArea() to
display the dimensions and area of the
// Derived class Rectangle inheriting from Shape
rectangle.
class Rectangle : public Shape {
This extended version showcases that derived
private:
classes can indeed be created from abstract
float length;
classes, and they can access and utilize both
float width;
the data members and non-pure virtual
public:
member functions defined in the base class.
// Constructor
It further illustrates the flexibility and
Rectangle(float l, float w) : length(l), width(w) {} extensibility of the object-oriented
programming paradigm in C++.
// Implementation of calculateArea for
Rectangle
void calculateArea() override {
area = length * width;
}
int main() {
float length, width;
std::cout << "Enter the length of the rectangle:
";
std::cin >> length;
return 0;
}
Code:-
Output:-
Home Tasks
Choose three aspects / functionalities of your lab project and show the implementation
of Abstract classes and pure virtual functions.
Program:- Description:-
#include <iostream> The program defines an abstract base class
#include <vector> Shape, which contains a pure virtual function
#include <cmath> calculateArea(). Concrete classes Square,
Circle, and Triangle inherit from Shape and
// Abstract class Shape provide their implementations of
class Shape { calculateArea().
public:
// Pure virtual function for calculating area Polymorphism is utilized to achieve runtime
virtual float calculateArea() const = 0; polymorphic behavior. A vector shapes of
virtual ~Shape() {} // Virtual destructor to pointers to Shape objects is created. The
ensure proper cleanup calculateTotalArea() function calculates the
}; total area of all shapes in the vector by
iterating over each element and calling the
// Concrete class Square inheriting from calculateArea() function on each object,
Shape demonstrating dynamic dispatch.
class Square : public Shape {
private: Memory is managed dynamically by
float side; allocating memory for objects of concrete
shape classes using the new operator. Proper
public: memory cleanup is ensured by deleting
// Constructor dynamically allocated objects using the delete
Square(float s) : side(s) {} operator in the cleanup loop.
public:
// Constructor
Circle(float r) : radius(r) {}
public:
// Constructor
Triangle(float b, float h) : base(b),
height(h) {}
int main() {
// Create a vector of Shape pointers
std::vector<Shape*> shapes;
// Clean up memory
for (const auto* shape : shapes) {
delete shape;
}
return 0;
}
CODE:-
Output:-
Conclusion:-
In this lab we have learnt that all the program defines an abstract base class `Shape` with a pure
virtual function `calculateArea. Concrete classes `Square`, `Circle`, and `Triangle` inherit from
`Shape` and provide their implementations of `calculateArea().This inheritance structure allows
for a unified interface to handle different types of shapes. Polymorphism is utilized for runtime
behavior based on the actual object types. The program employs a vector of pointers to `Shape`
objects to demonstrate dynamic dispatch. The program displays the areas of individual shapes
and calculates the total area of all shapes, verifying the correctness of the area calculation
algorithms. The use of abstract classes and polymorphism promotes code reusability and
extensibility. Adding new shapes only requires implementing the `calculateArea()` function in a
new derived class.