Lab 10
Lab 10
Nida Noor
BCS233215
Assignment
Create a class named GeometricShape containing
● A pure virtual function named show() is create in the GeometricShape class
Derive a class named Rectangle from class GeometricShape that includes
● data members for the length and width of a rectangle,
● respective mutator and accessor functions to set and get values of length and width, and
● overriding function named computeArea() to compute the area of rectangle (length x
width)
● overriding function named show() to display the details associated with an instance of class
Rectangle
From the Rectangle class, derive a class named Cuboid that contains
● a data field named height in addition to length and width,
● two functions named setHeight and getHeight() to set and get value of height data field,
and
● an overriding function named computeArea() to calculate the area of a cuboid (length x
width x height)
● overriding function named show() to display the details associated with an instance of class
Cuboid. In the main function, create instances of derived classes to access respective show()
function using
dynamic binding
Source Code
#include <iostream>
using namespace std;
// class GeometricShape
class GeometricShape {
public:
// Pure virtual functions
virtual void show() const = 0;
virtual double computeArea() const = 0;
// Virtual destructor
virtual ~GeometricShape() = default;
};
// Derived class
class Rectangle : public GeometricShape {
protected:
//data members
double length;
double width;
public:
// Constructor
Rectangle(double len = 0.0, double wid = 0.0)
{
length = len;
width = wid;
}
// Mutator
void setLength(double len)
{
length = len;
}
void setWidth(double wid)
{
width = wid;
}
// Accessor
double getLength() const
{
return length;
}
double getWidth() const {
return width;
}
// Overriding computeArea function
double computeArea() const override {
return length * width;
}
// Overriding show function
void show() const override {
cout << "Rectangle: " <<"\n";
cout << "length is : " << length << "\n";
cout << " width is: " << width << "\n";
cout << " Area is: " << computeArea() ;
cout<< "\n";
}
};
// Derived class from rectangle
class Cuboid : public Rectangle {
private:
//data member
double height;
public:
// Constructor
Cuboid(double len = 0.0, double wid = 0.0, double h = 0.0)
{
length = len;
width = wid;
height = h;
}
// Mutator
void setHeight(double h)
{
height = h;
}
// Accessor
double getHeight() const
{
return height;
}
// Overriding computeArea function
double computeArea() const override
{
return length * width * height;
}
// Overriding show function
void show() const override
{
cout << "Cuboid:"<<endl;
cout << "length is: " << length << endl;
cout << " Width is: " << width << endl;
cout << " Height is: " << height << endl;
cout<<"Volume is: " << computeArea() << endl;
}
};
int main() {
GeometricShape* s;
Rectangle r (5.0, 3.0);
s = &r;
// shows details of rectangle
s->show();
Cuboid cub (5.0, 3.0, 4.0);
s = &cub;
//shows details of cuboid
s->show();
return 0;
}