C++ question. Must use #include <iostream> and "using namespace std;" You must also have only a .cpp and .h file
Define a class called Rectangle that can store the height and width of a rectangle as instance variables, has a constructor that allows you to set the height and width when you create a Rectangle, a default constructor that sets both height and width to 0, and functions to return the following values:
* the perimeter [ which should be 2 * (height + width)]
* the area [which should be height * width]
C++ CODE:
Rectangle.h file code:
class Rectangle{
int height;
int width;
public:
Rectangle(int x=0,int y=0){
height = x;
width = y;
}
int perimeter(){
return 2*(height+width);
}
int area(){
return height*width;
}
};
Main Program CODE:
#include <iostream>
#include "rectangle.h"
using namespace std;
int main() {
Rectangle rec(5,4);
cout<<rec.perimeter()<<'\n';
cout<<rec.area()<<'\n';
return 0;
}
CODE SCREENSHOT:
Get Answers For Free
Most questions answered within 1 hours.