c++
* Be sure to add your name as a cout in the first lines of each program – else 0 credit.
* Add constructors – a default and parameterized constructor to each.
* Write an .h interface and a .cpp implementation for each class
* Write an Drive/Test file that tests the constructors and functions
* Write a UML class diagram for each class
Program 5 Rectangle – Complete the following code
then run it – Produce the correct output and Turn it in for credit
// classes example #include <iostream> using namespace std; class Rectangle { int width, height; public: void set_values (int,int); int area() { int answer; // complete this function so the code works return answer; } }; void Rectangle::set_values (int x, int y) { // complete this function so the code works } int main () { // Use this driver program
// Use set_values function to set values Rectangle rect1; rect1.set_values (5,6); cout << "area: " << rect1.area() << endl;
// Use set_values function to set values Rectangle rect2; rect2.set_values (3,4); cout << "area: " << rect2.area() << endl; return 0; }
————————————————————-
* Be sure to add your name as a cout in the first lines of each program – else 0 credit.
* Add constructors – a default and parameterized constructor to each.
* Write an .h interface and a .cpp implementation for each class
* Write an Drive/Test file that tests the constructors and functions
* Write a UML class diagram for each class
Expert Answer
#ifndef RECTANGLE_H
#define RECTANGLE_H
class Rectangle
{
private:
int width, height;
public:
Rectangle();
Rectangle(int,int);
void set_values (int,int);
int area();
};
#endif // RECTANGLE_H
#include “Rectangle.h”
Rectangle::Rectangle()
{
//ctor
this->height=0;
this->width=0;
}
Rectangle::Rectangle(int w,int h)
{
this->height=h;
this->width=w;
}
void Rectangle::set_values (int h,int w)
{
this->height=h;
this->width=w;
}
int Rectangle::area()
{
return (this->height*this->width);
}
#include <iostream>
#include “Rectangle.h”
using namespace std;
int main()
{
// Use set_values function to set values
Rectangle rect1;
rect1.set_values (5,6);
cout << “area: ” << rect1.area() << endl;
// Use set_values function to set values
Rectangle rect2;
rect2.set_values (3,4);
cout << “area: ” << rect2.area() << endl;
return 0;
}
OUTPUT:
UML CLASS: