-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntro OOP1.cpp
97 lines (77 loc) · 2.09 KB
/
Intro OOP1.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
[PROGRAM] : Object Oriented Programming
[AUTHOR] : Saddam Arbaa
[Email] : <saddamarbaas@gmail.com>
Object Oriented Programming with C++ - Introduction
*/
#include<iostream>
using namespace std;
// Create Rectangle class with some attributes
class Rectangle {
// Access specifier
public:
// Attributes
float length, width;
// Constructor with parameters
Rectangle(float len, float wid)
{
length = len;
width = wid;
}
// Access specifier(public member function )
public:
// function to set length of Rectangle
void setLength(float length)
{
if(length >= 0)
length = length;
else
cout << "Please Enter only positive values" << endl;
}
// function to set width of Rectangle
void setWidth(float width)
{
if(width >= 0)
width = width;
else
cout << "Please Enter only positive values" << endl;
}
// function to get(Return) length of Rectangle
float getLenght()
{
return length;
}
// function to get(Return) width of Rectangle
float getWidth()
{
return width;
}
// function to get(Return) Area of Rectangle
float getArea()
{
return length * width;
}
// function to display the Area of Rectangle
void display()
{
cout << "Area of rectangle is : " << length * width <<endl;
}
};
int main()
{
// Create an object of a Rectangle
Rectangle box1(40.5, 30);
// Print values
cout << "the length " << box1.getLenght() << endl;
cout << "the width " << box1.getWidth() << endl;
// Print the area
box1.display();
// Create another object of a Rectangle
Rectangle box2(12.5, 12.5);
// Print values
cout << "the length " << box2.getLenght() << endl;
cout << "the width " << box2.getWidth() << endl;
// Print the area
box2.display();
return 0;
}