iLAB STEPS STEP 1: Create a New Multifile Project Back to Top Create a new multi
ID: 3771701 • Letter: I
Question
iLAB STEPS
STEP 1: Create a New Multifile Project
Back to Top
Create a new multifile project with three classes: the base class Shape, the class Circle (derived public from Shape), and the class Rectangle (derived public from Shape). The classes will have the following requirements.
The class Shape should be an abstract class with the following pure virtual functions.
area()
perimeter()
The class Circle should be derived public from the class Shape and override both the area() and the perimeter() functions.
The perimeter() function should correctly calculate the circumference of a circle, given a radius.
The area() function should correctly calculate the area of a circle, given a radius
Include all the necessary accessor and mutator functions to accomplish the requirements of the class.
The class Rectangle should be derived public from the class Shape and override both the area() and the perimeter() functions.
The perimeter() function should correctly calculate the circumference of a rectangle, given its dimensions.
The area() function should correctly calculate the area of a rectangle, given its dimensions.
Include all the necessary accessor and mutator functions to accomplish the requirements of the class.
STEP 2: Create the Test Function
Back to Top
Instantiate at least one object of each of the Circle and the Rectangle classes. Provide appropriate constructors for both that will accept the necessary initialization arguments to provide the information required for all the class member functions. Exercise and test each member function of both classes for correct calculations and output.
STEP 3: Add a Base Class Pointer Array and an Additional Function
Back to Top
Add to the test function a base class array of pointers of the same dimension as the total number of Circle and Rectangle objects that were created in the previous step. Use this pointer array to access the Circle and the Rectangle objects to call a new, nonclass member function that will display all the information about each object.
Circle objects should display radius, circumference, and area.
Rectangle objects should display dimensions, perimeter. and area.
The information-display function should accept as its calling parameter a pointer of the class Shape.
Run the test function to demonstrate static (early) binding using the derived class objects calling their member functions, and run the test function to demonstrate dynamic (late) binding using the assigned Shape class pointers to call the nonclass, member-display-information function.
Explanation / Answer
Shape.h
#pragma once
class Shape
{
public:
Shape();
virtual ~Shape();
virtual double area() = 0;
virtual double perimeter() = 0;
};
Shape.cpp
#include "Shape.h"
Shape::Shape()
{
}
Shape::~Shape()
{
}
Circle.h
#pragma once
#include "Shape.h"
class Circle:public Shape
{
public:
Circle();
Circle(double r);
~Circle();
double area();
double area(double r);
double perimeter();
double perimeter(double r);
double getRadius(){ return radius; };
void setradius(double r){ radius = r; };
private:
double radius;
};
Circle.cpp
#include "Circle.h"
Circle::Circle()
{
}
Circle::Circle(double r)
{
radius = r;
}
Circle::~Circle()
{
}
double Circle::area(double r)
{
double area = 3.14*r*r;
return area;
}
double Circle::area()
{
double area = 3.14*radius*radius;
return area;
}
double Circle::perimeter()
{
double perimeter = 2 * 3.14*radius;
return perimeter;
}
double Circle::perimeter(double r)
{
double perimeter = 2 * 3.14*r;
return perimeter;
}
Rectangle.h
#pragma once
#include "Shape.h"
class Rectangle:public Shape
{
public:
Rectangle();
Rectangle(double l, double b);
~Rectangle();
double area();
double perimeter();
double area(double l, double b);
double perimeter(double l, double b);
double getLength(){ return length; };
void setLength(double l){ length = l; };
double getBreadth(){ return breadth; };
void setBreadth(double b){ breadth = b; };
private:
double length;
double breadth;
};
Rectangle.cpp
#include "Rectangle.h"
Rectangle::Rectangle()
{
}
Rectangle::Rectangle(double l, double b)
{
length = l;
breadth = b;
}
Rectangle::~Rectangle()
{
}
//Function Overriding
double Rectangle::area()
{
double area = length*breadth;
return area;
}
//Function Overloading
double Rectangle::area(double l,double b)
{
double area = l*b;
return area;
}
double Rectangle::perimeter()
{
double perimeter = 2 * (length + breadth);
return perimeter;
}
double Rectangle::perimeter(double l,double b)
{
double perimeter = 2 * (l + b);
return perimeter;
}
Test.cpp (for test function)
#include<iostream>
#include "Circle.h"
#include "Rectangle.h"
using namespace std;
int main()
{
Circle* c = new Circle(5);
cout <<"Radius of circle is "<< c->getRadius()<<endl;
cout<<"area of circle is "<<c->area()<<endl;
cout<<"circumfenrence of circle is " << c->perimeter()<<endl;
cout << "area of circle is " << c->area(8) << endl;
cout << "circumfenrence of circle is " << c->perimeter(8) << endl;
cout << endl;
Rectangle* r = new Rectangle(2, 5);
cout <<"length of rectangle is "<< r->getLength() << endl;
cout << "breadth of rectangle is " << r->getBreadth() << endl;
cout << "area of rectangle is " << r->area() << endl;
cout << "perimeter of rectangle is " << r->perimeter() << endl;
cout << "area of rectangle is " << r->area(5,4) << endl;
cout << "perimeter of rectangle is " << r->perimeter(5,4) << endl;
cout << endl;
Shape* arr[] = { c, r };
for (int i = 0; i < 2; ++i)
{
cout << "area " << arr[i]->area() << endl;
cout << "perimeter " << arr[i]->perimeter() << endl;
}
system("pause");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.