15. In this exercise, you modify the Rectangle class from Lab 15-2 it allows a p
ID: 3578072 • Letter: 1
Question
15. In this exercise, you modify the Rectangle class from Lab 15-2 it allows a program to view the contents of the length members. You also modify the Terney and width data length and width measurements. program so that it displays the a. If necessary, create a new project named Intermediatel5 Project and save it in the Cpp8 Chap15 folder. Copy the instructions from the Lab15-2 cpp file into a source file named Intermediatel5.cpp. Change the filename in the first comment. b. Copy the instructions from the Lab15-2 Rectangle.h file (which is located in either the Cpp8lChap15lLab15-2 Project folder or the Cpp8lChap15 folder) into a header file named Intermediate15 Rectangle.h file. Change the filename in the first comment. c Add two value-returning methods to the Rectangle class. Each method should return the value of one of the private variables. d. Modify the Terney Landscaping program so that it uses the methods to display the length and width of the Rectangle object. (The program should also display the area and total price.) Test the program appropriately.Explanation / Answer
//declaration and implementation file of Rectangle
//Rectangle.h
class Rectangle
{
private:
double length;
double width;
public:
Rectangle();
void setDimensions(double,double);
double calcArea();
double calcPerimenter();
//two value returing fucntions
//two getter methods for class
//private variables to return
double getLength() const;
double getWidth() const;
};
//class Rectangle implementation
Rectangle::Rectangle()
{
length=0;
width=0;
}
void Rectangle::setDimensions(double len, double wid)
{
if(len>0.0 && wid>0.0)
{
length=len;
width=wid;
}//end if
}
//returns area
double Rectangle::calcArea()
{
return length*width;
}
//return perimeter
double Rectangle::calcPerimenter()
{
return (length+width)*2;
}
//The method getLength that returns length of Rectangle
double Rectangle::getLength() const
{
return length;
}
//The method getWidth that returns width of Rectangle
double Rectangle::getWidth() const
{
return width;
}
--------------------------------------------
//lab15-2.cpp
//display the cost ofthe laying sod
#include<iostream>
#include <iomanip>
#include "Rectangle.h"
using namespace std;
int main()
{
Rectangle lawn;
//variables
double lawnLength=0;
double lawnWidth=0;
double priceSqYd=0;
double lawnArea=0;
double totalPrice=0;
//get length ,width and sod price
cout<< " Length ( in feet):";
cin>>lawnLength;
cout<< " Width ( in feet):";
cin>>lawnWidth;
cout<< " Sod price ( per square yard):";
cin>>priceSqYd;
lawn.setDimensions(lawnLength,lawnWidth);
//calculate area
lawnArea=lawn.calcArea();
//calculate total price
totalPrice=lawnArea*priceSqYd;
//display lenght and widht of the lawn
cout<<fixed<<setprecision(2)<<endl;
cout<< " Length ( in feet):"<<lawnLength<<endl;
cout<< " Width ( in feet):"<<lawnWidth<<endl;
cout<< " Sod price ( per square yard):";
//display area and total price
cout<<"Square yards: "<<lawnArea<<endl;
cout<<"Total Price: $ "<<totalPrice<<endl;
//pause program output on console
//utnil user press a key
system("pause");
return 0;
}
--------------------------------------------
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.