On Microsoft VIsual Studio C++ need it in a C++ code that works also use the way
ID: 3846569 • Letter: O
Question
On Microsoft VIsual Studio C++
need it in a C++ code that works also use the way that starts with hello world
Your code for this week shall meet the following requirements:
(10) Compiles
Create a class named Rectangle
(5) This class has three private attributes: length, width and units (length and width are floats and units is a string).
(10) For each attribute write a setter and a getter.
(10) Write two constructors for the Rectangle class: a default constructor and a
constructor that will accept three parameters. The default values are 0, 0, cm.
(10) Write a function called GetArea. This function will return the product of length times width.
(15) Write a program that demonstrates that all functions and constructors of your Rectangle class work properly.
Explanation / Answer
#include <iostream>
using namespace std;
class Rectangle
{
private:
float length,width;
string units;
public:
Rectangle()//default constructor
{
length = 0;
width = 0;
units = "cm";
}
Rectangle(float length,float width,string units)//argument constructor
{
this->length = length;
this->width = width;
this->units = units;
}
//set and get methods
void setLength(float length)
{
this->length = length;
}
float getLength()
{
return length;
}
void setWidth(float width)
{
this->width = width;
}
float getWidth()
{
return width;
}
void setUnits(string units)
{
this->units = units;
}
string getUnits()
{
return units;
}
float GetArea() //compute area
{
return length*width;
}
};
int main()
{
Rectangle r1(4.5,6.7,"km");
cout<<"Length of Rectangle = "<<r1.getLength()<<r1.getUnits();
cout<<" Width of Rectangle = "<<r1.getWidth()<<r1.getUnits();
cout<<" Area of Rectangle = "<<r1.GetArea()<<" square"<<r1.getUnits();
return 0;
}
output:
Length of Rectangle = 4.5Km
Width of Rectangle = 6.7Km
Area of Rectangle = 30.15 squarekm
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.