C++ OOP and Classes. Would someone be able to walk me through this please? Thank
ID: 3669013 • Letter: C
Question
C++ OOP and Classes. Would someone be able to walk me through this please? Thank you!
Program square.cpp is as follows:
// This program declares the Square class and uses member functions to find
// the perimeter and area of the square
#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;
}
Explanation / Answer
#include <iostream>
using namespace std;
class Square{// FILL IN THE CODE TO DECLARE A CLASS CALLED Square. TO DO THIS SEE
private:// THE IMPLEMENTATION SECTION.
float side;
public:
void setSide(float);
float findArea() const;
float findPerimeter() const;
Square();
Square(float); // add these after commiting
~Square();
};
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)
cout << "What is the length of one side of the square?" << endl;
cin >> size;
// FILL IN THE CODE THAT CALLS SetSide.
box.setSide(size);
// FILL IN THE CODE THAT WILL RETURN THE AREA FROM A CALL TO A FUNCTION
// AND PRINT OUT THE AREA TO THE SCREEN
cout << "This is the Area of the square: " << box.findArea() << endl;
// FILL IN THE CODE THAT WILL RETURN THE PERIMETER FROM A CALL TO A
// FUNCTION AND PRINT OUT THAT VALUE TO THE SCREEN
cout << "This is the Perimeter of the square: " << box.findPerimeter()<< endl;
Square box1(9);
cout << "The area of box1 is " << box1.findArea() << endl;
cout << "The perimeter of box1 is "<< box1.findPerimeter() << endl;
char st;
cin >> st;
return 0;
}
//__________________________________________________________________
//Implementation section Member function implementation
Square::Square()
{
side = 1;
}
Square::Square(float x)
{
side = x;
}
Square::~Square()
{
}
//**************************************************
// 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() const
{
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() const
{
return 4 * side;
}
Sample output
What is the length of one side of the square?
4
This is the Area of the square:
16
This is the Perimeter of the square:
16
The area of box1 is 81
The perimeter of box1 is 36
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.