File tree Expand file tree Collapse file tree 2 files changed +104
-0
lines changed Expand file tree Collapse file tree 2 files changed +104
-0
lines changed Original file line number Diff line number Diff line change
1
+ // C++ program to demonstrate Abstraction
2
+
3
+ // Abstraction - An abstraction is a way of providing only essential details to the outside world and hiding
4
+ // the implemetation details which are unnecessary.
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
+ private:
12
+ // User doesn't needs to know how area is being calculated
13
+ int base,height;
14
+ int AreaOfT;
15
+ void AreaOfTriangle (){
16
+ AreaOfT=(base*height)/2 ;
17
+ }
18
+ public:
19
+ // User only needs to give input and get the result
20
+ Triangle (int ibase,int iheight){
21
+ base=ibase;
22
+ height=iheight;
23
+ AreaOfTriangle ();
24
+ }
25
+ void displayT (){
26
+ cout<<" Area of triangle with base " <<base<<" and height " <<height<<" is " <<AreaOfT<<endl;
27
+ }
28
+ };
29
+
30
+ // class 2 (binds data and functions for calculating area of a rectangle)
31
+ class Rectangle {
32
+ private:
33
+ // User doesn't needs to know how area is being calculated
34
+ int length,breadth;
35
+ int AreaOfR;
36
+ void AreaOfRectangle (){
37
+ AreaOfR=length*breadth;
38
+ }
39
+ public:
40
+ // User only needs to give input and get the result
41
+ Rectangle (int ilength,int ibreadth){
42
+ length=ilength;
43
+ breadth=ibreadth;
44
+ AreaOfRectangle ();
45
+ }
46
+ void displayR (){
47
+ cout<<" Area of rectangle with length " <<length<<" and breadth " <<breadth<<" is " <<AreaOfR<<endl;
48
+ }
49
+ };
50
+
51
+ int main (){
52
+ // Create an object of class Triangle
53
+ Triangle t1 (10 ,20 );
54
+ t1.displayT ();
55
+
56
+ // Create an object of class Rectangle
57
+ Rectangle r1 (10 ,20 );
58
+ r1.displayR ();
59
+
60
+ return 0 ;
61
+ }
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