0% found this document useful (0 votes)
20 views14 pages

Lab 10 (FA20-BEE-224

The lab report discusses the implementation of abstract classes in C++ to calculate the area of different shapes, including squares, circles, and rectangles. It explains the use of pure virtual functions and polymorphism to create a unified interface for shape classes. The report concludes that the use of abstract classes enhances code reusability and extensibility, allowing for easy addition of new shapes.
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)
20 views14 pages

Lab 10 (FA20-BEE-224

The lab report discusses the implementation of abstract classes in C++ to calculate the area of different shapes, including squares, circles, and rectangles. It explains the use of pure virtual functions and polymorphism to create a unified interface for shape classes. The report concludes that the use of abstract classes enhances code reusability and extensibility, allowing for easy addition of new shapes.
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/ 14

Object Oriented Programming

Lab report 10

Submitted by:-
Muhammad Abdullah

Registration No:-
FA20-BEE-224

Submitted to:-
Dr. Umar Khan

Dated: 12/05/2024

Lab 10 Implementation of abstract Classes


Abstract Class:-
Abstract Class is a class which contains at least one Pure Virtual function in it. Abstract
Classes are used to provide an Interface for its sub classes. Classes inheriting an Abstract
Class must provide definition to the pure virtual function, otherwise they will also become
Abstract class.

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;

std::cout << "Enter the radius of the circle:


";
std::cin >> radius;

// Creating objects of Square and Circle


Square square(side);
Circle circle(radius);

// Calculating and displaying the areas


std::cout << "Area of the square: " <<
square.calculateArea() << std::endl;
std::cout << "Area of the circle: " <<
circle.calculateArea() << std::endl;

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;

std::cout << "Enter the radius of the circle: ";


std::cin >> radius;

// Creating objects of Square and Circle


Square square(side);
Circle circle(radius);

// Calculating and displaying the areas


std::cout << "Area of the square: " <<
square.calculateArea() << std::endl;
std::cout << "Area of the circle: " <<
circle.calculateArea() << std::endl;

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;
}

// Function to display the dimensions of the


rectangle
void displayDimensions() {
std::cout << "Length: " << length << ", Width:
" << width << std::endl;
}
};

int main() {
float length, width;
std::cout << "Enter the length of the rectangle:
";
std::cin >> length;

std::cout << "Enter the width of the rectangle:


";
std::cin >> width;

// Creating an object of Rectangle


Rectangle rectangle(length, width);

// Calculating area and displaying the


dimensions and area
rectangle.calculateArea();
std::cout << "Rectangle Dimensions:" <<
std::endl;
rectangle.displayDimensions();
rectangle.displayArea();

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.

// Implementation of calculateArea for The program displays the areas of individual


Square shapes and calculates the total area of all
float calculateArea() const override { shapes, showcasing the correctness of the
return side * side; implemented algorithms for area calculation.
}
};

// Concrete class Circle inheriting from Shape


class Circle : public Shape {
private:
float radius;

public:
// Constructor
Circle(float r) : radius(r) {}

// Implementation of calculateArea for


Circle
float calculateArea() const override {
return M_PI * radius * radius;
}
};
// Concrete class Triangle inheriting from
Shape
class Triangle : public Shape {
private:
float base;
float height;

public:
// Constructor
Triangle(float b, float h) : base(b),
height(h) {}

// Implementation of calculateArea for


Triangle
float calculateArea() const override {
return 0.5 * base * height;
}
};

// Function to calculate total area of shapes


float calculateTotalArea(const
std::vector<Shape*>& shapes) {
float totalArea = 0.0;
for (const Shape* shape : shapes) { //
Change from reference to pointer
totalArea += shape->calculateArea();
}
return totalArea;
}

int main() {
// Create a vector of Shape pointers
std::vector<Shape*> shapes;

// Add different shapes to the vector


shapes.push_back(new Square(5)); //
Square with side 5 units
shapes.push_back(new Circle(3)); //
Circle with radius 3 units
shapes.push_back(new Triangle(4, 6));//
Triangle with base 4 units and height 6 units

// Draw shapes and calculate their areas


std::cout << "Calculating areas of shapes:"
<< std::endl;
for (const Shape* shape : shapes) { //
Iterate over pointers to Shape objects
std::cout << "Area: " << shape-
>calculateArea() << std::endl;
}

// Calculate total area of all shapes


std::cout << "\nTotal area of all shapes: "
<< calculateTotalArea(shapes) << std::endl;

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

You might also like