More fun with Classes and Objects

You are going to create a small program that relies on object oriented design. For these programs only a single source file is required.

Part A: Create project Square

  • Launch your compiler
  • Create a new C++ source code file and project.
  • Modify the following code as indicated. (If your confused, look at the implementation code and last weeks lab project.)
  • The program asks you to fill in the class definition and client code based on the implementation of the member functions. Fill in the code that the following input and output will be generated:

Sample Input and Output

Please input the length of the side of the square: 8

The area of the square is 64

The perimeter of the square is 32

#include <iostream>
using namespace std;

// FILL IN THE CODE TO DECLARE A CLASS CALLED Square. TO DO THIS SEE
// THE IMPLEMENTATION SECTION.

int main()
{
Square box; // box is defined as an object of the Square class
float size; // size contains the length of a side of the square

// FILL IN THE CLIENT CODE THAT WILL ASK THE USER FOR THE LENGTH OF THE SIDE
// OF THE SQUARE. (This is stored in size)

// FILL IN THE CODE THAT CALLS SetSide.

// FILL IN THE CODE THAT WILL RETURN THE AREA FROM A CALL TO A FUNCTION
// AND PRINT OUT THE AREA TO THE SCREEN

// FILL IN THE CODE THAT WILL RETURN THE PERIMETER FROM A CALL TO A
// FUNCTION AND PRINT OUT THAT VALUE TO THE SCREEN

return 0;
}

//__________________________________________________________________
//Implementation section Member function implementation

//**************************************************
// setSide
//
// task: This procedure takes the length of a side and
// places it in the appropriate member data
// data in: length of a side
//***************************************************

void Square::setSide(float length)

{
side = length;
}
//**************************************************
// findArea
//
// task: This finds the area of a square
// data in: none (uses value of data member side)
// data returned: area of square
//***************************************************

float Square::findArea()
{
return side * side;
}
//**************************************************
// findPerimeter
//
// task: This finds the perimeter of a square
// data in: none (uses value of data member side)
// data returned: perimeter of square
//***************************************************
float Square::findPerimeter()
{
return 4 * side;
}


Part B: Create project Circle

  • Launch MS Visual C++
  • Create a new C++ source code file and project.
  • Modify the following code as indicated. (If your confused, look at the implementation code and last weeks lab project.)
  • Alter the code so that setting the center of the circle is also done during object definition. This means that the constructors will also take care of this initialization. Make the default center at point (0,0) and keep the default radius as 1. Have sphere defined with initial values of 8 for the radius and (9,10) for the center.

Sample Output

The radius of the circle is 8

The center of the circle is (9,10)

The area of the circle is 200.96

The circumference of the circle is 50.24

#include <iostream>
using namespace std;
//_________________________________________________________________________
// This program declares a class for a circle that will have
// member functions that set the center, find the area, find
// the circumference and display these attributes.
// The program as written does not allow the user to input data, but
// rather has the radii and center coordinates of the circles (spheres in the program)
// initialized at declaration or set by a function.

//class declaration section (header file)

class Circles
{
public:
void setCenter(int x, int y);
double findArea();
double findCircumference();
void printCircleStats(); // This outputs the radius and center of the circle.
Circles (float r); // Constructor
Circles(); // Default constructor
private:
float radius;
int center_x;
int center_y;
};


const double PI = 3.14;

//Client section

int main()
{
Circles sphere(8);
sphere.setCenter(9,10);
sphere.printCircleStats();

cout << "The area of the circle is " << sphere.findArea() << endl;
cout << "The circumference of the circle is "
<< sphere.findCircumference() << endl;

return 0;
}

//___________________________________________________________________________
//Implementation section Member function implementation

Circles::Circles()
{
radius = 1;
}
// Fill in the code to implement the non-default constructor

// Fill in the code to implement the findArea member function

// Fill in the code to implement the findCircumference member function

void Circles::printCircleStats()
// This procedure prints out the radius and center coordinates of the circle
// object that calls it.

{
cout << "The radius of the circle is " << radius << endl;
cout << "The center of the circle is (" << center_x
<< "," << center_y << ")" << endl;
}

void Circles::setCenter(int x, int y)
// This procedure will take the coordinates of the center of the circle from
// the user and place them in the appropriate member data.

{
center_x = x;
center_y = y;
}


Turnin source printouts and screen shots of output for lab credit.


Back to Top

! You are using a version 4 browser or older. If you are experiencing any problems with scrolling, please reload the page.