File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change
1
+ // C++ program to demonstrate Encapsulation
2
+
3
+ // Encapsulation - Encapsulation is an object oriented programming concept that binds together the data and
4
+ // functions that manipulate the data.
5
+
6
+ #include < bits/stdc++.h>
7
+ using namespace std ;
8
+
9
+ // class 1 (binds data and functions for calculating area of a triangle)
10
+ class Triangle {
11
+ public:
12
+ int base,height;
13
+ void AreaOfTriangle (){
14
+ int AreaOfT=(base*height)/2 ;
15
+ cout<<" Area of triangle with base " <<base<<" and height " <<height<<" is " <<AreaOfT<<endl;
16
+ }
17
+ };
18
+
19
+ // class 2 (binds data and functions for calculating area of a rectangle)
20
+ class Rectangle {
21
+ public:
22
+ int length,breadth;
23
+ void AreaOfRectangle (){
24
+ int AreaOfR=length*breadth;
25
+ cout<<" Area of rectangle with length " <<length<<" and breadth " <<breadth<<" is " <<AreaOfR<<endl;
26
+ }
27
+ };
28
+
29
+ int main (){
30
+ // Create an object of class Triangle
31
+ Triangle t1;
32
+ t1.base =10 ;
33
+ t1.height =20 ;
34
+ t1.AreaOfTriangle ();
35
+
36
+ // Create an object of class Rectangle
37
+ Rectangle r1;
38
+ r1.length =10 ;
39
+ r1.breadth =20 ;
40
+ r1.AreaOfRectangle ();
41
+
42
+ return 0 ;
43
+ }
You can’t perform that action at this time.
0 commit comments