Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A class is a programmer-defined data type that describes what an object of the c

ID: 3815046 • Letter: A

Question

A class is a programmer-defined data type that describes what an object of the class will look like when it is created. It consists of a set of variables and a set of functions. A class is repository of functions that any main driver may use so programmers do not have to rewrite the same code. In this project you will create two classes and two main drivers to test the classes.

Part I

Part I will produce 2 files.

Geometry.h file is the Geometry class with its members and functions.

main cpp file tests all the code in the Geometry class.

The textbook examples place the class code in the same file as the main function, but your code will be more re-usable if the class is defined in a separate file – Geometry.h . The header file will need to be included in the program as you did in the Stock Problem and the stocks.h file.

The final version of the Geometry class (Geometry.h) will include:

Three constructors:

Constructor 1 – receives 2 integer values for length and width. (rectangle) Calls checkNum to validate each value. Assigns values to length and width.

Constructor 2 – receives 1 integer value for side. (cube) Calls checkNum to validate the value. Assigns value to side.

Constructor 3 – default constructor. Set variables to zero.

Private members: length

width

side

Private function:

checkNum - This function receives an int and checks to see if the value is less than or equal to zero. If the value is less than or equal to zero, display an error message and exit the program. exit(1); See the check_date() function in Listing Display 10.4.

Function Definitions

Here is an outline of the code to include in Geometry.h

class Geometry

{

public:

private:

};

// Function headers and code

Here is an outline for the code in the cpp file to guide you through the testing of the Geometry class:

#include

using namespace std;

#include "Geometry.h"

int main()

{

         //Set up values for testing and change as needed

int len = 0, wid = 0, side = 0;

        //Add code to test setLength, setWidth, setSide Display 10.4

       //Add code to instantiate the constructor for the rectangle Display 10.6

     //Test getLength and getWidth

      //Test getArea

      //Test getPerimeter

      //Add code to instantiate the constructor for the cube Display 10.6

      //Test getSide

     //Test getSurfaceArea

      //Test default constructor Display 10.6

      system("pause");

       return 0;

      }

Change the location of Geometry.h as needed or save it in the project folder with the cpp file and you don’t need a drive designation.

Function Receives Returns Actions setLength int void Calls checkNum Assigns length setWidth int void Calls checkNum Assigns width setSide int void Calls checkNum Assigns side getLength int getWidth int getSide int getArea int Calculates area of a rectangle getPerimeter int Calculates perimeter of a rectangle getSurfaceArea int Calculates surface area of a cube

Explanation / Answer

//Geometry.h
#ifndef GEOMETRY_H
#define GEOMETRY_H
#include<iostream>
#include<ctype.h>
using namespace std;
class Geometry
{
private:
int length;
int width;
int side;
bool checkNum(int num);
public:

Geometry();
Geometry(int l, int w, int s);
void setLength(int );
void setWidth(int);
void setSide(int);
int getLength();
int getWidth();
int getSide();
int getArea();
int getPerimeter();
int getSurfaceArea();

};
Geometry::Geometry()
{
length=0;
width=0;
side=0;
}
Geometry::Geometry(int l, int w, int s)
{
setLength(l);
setWidth(w);
setSide(s);
}
void Geometry::setLength(int length)
{
if(checkNum(length))
this->length=length;
else
{
cout<<"Invalid length"<<endl;
system("pause");
exit(0);
}
}
void Geometry::setWidth(int width)
{
if(checkNum(width))
this->width=width;
else
{
cout<<"Invalid width"<<endl;
system("pause");
exit(0);
}
}

void Geometry::setSide(int side)
{
if(checkNum(side))
this->side=side;
else
{
cout<<"Invalid side"<<endl;
system("pause");
exit(0);
}
}

int Geometry::getLength()
{
return length;
}
int Geometry::getWidth()
{
return width;
}

int Geometry::getSide()
{
return side;
}

int Geometry::getArea()
{
return length*width;
}
bool Geometry::checkNum(int num)
{
if(num<=0)
{
return false;
}
else
return true;
}
int Geometry::getPerimeter()
{
return 2*(length+width);
}
int Geometry::getSurfaceArea()
{
return 6*side*side;
}
#endif



//main.cpp
#include <iostream>
#include "Geometry.h"
using namespace std;
int main()
{

//Set up values for testing and change as needed
int len = 0, wid = 0, side = 0;
//Add code to test setLength, setWidth, setSide Display 10.4
Geometry g;
g.setLength(3);
g.setWidth(4);

//Add code to instantiate the constructor for the rectangle Display 10.6
//Test getLength and getWidth
//Test getArea
//Test getPerimeter
cout << "Length: " << g.getLength() << endl;
cout << "width: " << g.getWidth() << endl;
cout << "Area: " << g.getArea() << endl;
cout << "Perimeter: " << g.getPerimeter() << endl;

g.setSide(2);
cout << "side: " << g.getSide() << endl;
cout << "Surface Area: " << g.getSurfaceArea() << endl;
//Add code to instantiate the constructor for the cube Display 10.6
//Test getSide
//Test getSurfaceArea
//pause program output on console
system("pause");
return 0;
}

/*
output:

Length: 3
width: 4
Area: 12
Perimeter: 14
side: 2
Surface Area: 24

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote