3 Day Data Abstraction C++
3 Day Data Abstraction C++
Data Abstraction
Data Abstraction is a process or technique to hiding unnecessary or confidential data and
showing any essential data to user.
#include <iostream>
class Bank
private:
intaccno,bal,profit,loss;
string name;
public:
void assign()
cin>>accno;
cin>>name;
cin>>bal;
cin>>profit;
cout<<"\nLoss : ";
cin>>loss;
voidclerck()
cout<<"\nName : "<<name;
cout<<"\nBalance : "<<bal;
void manager()
cout<<"\nName : "<<name;
cout<<"\nBalance : "<<bal;
cout<<"\nProfit : "<<profit;
cout<<"\nLoss : "<<loss;
};
int main()
Bank b;
b.assign();
b.clerck();
b.manager();
return 0;
DataEncapsulation
Encapsulation is an Object Oriented Programming concept that binds together the data
and functions that manipulate the data, and that keeps both safe from outside
interference and misuse.
#include <iostream>
class A
private :
int id;
string name;
doublesal;
public:
voidsetId(int id2)
id=id2;
voidsetName(string name2)
name=name2;
voidsetSal(double sal2)
sal=sal2;
intgetId()
return id;
stringgetName()
return name;
doublegetSal()
returnsal;
};
int main()
A a1,a2;
a1.setId(1);
a1.setName("Ashish");
a1.setSal(5000);
a2.setId(2);
a2.setName("Suraj");
a2.setSal(7000);
cout<<"\nId : "<<a1.getId();
cout<<"\nName : "<<a1.getName();
cout<<"\nSalary : "<<a1.getSal();
cout<<"\nId : "<<a2.getId();
cout<<"\nName : "<<a2.getName();
cout<<"\nSalary : "<<a2.getSal();
return 0;
Polymorphism
#include <iostream>
usingnamespacestd;
classcompile
{
public:
intmain()
{
compile obj1;
Polymorphism is by far the most important and widely used concept in object oriented
different ways.
Real life example of polymorphism, a person at a same time can have different
polymorphism.
ExampleofPolymorphism
Virtual Function
#include <iostream>
class A
public:
};
class B:public A
public:
void display(int x)
cout<<"\nSquere of x : "<<x*x;
};
class C:public B
public:
void display(int x)
cout<<"\nCube of x : "<<x*x*x;
};
int main()
A *a1;
A a;
C c;
a1=&a;
a1->display(10);
a1=&b;
a1->display(10);
a1=&c;
a1->display(10);
return 0;