For your program you will create a class that represents square shapes. I provid
ID: 3717731 • Letter: F
Question
For your program you will create a class that represents square shapes. I provide you an incomplete client program that will test your class and an incomplete header file where you have to declare and implement the class.
In the client program you have to complete certain statements according to the indications provided in the corresponding comments.
In the header file you have to create class square according to the following specifications:
Member functions
// member that assigns a value to the side void setSide(int s);
// member function that returns the value of side int getSide();
// parameterized constructor with default parameter = 0 square(int s);
// member function that returns the addition of the areas of 2 squares int plus(square sq); // member function that returns the subtraction of the areas of two squares int minus(square sq);
// member function that returns a square of side equal to the side of the square multiplied by a factor square increaseBy(int factor);
// member function that returns a square of area equivalent to the average area of 2 squares. The // side of the returned square must be rounded to the nearest ones. square avgSquare(square sq);
// member function that compares 2 squares for equality bool equalTo(square sq);
// member function that returns the area of the square int area();
// member function that returns the perimeter of the square int perimeter();
Data member
side: holds the side of the square (whole number)
When you declare the class you must define which members should be public and which should be private, which member functions should be const, and the data type of the data member among other things. You then have to define the member functions below the class declaration in the same file. CODE TO COMPLETE:
//CLIENT PROGRAM THAT WORKS WITH SQUARES
#include <iostream>
#include <fstream>
/*your code here*/ // include the file header for squares
using namespace std;
int main()
{
ifstream inFile;
int s1, s2, s3; // declare three int variables
// declare 4 objects of type square named sq1, sq2, sq3, and sq4 initializing the third one to 10
/*your code here*/
inFile.open("input21.txt");
if(!inFile)
{
cout << "Could not open input file!" << endl;
system("pause");
return 1;
}
// Access members using dot operator
cout << "Before assigning values to the squares their sides are:" << endl;
cout << "Square 1: " << /*your code here*/ << endl; // Show the initial side of Square 1
cout << "Square 2: " << /*your code here*/ << endl; // Show the initial side of Square 2
cout << "Square 3: " << /*your code here*/ << endl; // Show the initial side of Square 3
cout << endl << "............Starting to process the file............" << endl << endl;
inFile >> s1 >> s2 >> s3; // read 3 sides from the file (priming read)
while(inFile)
{
cout << "Storing the values read into the side of squares..." << endl << endl;
/*your code here*/ // store s1 in Square 1
/*your code here*/ // store s2 in Square 2
/*your code here*/ // store s3 in Square 3
cout << "Displaying the new side of squares" << endl;
cout << "Square 1: " << /*your code here*/ << endl; // Show the new side of Square 1
cout << "Square 2: " << /*your code here*/ << endl; // Show the new side of Square 2
cout << "Square 3: " << /*your code here*/ << endl; // Show the new side of Square 3
cout << endl << "Testing other member functions" << endl << endl;
cout << "Area of Square 1: " << /*your code here*/ << endl; // show the area of Square 1
cout << "Perimeter of Square 2: " << /*your code here*/ << endl; // show the perimeter of Square 2
if(/*your code here*/) // if Square 1 is equal to Square 3
cout << "Square 1 and Square 3 are equal" << endl;
else
cout << "Square 1 and Square 3 are not equal" << endl;
cout << "Area of (Square 2 + Square 3): " << /*your code here*/ << endl; // Add the areas of Square 2 and Square 3
cout << "Area of (Square 3 - Square 1): " << /*your code here*/ << endl; // Subtract the area of Square 1 from Square 3
cout << "Creating a square double the size of Square 1" << endl;
/*your code here*/ // Double the area of Square 1 and assign it to Square 4
cout << "The side of the new square: " << /*your code here*/ << endl; // Show the side of Square 4
cout << "Creating a square of area equal to average area of Square 2 and Square 3" << endl;
/*your code here*/ // Get the average area of Square 2 and Square 3 and assign it to Square 4
cout << "The side of the new square: " << /*your code here*/ << endl; // Show the side of Square 4
cout << "The area of the new square: " << /*your code here*/ << endl; // Show the area of Square 4
cout << endl << "..........Done with this set of values :-).........." << endl << endl;
inFile >> s1 >> s2 >> s3; // read next set of 3 sides
}
inFile.close();
system("pause");
return 0;
}
// SPECIFICATION FILE FOR C++ CLASS Square
// class declaration
class square
{
/*your code here*/
};
// class implementation
/*your code here*/
OUTPUT SHOULD BE THE FOLLOWING: The following is a sample run of my solution processing the values from the input file provided: ___________________________________________________________________________________
Before assigning values to the squares their sides are: Square 1: 0 Square 2: 0 Square 3: 10
............Starting to process the file............
Storing the values read into the side of squares...
Displaying the new side of squares Square 1: 2 Square 2: 5 Square 3: 7
Testing other member functions
Area of Square 1: 4 Perimeter of Square 2: 20 Square 1 and Square 3 are not equal Area of (Square 2 + Square 3): 74 Area of (Square 3 - Square 1): 45 Creating a square double the size of Square 1 The side of the new square: 4 Creating a square of area equal to average area of Square 2 and Square 3 The side of the new square: 6 The area of the new square: 36
..........Done with this set of values :-)..........
Storing the values read into the side of squares...
Displaying the new side of squares Square 1: 3 Square 2: 2 Square 3: 1
Testing other member functions Area of Square 1: 9 Perimeter of Square 2: 8 Square 1 and Square 3 are not equal Area of (Square 2 + Square 3): 5 Area of (Square 3 - Square 1): -8 Creating a square double the size of Square 1 The side of the new square: 6 Creating a square of area equal to average area of Square 2 and Square 3 The side of the new square: 2 The area of the new square: 4
..........Done with this set of values :-)..........
Storing the values read into the side of squares...
Displaying the new side of squares Square 1: 4 Square 2: 4 Square 3: 4
Testing other member functions
Area of Square 1: 16 Perimeter of Square 2: 16 Square 1 and Square 3 are equal Area of (Square 2 + Square 3): 32 Area of (Square 3 - Square 1): 0 Creating a square double the size of Square 1 The side of the new square: 8 Creating a square of area equal to average area of Square 2 and Square 3 The side of the new square: 4 The area of the new square: 16
..........Done with this set of values :-)..........
Press any key to continue . . . ___________________________________________________________________________________ PLEASE COMPLETE USING C++ LANGUAGE// OUTPUT SHOULD BE EXACTLY THE SAME!!
Explanation / Answer
ANS:-
Given that,
PROGRAM:-
main.cpp
#include <iostream>
#include <fstream>
/*your code here*/ // include the file header for squares
#include "square.h"
using namespace std;
int main()
{
ifstream inFile;
int s1, s2, s3; // declare three int variables
// declare 4 objects of type square named sq1, sq2, sq3, and sq4 initializing the third one to 10
square sq1;
square sq2;
square sq3(10);
square sq4;
/*your code here*/
inFile.open("D:\chegg\input21.txt"); // Change the address of file here
if(!inFile)
{
cout << "Could not open input file!" << endl;
system("pause");
return 1;
}
// Access members using dot operator
cout << "Before assigning values to the squares their sides are:" << endl;
cout << "Square 1: " << /*your code here*/sq1.getSide() << endl; // Show the initial side of Square 1
cout << "Square 2: " << /*your code here*/sq2.getSide() << endl; // Show the initial side of Square 2
cout << "Square 3: " << /*your code here*/sq3.getSide() << endl; // Show the initial side of Square 3
cout << endl << "............Starting to process the file............" << endl << endl;
inFile >> s1 >> s2 >> s3; // read 3 sides from the file (priming read)
while(inFile)
{
cout << "Storing the values read into the side of squares..." << endl << endl;
/*your code here*/ // store s1 in Square 1
sq1.setSide(s1);
/*your code here*/ // store s2 in Square 2
sq2.setSide(s2);
/*your code here*/ // store s3 in Square 3
sq3.setSide(s3);
cout << "Displaying the new side of squares" << endl;
cout << "Square 1: " << /*your code here*/sq1.getSide() << endl; // Show the new side of Square 1
cout << "Square 2: " << /*your code here*/sq2.getSide() << endl; // Show the new side of Square 2
cout << "Square 3: " << /*your code here*/sq3.getSide() << endl; // Show the new side of Square 3
cout << endl << "Testing other member functions" << endl << endl;
cout << "Area of Square 1: " << /*your code here*/sq1.area() << endl; // show the area of Square 1
cout << "Perimeter of Square 2: " << /*your code here*/sq2.perimeter() << endl; // show the perimeter of Square 2
if(/*your code here*/sq1.equalTo(sq3)) // if Square 1 is equal to Square 3
cout << "Square 1 and Square 3 are equal" << endl;
else
cout << "Square 1 and Square 3 are not equal" << endl;
cout << "Area of (Square 2 + Square 3): " << /*your code here*/sq2.plus(sq3) << endl; // Add the areas of Square 2 and Square 3
cout << "Area of (Square 3 - Square 1): " << /*your code here*/sq3.minus(sq1) << endl; // Subtract the area of Square 1 from Square 3
cout << "Creating a square double the size of Square 1" << endl;
/*your code here*/ // Double the area of Square 1 and assign it to Square 4
sq4 = sq1.increaseBy(2);
cout << "The side of the new square: " << /*your code here*/ sq4.getSide()<< endl; // Show the side of Square 4
cout << "Creating a square of area equal to average area of Square 2 and Square 3" << endl;
/*your code here*/ // Get the average area of Square 2 and Square 3 and assign it to Square 4
sq4 = sq2.avgSquare(sq3);
cout << "The side of the new square: " << /*your code here*/ sq4.getSide()<< endl; // Show the side of Square 4
cout << "The area of the new square: " << /*your code here*/ sq4.area()<< endl; // Show the area of Square 4
cout << endl << "..........Done with this set of values :-).........." << endl << endl;
inFile >> s1 >> s2 >> s3; // read next set of 3 sides
}
inFile.close();
system("pause");
return 0;
}
square.h
#include<cmath>
#include<iostream>
using namespace std;
// class declaration
class square
{
/*your code here*/
private:
int side;
public:
void setSide(int s);
int getSide();
square(int s);
square();
int plus(square sq);
int minus(square sq);
square increaseBy(int factor);
square avgSquare(square sq);
bool equalTo(square sq);
int area();
int perimeter();
};
// class implementation
/*your code here*/
void square::setSide(int s) {
this->side = s;
}
int square::getSide() {
return this->side;
}
square::square() {
this->side = 0;
}
square::square(int s) {
this->side = s;
}
int square::plus(square sq) {
return this->area() + sq.area();
}
int square::minus(square sq) {
return abs(this->area() - sq.area());
}
square square:: increaseBy(int factor) {
square ss(this->side*factor);
return ss;
}
square square::avgSquare(square sq) {
double avgArea = (this->area() + sq.area()) / 2;
int newside = sqrt(avgArea);
square ss(newside);
return ss;
}
bool square::equalTo(square s) {
if(this->side == s.side) {
return true;
} else {
return false;
}
}
int square::area() {
return this->side * this->side;
}
int square::perimeter() {
return 4*this->side;
}
Output
sample text file
1 2 3
4 5 6
7 8 9
4 5 6
Output for this
Before assigning values to the squares their sides are:
Square 1: 0
Square 2: 0
Square 3: 10
............Starting to process the file............
Storing the values read into the side of squares...
Displaying the new side of squares
Square 1: 1
Square 2: 2
Square 3: 3
Testing other member functions
Area of Square 1: 1
Perimeter of Square 2: 8
Square 1 and Square 3 are not equal
Area of (Square 2 + Square 3): 13
Area of (Square 3 - Square 1): 8
Creating a square double the size of Square 1
The side of the new square: 2
Creating a square of area equal to average area of Square 2 and Square 3
The side of the new square: 2
The area of the new square: 4
..........Done with this set of values :-)..........
Storing the values read into the side of squares...
Displaying the new side of squares
Square 1: 4
Square 2: 5
Square 3: 6
Testing other member functions
Area of Square 1: 16
Perimeter of Square 2: 20
Square 1 and Square 3 are not equal
Area of (Square 2 + Square 3): 61
Area of (Square 3 - Square 1): 20
Creating a square double the size of Square 1
The side of the new square: 8
Creating a square of area equal to average area of Square 2 and Square 3
The side of the new square: 5
The area of the new square: 25
..........Done with this set of values :-)..........
Storing the values read into the side of squares...
Displaying the new side of squares
Square 1: 7
Square 2: 8
Square 3: 9
Testing other member functions
Area of Square 1: 49
Perimeter of Square 2: 32
Square 1 and Square 3 are not equal
Area of (Square 2 + Square 3): 145
Area of (Square 3 - Square 1): 32
Creating a square double the size of Square 1
The side of the new square: 14
Creating a square of area equal to average area of Square 2 and Square 3
The side of the new square: 8
The area of the new square: 64
..........Done with this set of values :-)..........
Storing the values read into the side of squares...
Displaying the new side of squares
Square 1: 4
Square 2: 5
Square 3: 6
Testing other member functions
Area of Square 1: 16
Perimeter of Square 2: 20
Square 1 and Square 3 are not equal
Area of (Square 2 + Square 3): 61
Area of (Square 3 - Square 1): 20
Creating a square double the size of Square 1
The side of the new square: 8
Creating a square of area equal to average area of Square 2 and Square 3
The side of the new square: 5
The area of the new square: 25
..........Done with this set of values :-)..........
Press any key to continue
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.