Convert this UML in slide 78 of Ch 13 to C++ code and add two constructors; A de
ID: 3734761 • Letter: C
Question
Convert this UML in slide 78 of Ch 13 to C++ code and add two constructors;
A default constructor, and another constructor that sets length and width with some values.
In the main() function:
Create 2 objects and use/call all the methods implemented.
rect1 should use the default constructor to assign 0 to the attributes.
rect2 should use the non-default constructor.
Separate the declaration from implementation.
submit 2 files:
rectangle.h
rectangle.cpp
Explanation / Answer
Rectangle.h
#ifndef RECTANGLE_H
#define RECTANGLE_H
class Rectangle
{
public:
Rectangle();
Rectangle(double width,double length);
void setLength(double len);
void setWidth(double w);
double getWidth();
double getLength();
private:
//Declaring instance variables
double width;
double length;
};
#endif
__________________
Rectangle.cpp
#include <iostream>
using namespace std;
#include "Rectangle.h"
Rectangle::Rectangle()
{
this->width=0;
this->length=0;
}
//Parameterized constructor
Rectangle::Rectangle(double width,double length)
{
this->width=width;
this->length=length;
}
// getters and setters
void Rectangle::setLength(double len)
{
this->length=len;
}
void Rectangle::setWidth(double w)
{
this->width=w;
}
double Rectangle::getWidth()
{
return width;
}
double Rectangle::getLength()
{
return length;
}
____________________
Main.cpp
#include <iostream>
using namespace std;
#include "Rectangle.h"
int main(int argc, char **argv)
{
//Creating an Instance of Rectangle class
Rectangle rect1;
rect1.setLength(6);
rect1.setWidth(5);
//Creating an Instance of Rectangle class
Rectangle rect2(2,10);
//Displaying the output
cout<<" Rectangle#1"<<endl;
cout<<"Length :"<<rect1.getLength()<<endl;
cout<<"Width :"<<rect1.getWidth()<<endl;
cout<<" Rectangle#2"<<endl;
cout<<"Length :"<<rect2.getLength()<<endl;
cout<<"Width :"<<rect2.getWidth()<<endl;
return 0;
}
_________________Thank You