Skip to content

Commit f5444c7

Browse files
authored
Merge pull request #865 from pushpakumari5117/issue-864
Added the code for Encapsulation in C++
2 parents 328809f + 4176c7f commit f5444c7

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
}

0 commit comments

Comments
 (0)