C++ Program Squares as a Class Exercise 1: This program asks you to fill in the
ID: 3634570 • Letter: C
Question
C++ Program
Squares as a Class
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 8The 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;
}
Explanation / Answer
Please Rate:Thanks
This program will help you
#include<iostream.h>
class Square{
float side;
public:
void setSide(float);
float findArea();
float findPerimeter();
};
float Square :: findPerimeter(){
return 4*side;
}
float Square :: findArea(){
return side*side;
}
void Square :: setSide(float length){
side=length;
}
int main(){
Square box;
float size;
cout<<"Enter the length of side ";
cin>>size;
box.setSide(size);
cout<<"Area = "<<box.findArea()<<endl;
cout<<"Perimeter ="<<box.findPerimeter();
return 0;
}
-----------------------------------------------------------------------
Output1:
output2:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.