Question

Need help building this C++ program For your program you will create a class that represents...

Need help building this C++ program

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 a square of side equal to the side of the square multiplied by a factor square increaseBy(int factor);

// 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. Run my solution and pay attention to the output to determine what function is tested at each stage of it. It is your responsibility to understand the requirements specified on this handout; if you have doubts about certain aspects of the problem then you must ask the designer of the class (me) for more details. Of course, your questions should be as specific as possible. You may also consult with other programmers (classmates) through Blackboard.

Read the documentation above carefully to understand how the members of the class must be implemented and used. If you find inconsistencies or have some questions please post them on the corresponding topic of the discussion board. I will first wait for somebody to reply and then will provide my answer.

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 Creating a square double the size of Square 1 The side 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: 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 Creating a square double the size of Square 1 The side of the new square: 6

..........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 Creating a square double the size of Square 1 The side of the new square: 8

..........Done with this set of values :-)..........

Press any key to continue . . .

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// square.h

#ifndef SQUARE_H_
#define SQUARE_H_

class square
{
private:
   int side;
public:
   square(int s=0);
   void setSide(int s);
   int getSide() const;
   int plus(const square &sq);
   square increaseBy(int factor) const;
   bool equalTo(const square &sq) const;
   int area() const;
   int perimeter() const;
};


square::square(int s) : side(s)
{}

void square::setSide(int s)
{
   side = s;
}

int square::getSide() const
{
   return side;
}

int square::plus(const square &sq)
{
   return(area()+sq.area());
}

square square:: increaseBy(int factor) const
{
   return square(side*factor);
}

bool square:: equalTo(const square &sq) const
{
   return (side == sq.side);
}

int square:: area() const
{
   return side*side;
}

int square:: perimeter() const
{
   return 4*side;
}

#endif
//end of square.h


// main.cpp : C++ client program to test the square class
#include "square.h"
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
   square s1,s2,s3(10);
   cout<<"Before assigning values to the squares their sides are: Square 1: "<<s1.getSide()<<" Square 2: "<<s2.getSide()<<" Square 3: "<<s3.getSide()<<endl;
   ifstream fin("input.txt"); // provide full path to file
   int side;
   // if file can be opened
   if(fin.is_open())
   {
       cout<<"............Starting to process the file............"<<endl;
       // read till the end of file
       while(!fin.eof())
       {
           cout<<"Storing the values read into the side of squares..."<<endl;
           fin>>side;
           s1.setSide(side);
           fin>>side;
           s2.setSide(side);
           fin>>side;
           s3.setSide(side);

           cout<<"Displaying the new side of squares Square 1: "<<s1.getSide()<<" Square 2: "<<s2.getSide()<<" Square 3: "<<s3.getSide()<<endl;
           cout<<"Testing other member functions"<<endl;
           cout<<"Area of Square 1: "<<s1.area()<<endl;
           cout<<"Perimeter of Square 2: "<<s2.perimeter()<<endl;
           if(s1.equalTo(s3))
               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): "<<s2.plus(s3)<<endl;
           cout<<"Creating a square double the size of Square 1 "<<endl;
           square sq = s1.increaseBy(2);
           cout<<"The side of the new square: "<<sq.getSide()<<endl;

           cout<<"..........Done with this set of values :-).........."<<endl<<endl;
       }

       fin.close(); // close the file
   }else
       cout<<"Unable to open file: input.txt"<<endl;

   return 0;
}
//end of program

Output:

Input file :

Format of the input file:

Each line of the input file contains 3 values for side of 3 squares which are separated by a single space or tab

Output:

Add a comment
Know the answer?
Add Answer to:
Need help building this C++ program For your program you will create a class that represents...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • For this lab you will be creating a class representing the shape square. A Square is...

    For this lab you will be creating a class representing the shape square. A Square is a special case of a Rectangle where both sides have the same length. Rectangle has been provided for your use in this lab. A Rectangle has a height and a width as member variables, two constructors, two member functions, area() and perimeter(), and lastly, an input and output operator. Your Square class should publicly inherit from Rectangle. You will need to update the Constructors...

  • •Work in groups of two.   You will write a program that will calculate the area of...

    •Work in groups of two.   You will write a program that will calculate the area of a square based on a randomly generated integer to be used as the side. •Person 1 –write the main function which will: 1.Generate a random integer between 1 and 100 which will be the side of the square 2.Pass that number to called functions called square_area and square_perimeter. 3.Display the side, the returned area, and the returned perimeter. •Person 2 – in a separate...

  • (i) Create a class square with attribute (integer) length which defaults to 5. Provide member functions...

    (i) Create a class square with attribute (integer) length which defaults to 5. Provide member functions that calculate the perimeter (4 * length) and the area (length*length) of the triangle. Also provide set and get functions for the length attribute. The set function should verify that length is between 0.0 to 100. Your class should also provide a display function that draws the aquare using * character. For example, if length = 5, the display function should draw             *****...

  • Please help with this assignment. Thanks. 1. Write a C++ program containing a class Invoice and...

    Please help with this assignment. Thanks. 1. Write a C++ program containing a class Invoice and a driver program called invoiceDriver.cpp. The class Invoice is used in a hardware store to represent an invoice for an item sold at the store. An invoice class should include the following: A part number of type string A part description of type string A quantity of the item being purchased of type int A price per item of type int A class constructor...

  • Write a program in C++ that simulates a soft drink machine. The program will need several...

    Write a program in C++ that simulates a soft drink machine. The program will need several classes: DrinkItem, DrinkMachine and Receipt. For each of the classes you must create the constructors and member functions required below. You can, optionally, add additional private member functions that can be used for doing implementation work within the class. DrinkItem class The DrinkItem class will contains the following private data members: name: Drink name (type of drink – read in from a file). Type...

  • Creating a Class in C++ Summary In this lab, you create a programmer-defined class and then...

    Creating a Class in C++ Summary In this lab, you create a programmer-defined class and then use it in a C++ program. The program should create two Rectangle objects and find their area and perimeter. Instructions Ensure the class file named Rectangle.cpp is open in your editor. In the Rectangle class, create two private attributes named length and width. Bothlength and width should be data type double. Write public set methods to set the values for lengthand width. Write public...

  • Q2) Interface Create a program that calculates the perimeter and the area of any given 2D...

    Q2) Interface Create a program that calculates the perimeter and the area of any given 2D shape. The program should dynamically assign the appropriate calculation for the given shape. The type of shapes are the following: • Quadrilateral 0 Square . Perimeter: 4xL • Area:LXL O Rectangle • Perimeter: 2(L+W) • Area:LxW Circle Circumference: I x Diameter (TT = 3.14) Area: (TT xD')/4 Triangle (assume right triangle) o Perimeter: a+b+c O Area: 0.5 x base x height (hint: the base...

  • I need help with my homework please. I should write this program in C++ language and...

    I need help with my homework please. I should write this program in C++ language and use Inventory.h file (header file), Inventory.cpp file (implementation file), and main.cpp file (main program). This is the program: Demonstrate the class in a driver program. Input validation, don’t accept negative values for item number, quantity, or cost. 6. Inventory Class Design an Inventory class that can hold information and calculate data for items ma retail store's inventory. The class should have the following private...

  • in C++ use Inheritance for the following Shape Circle Sphere Cylinder Rectangle Square Cube You must define one class for each shape. And, use public inheritance when deriving from base cla...

    in C++ use Inheritance for the following Shape Circle Sphere Cylinder Rectangle Square Cube You must define one class for each shape. And, use public inheritance when deriving from base classes. Circle int r; void area(); void perimeter(); void volume(); //No volume for circle Sphere int r; void area();//Sphere surface area= 4 × pi × radius2 void perimeter(); //No perimeter for Sphere void volume();//Sphere volume= 4/3 × pi × radius2 Cylinder int r, height; void area();// Cylinder surface area=perimeter of...

  • The program must be in C# syntax. Write the definition for a generic class called Rectangle...

    The program must be in C# syntax. Write the definition for a generic class called Rectangle that has data members length and width. The class has the following member functions: setlength to set the length data member setwidth to set the width data member perimeter to calculate and return the perimeter of the rectangle area to calculate and return the area of the rectangle show to return a text to display the length and width of the rectangle sameArea that...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT