-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstructor_destructor_inheritance.cpp
More file actions
59 lines (59 loc) · 1.02 KB
/
Copy pathconstructor_destructor_inheritance.cpp
File metadata and controls
59 lines (59 loc) · 1.02 KB
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
#include <iostream>
using namespace std;
class Rectangle {
private:
int length;
int width;
public:
Rectangle() {
length = 0;
width = 0;
cout << "Default constructor called!" << endl;
}
Rectangle(int side) {
length = side;
width = side;
cout << "Square constructor called!" << endl;
}
Rectangle(int l, int w) {
length = l;
width = w;
cout << "Rectangle constructor called!" << endl;
}
void display() {
cout << "Length: " << length << ", Width: " << width << endl;
}
};
int main() {
int choice;
cout << "Choose how to create the rectangle:\n";
cout << "1. Default (0x0)\n";
cout << "2. Square (one side)\n";
cout << "3. Rectangle (length & width)\n";
cout << "Enter choice: ";
cin >> choice;
if (choice == 1) {
Rectangle r1;
r1.display();
}
else if (choice == 2) {
int side;
cout << "Enter side length: ";
cin >> side;
Rectangle r2(side);
r2.display();
}
else if (choice == 3) {
int l, w;
cout << "Enter length: ";
cin >> l;
cout << "Enter width: ";
cin >> w;
Rectangle r3(l, w);
r3.display();
}
else {
cout << "Invalid choice!\n";
}
return 0;
}