Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Lab 13.1 Squares as a Class Retrieve program square.cpp from the Lab 13 folder. The code is as follows: // This program declares the Square

Lab 13.1 Squares as a Class Retrieve program square.cpp from the Lab 13 folder. The code is as follows:

// This program declares the Square class and uses member functions to find // the perimeter and area of the square

#include 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; } continues

262 LESSON SET 13 Introduction to Classes

//************************************************** // 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; } Exercise 1: This program asks you to fill in the class declaration and client code based on the implementation of the member functions. Fill in the code so that the following input and output will be generated: 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 Exercise 2: Add two constructors and a destructor to the class and create the implementation of each. One constructor is the default constructor that sets the side to 1. The other constructor will allow the user to initialize the side at the definition of the object. The destructor does not have to do anything but reclaim memory space. Create an object called box1 that gives the value of 9 to the constructor at the definition. Add output statements so that the following is printed in addition to what is printed in Exercise 1. The area of box1 is 81 The perimeter of box1 is 36

CODE

// This program declares the Square class and uses member functions to find

// the perimeter and area of the square

// Navjot singh

#include

using namespace std;

class Square {

float side;

public:

void setSide(float);// FILL IN THE CODE TO DECLARE A CLASS CALLED Square. TO DO THIS SEE

// THE IMPLEMENTATION SECTION.

float findArea();

float findPerimeter();

};

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

cout << "Please input the length of the side of the square ";

cin >> size; // FILL IN THE CLIENT CODE THAT WILL ASK THE USER FOR THE LENGTH OF THE

// SIDE OF THE SQUARE. (This is stored in size)

box.setSide(size); // FILL IN THE CODE THAT CALLS SetSide.

cout << "The area of the square is " << box.findArea() << ' ';// FILL IN THE CODE THAT WILL RETURN THE AREA FROM A CALL TO A FUNCTION

// AND PRINT OUT THE AREA TO THE SCREEN.

cout << "The perimeter of the square is " << box.findPerimeter() << ' '; // FILL IN THE CODE THAT WILL RETURN THE PERIMETER FROM A CALL TO A

// FUNCTION AND PRINT OUT THAT VALUE TO THE SCREEN.

system("pause");

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;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Accidental Data Scientist

Authors: Amy Affelt

1st Edition

1573877077, 9781573877077

More Books

Students also viewed these Databases questions

Question

Find the sum of all the even integers between 23 and 97.

Answered: 1 week ago