ómpatibility Mode]-Word Rectangle Class) 1. In a header file, create a class Rec
ID: 3723766 • Letter: #
Question
ómpatibility Mode]-Word Rectangle Class) 1. In a header file, create a class Rectangle with 2 private floating data members: length and width. 2. Use constructor that takes two parameters and uses them to initialize the data members. 3. Also, provide set and get functions for the length and width attributes. The set function should verify that the parameter is larger than 0.0 and less than 20.0. If value is verified, then set functions assigns the value to data member and return true. Other- wise, returns false. 4. In a separate file, write a test program (containing main method) that a. creates two Rectangle objects with different values for data members b. print the attributes (i.e. data members) of first objectExplanation / Answer
// Name this header file as rect.h
#include <iostream>
using namespace std;
class Rectangle
{
private:
float length, width;
public:
// Constructor to initialize values
Rectangle(float a, float b)
{
length =a;
width = b;
}
// to set value to length and width
bool setValue(float x, float y)
{
if ((x < 0.0)||(x > 20.0)||(y < 0.0)||(y > 20.0))
return false;
length = x;
width = y;
return true;
}
// to get length
float getLength()
{
return length;
}
// to get width
float getWidth()
{
return width;
}
};
// Put the code below in rect_main.cpp
#include<iostream>
#inlcude<rect.h>
int main()
{
Rectangle r1(0,0), r2(0,0); // Two rectangle objects
r1.setValue(3.0, 6.0); // setting value to 1st object
r2.setValue(9.0, 12.0); // setting value to 2nd object
cout<<" Rectangle length is: "<<r1.getLength(); //print length of 1st object
cout<<" Rectangle width is: "<<r1.getWidth()<<endl; // print width of 1st object
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.