-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrectangle.cpp
63 lines (58 loc) · 1.25 KB
/
rectangle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <stdio.h>
#include <iostream>
using namespace std;
#include <stdio.h>
#include <iostream>
#include <stdexcept>
using namespace std;
class Rectangle {
private:
int width;
int height;
public:
/* default constructor*/
Rectangle() {
cout<<"default constructor 2x1\n";
//default is 2x1
width = 2;
height = 1;
}
/* overload set w and h */
Rectangle(int w,int h) {
cout<<"overload\n";
if(w<1 || h>=100) {
throw std::invalid_argument("1<= width, height<=100");
}
width = w;
height = h;
}
void set_width (int w) {
width = w;
}
void set_height (int h) {
height = h;
}
void set_input () {
std::cin >> height >> width;
}
void set_output () {
std::cout<< height<<"x"<< width<<"="<<height*width<<endl;
}
/* Evaluate Area */
int Area() {
return height*width;
}
};
/* Main */
int main()
{
cout<<"Rectangle hackerrank problem:"<<endl;
Rectangle r1(2,3);
Rectangle r2;
cout<<"R1 Area="<<r1.Area()<<endl;
cout<<"R2 Area="<<r2.Area()<<endl;
cout<<"set Input and Output: Enter w h"<<endl;
r2.set_input();
r2.set_output();
return 1;
}