Skip to content

Commit 7289b86

Browse files
Added the code for Abstraction in C++
1 parent 5a1620c commit 7289b86

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

OOPs/Abstraction/Abstraction_cpp.cpp

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

0 commit comments

Comments
 (0)