Question

C++ program.

Modify example, Program 13-1 from the textbook, by adding a data member, color (use type string) and 3 member functions, setC

int main()
{
   Rectangle box;     // Define an instance of the Rectangle class
   double rectWidth; // Local variable for width
   double rectLength; // Local variable for length
   string rectColor;

   // Get the rectangle's width and length from the user.
   cout << "This program will calculate the area of a\n";
   cout << "rectangle. What is the width? ";
   cin >> rectWidth;
   cout << "What is the length? ";
   cin >> rectLength;
   cout << "What is the color? ";
   cin >> rectColor;

   // Store the width and length of the rectangle
   // in the box object.
   box.setWidth(rectWidth);
   box.setLength(rectLength);
   box.setColor(rectColor);

   // Display the rectangle's data.
   cout << "Here is the rectangle's data:\n";
   cout << "Width: " << box.getWidth() << endl;
   cout << "Length: " << box.getLength() << endl;
   cout << "Color: " << box.getColor() << endl;
   cout << "Area: " << box.getArea() << endl;
   cout << "Perimeter: " << box.getPerimeter() << endl;
   return 0;

}
}Sample output This program will calculate the area of a rectangle. What is the width? 4 What is the length? 5 What is the col

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

Code:

#include<iostream>
using namespace std;

class Rectangle
{
   public:
       double rectLength; //length of rectangle
       double rectWidth; //breadth of rectangle
       string rectColor;
       /* declaring member functions */
       void setLength(double rectLength);
       void setWidth(double rectWidth);
       void setColor(string rectColor);
       double getLength();
       double getWidth();
       string getColor();
       double getArea();
       double getPerimeter();
};

void Rectangle::setLength(double length)
{
   rectLength = length;
}
void Rectangle::setWidth(double width )
{
   rectWidth = width;
}

void Rectangle::setColor(string color )
{
   rectColor = color;
}

double Rectangle::getLength()
{
   return rectLength;
}
double Rectangle::getWidth()
{
   return rectWidth;
}

string Rectangle::getColor()
{
   return rectColor;
}

double Rectangle::getArea()
{
   return rectWidth*rectLength;
}

double Rectangle::getPerimeter()
{
   return 2*(rectWidth+rectLength);
}


int main()
{
Rectangle box; // Define an instance of the Rectangle class
double rectWidth; // Local variable for width
double rectLength; // Local variable for length
string rectColor;

// Get the rectangle's width and length from the user.
cout << "This program will calculate the area of a\n";
cout << "rectangle. What is the width? ";
cin >> rectWidth;
cout << "What is the length? ";
cin >> rectLength;
cout << "What is the color? ";
cin >> rectColor;

// Store the width and length of the rectangle
// in the box object.
box.setWidth(rectWidth);
box.setLength(rectLength);
box.setColor(rectColor);

// Display the rectangle's data.
cout << "Here is the rectangle's data:\n";
cout << "Width: " << box.getWidth() << endl;
cout << "Length: " << box.getLength() << endl;
cout << "Color: " << box.getColor() << endl;
cout << "Area: " << box.getArea() << endl;
cout << "Perimeter: " << box.getPerimeter() << endl;
return 0;

}

Output:

This program will calculate the area of a rectangle. What is the width? 2 What is the length? 2 What is the color? Red Here i

Add a comment
Know the answer?
Add Answer to:
C++ program. int main() {    Rectangle box;     // Define an instance of the Rectangle class...
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
  • Hello I have a question. I would like to check if the code follows this requirement....

    Hello I have a question. I would like to check if the code follows this requirement. Rectangle.h - A complete Rectangle Class including both declaration and definition appRectangle,cpp separate in two different files the class definition and implementation Code: rectangle.h // Rectangle class declaration. class Rectangle { private: double width; double length; public: void setWidth(double); void setLength(double); double getWidth() const; double getLength() const; double getArea() const; }; //************************************************** // setWidth assigns a value to the width member. * //************************************************** void...

  • 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...

  • Pure Abstract Base Class Project. Define a class called BasicShape which will be a pure abstract class. The clas...

    Pure Abstract Base Class Project. Define a class called BasicShape which will be a pure abstract class. The class will have one protected data member that will be a double called area. It will provide a function called getArea which should return the value of the data member area. It will also provide a function called calcArea which must be a pure virtual function. Define a class called Circle. It should be a derived class of the BasicShape class. This...

  • 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...

  • 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...

  • Why is my program returning 0 for the area of a triangle? public class GeometricObjects {...

    Why is my program returning 0 for the area of a triangle? public class GeometricObjects {    private String color = " white ";     private boolean filled;     private java.util.Date dateCreated;     public GeometricObjects() {         dateCreated = new java.util.Date();     }     public GeometricObjects(String color, boolean filled) {         dateCreated = new java.util.Date();         this.color = color;         this.filled = filled;     }     public String getColor() {         return color;     }     public void setColor(String color)...

  • In the code (C++) below, if you input 2 as length and 4 as width, the...

    In the code (C++) below, if you input 2 as length and 4 as width, the output is: Enter the rectangle's length: 2 Enter the rectangle's width: 4 Length: 2 | Width: 4 | Area: 8 | Perimeter:12 We worked on this code during class, but there were some things that I did not understand. 1) how does compiler read"l" as length, "w" as width, etc.? I thought you had to update it first, like "w=width." 2) i thought you...

  • please use c++ Write a program that contains a class Rectangle with two private double precision...

    please use c++ Write a program that contains a class Rectangle with two private double precision members iLength and iWidth, public set and get member functions for these two members, a two argument constructor that sets the length and width to any two user specified values and a void function area() that computes the area and then prints this value by inserting it into the cout output stream. Write a main function that ereates a Rectangle with a length of...

  • Construct a C++ class named Rectangle that has floating-point data members named length and width.  The class...

    Construct a C++ class named Rectangle that has floating-point data members named length and width.  The class should have a zero-argument constructor that initializes each data member to 0. It should have member functions named calcPerimeter() and calcArea() that calculate the perimeter and area of a rectangle respectively, a member function setLength() and setWidth() to set the length and width, member functions getLength() and getWidth() to return the length and width, and a member function showData() that displays the rectangle’s length,...

  • Find two syntax errors in the following program: #include <iostream> using namespace std; int area(int length,...

    Find two syntax errors in the following program: #include <iostream> using namespace std; int area(int length, int width = 0); int main() { int length, width; // for rectangle use both arguments cout << "Enter length and width of a rectangle" << endl; cin >> length >> width; cout << "The area is " << area(length, width) << endl; // for square, only need first argument cout << "Enter side of a square" << endl; cin >> length; cout <<...

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
ADVERTISEMENT