Goals:
Through the completion of this assignment students should familiarize themselves with the creation of an interface. Students should understand the purpose of organizing methods into an interface so that multiple classes can share the same functionality. Students should also note that implementation of an interface provides a simple way for classes to be organized within a single category without the need for complex inheritance, which can be troublesome and easily leads to code rot — software that loses its reusability over time.
Files to be Submitted:
In this assignment students should create five header classes called Point, Circle, Rectangle, Square, FigureGeometry and one cpp file TestAll. The TestAll class should implement a main method which tests all of the other files created in the assignment. After the assignment has been completed, all six files should be submitted for grading into the Dropbox for Assignment 3, which can be found in the Dropbox menu on the course website. Students should note that only the *.h or .cpp file for each class needs to be submitted.
Please click on each of the following links to view specific requirements for the files that must be created and submitted: (File criteria are listed below)
- Requirements: Point.h
- Requirements: FigureGeometry.h
- Requirements: Circle.h
- Requirements: Rectangle.h
- Requirements: Square.h
- Requirements: TestAll.cpp
Tips:
The following code example displays a basic implementation of an interface:
//FigureGeometry.h:
#ifndef FIGUREGEOMETRY_H
#define FIGUREGEOMETRY_H
classFigureGeometry
{
private:
public:
FigureGeometry() {}
virtualfloatgetArea() const = 0;
virtualfloatgetPerimeter() const = 0;
};
staticconst float PI = 3.14f;
#endif
Requirements: TestAll.cpp
Description:
The TestAll class should be declared as a class and should meet all the requirements listed below. Its purpose is to implement a main method which creates three objects — a Circle object, a Square object, and a Rectangle object — and test each of the files that have been designed for the Chapter 3 Assignment.
Methods:
Modifiers
Return Type
Name
Parameters
Implementation
void
main
()
Students should already be familiar with how to instantiate objects and print values to the screen using cout<< . Therefore, the actual implementation code for this assignment will not be provided. Students may organize the output of data according to their own specifications. However, the main method must perform the following tasks:
1. Create an instance of Circle, called c1, with a radius of 5.
2. Create an instance of Square, called s1, with a side length of 5.
3. Create an instance of Rectangle, called r1, with a width of 5 and a height of 7.
4. Print the radius of c1.
5. Print the area of c1.
6. Print the perimeter of c1.
7. Print the side length of s1.
8. Print the area of s1.
9. Print the perimeter of s1.
10. Print the width of r1.
11. Print the height of r1.
12. Print the area of r1.
13. Print the perimeter of r1.
Output:
Output of the main method should be similar to the following:
Details of c1:
radius: 5
area: 78.5
perimeter: 31.4
Details of s1:
side length: 5
area: 25
perimeter: 20
Details of r1:
width: 5
height: 7
area: 35
perimeter: 24
Requirements: FigureGeometry.h
Description:
The FigureGeometry interface should be declared as a class FigureGeometry and should meet all the requirements listed below. Its purpose is to declare all the necessary methods that any geometric figure, such as a circle, rectangle, or square, should contain. The FigureGeometry interface should also declare a numeric constant, called PI, which can be used by classes that include the FigureGeometry interface. Students should also note that the inclusion of instance variables within an interface declaration is not allowed; only static constants may be defined within an interface declaration.
Constants:
Modifiers
Type
Name
Value
static
const float
PI
3.14f
Methods:
Return Type
Name
Parameters
float
getArea()
none
float
getPerimeter()
none
Tips:
The following coding example illustrates a version of the FigureGeometry.
ifndef FIGUREGEOMETRY_H_
#define FIGUREGEOMETRY_H_
class FigureGeometry
{
private:
public:
FigureGeometry() {} //default constructor
/**
* Classes that implement the FigureGeometry interface MUST override this
* method which should return the geometric area of a figure:
*
* In an interface, methods are virtual.
*
*/
virtualfloatgetArea() const = 0;
/**
* Remember, all interface methods are virtual,
* and abstract method declarations should always
* end in a semicolon instead of a method body.
*/
/**
* Classes that implement the FigureGeometry interface MUST also override
* this method which should return the geometric perimeter of a figure:
*/
virtualfloatgetPerimeter() const = 0;
}
staticconstfloat PI = 3.14f;
#endif
Requirements: Circle.h
Description:
The Circle class should be declared as a public class that includes the FigureGeometry interface described in the Chapter 3 Assignment sheet and should meet all the requirements listed below. Its purpose is to store the radius of a circular figure and provide the methods necessary to calculate the area and perimeter of such a figure.
Variables:
Modifiers
Type
Name
Purpose
private
float
radius
stores the radius of a Circle object
Constructors:
Modifiers
Parameters
Implementation
public
float theRadius
initializes the radius of a Circle object in the following manner:
radius = theRadius;
Methods:
Modifiers
Return Type
Name
Parameters
Implementation
public
float
getRadius
none
returns the radius of a Circle object in the following manner:
return radius;
virtual
float
getArea
none
returns the area of a Circle object in the following manner:
return getRadius() * getRadius() * PI;
virtual
float
getPerimeter
none
returns the perimeter of a Circle object in the following manner:
return getRadius() * 2 * PI;
public
void
setRadius
float theRadius
assigns the radius of a Circle object in the following manner:
radius = theRadius;
Requirements: Rectangle.h
Description:
The Rectangle class should be declared as a public class that implements the FigureGeometry interface described in the Chapter 3 Assignment sheet and should meet all the requirements listed below. Its purpose is to store the Point of a rectangular figure (using the Point class described in the Chapter 3 Assignment sheet) and provide the methods necessary to calculate the area and perimeter of such a figure.
Variables:
Modifiers
Type
Name
Purpose
private
Point
point
stores the Point point of a Rectangle object
Constructors:
Modifiers
Parameters
Implementation
public
Point p1;
initializes the Point p1 of a Rectangle object in the following manner:
point =p1;
Methods:
Modifiers
Return Type
Name
Parameters
Implementation
public
int
getWidth
none
returns the width of a Rectangle object in the following manner:
return point.getWidth();
public
int
getHeight
none
returns the height of a Rectangle object in the following manner:
return point.getHeight();
virtual
float
getArea
none
returns the area of a Rectangle object in the following manner:
return getWidth() * getHeight();
virtual
float
getPerimeter
none
returns the perimeter of a Rectangle object in the following manner:
return ( getWidth() + getHeight() ) * 2;
public
void
setPoint
Point p1;
assigns the Point p1 to point:
point= p1;
Requirements: Square.h
Description:
The Square class should be declared as a class that includes the FigureGeometry interface described in the Chapter 3 Assignment sheet and should meet all the requirements listed below. Its purpose is to store the Point of a square figure (using the Point class described in the Chapter 3 Assignment sheet) and provide the methods necessary to calculate the area and perimeter of such a figure.
Variables:
Modifiers
Type
Name
Purpose
private
Point
point
stores the Point of Square object
Constructors:
Modifiers
Parameters
Implementation
public
Point p1
initializes the Point of a Square object in the following manner:
point= p1;
Methods:
Modifiers
Return Type
Name
Parameters
Implementation
public
int
getSideLength
none
returns the side length of a Square object in the following manner:
return point.getWidth();
virtual
float
getArea
none
returns the area of a Square object in the following manner:
return getSideLength() * getSideLength();
virtual
float
getPerimeter
none
returns the perimeter of a Square object in the following manner:
return getSideLength() * 4;
public
void
setPoint
Point p1
assigns the side length of a Square object in the following manner:
point= p1;
Requirements: Point.h
Description:
The Point class should be declared as a class and should meet all the
requirements listed below. Its purpose is to store the Point (width and
height) of a two-dimensional, rectangular geometric figure.
Variables:
Modifiers TypeName Purpose
Private: intwidth stores the width of a Point object
Private: intheight stores the height of a Point object
Constructors:
Modifiers Parameters Implementation
Public initializes the width and height of a Point object in the following manner:
width = 0;
height = 0;
Public inttheWidth, initializes the width and height of a Point object in the
InttheHeightfollowing manner: width = theWidth;
height = theHeight;
Methods:
Modifiers Return Type Name Parameters Implementation
Public int getWidth() const none returns the width of a Point object:
returnwidth;
Public int getHeight() const none returns the height of a point object:
returnheight;
Public void setWidthint width = theWidth;
theWidth
Public void setHeight int height = theHeight;
theHeight