0% found this document useful (0 votes)
27 views

3 Day Data Abstraction C++

The document discusses key concepts in C++ including data abstraction, encapsulation, polymorphism, and virtual functions. It provides examples to illustrate each concept. Data abstraction hides unnecessary details and shows essential data to users. Encapsulation binds data and functions that manipulate the data, keeping them safe from outside interference. Polymorphism allows functions to be defined in different ways and is achieved through function overloading and virtual functions. Virtual functions allow derived classes to override base class functions.

Uploaded by

Himanshi Bobde
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

3 Day Data Abstraction C++

The document discusses key concepts in C++ including data abstraction, encapsulation, polymorphism, and virtual functions. It provides examples to illustrate each concept. Data abstraction hides unnecessary details and shows essential data to users. Encapsulation binds data and functions that manipulate the data, keeping them safe from outside interference. Polymorphism allows functions to be defined in different ways and is achieved through function overloading and virtual functions. Virtual functions allow derived classes to override base class functions.

Uploaded by

Himanshi Bobde
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Asterisc Computer Institute 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>

using namespace std;

class Bank

private:

intaccno,bal,profit,loss;

string name;

public:

void assign()

cout<<"Enter Account Number : ";

cin>>accno;

cout<<"\nEnter name : ";

cin>>name;

cout<<"\nEnter Balance : ";

cin>>bal;

cout<<"\nEnter Profit : ";

cin>>profit;

cout<<"\nLoss : ";

cin>>loss;

1 Chandrakant Sir (M) 7744822228/7743822228


Janaee Plaza 2nd Floor, Bhande Plot Sq., Umred Road, Nagpur 440024
Asterisc Computer Institute C++

voidclerck()

cout<<"\nAccount Number : "<<accno;

cout<<"\nName : "<<name;

cout<<"\nBalance : "<<bal;

void manager()

cout<<"\nAccount Number : "<<accno;

cout<<"\nName : "<<name;

cout<<"\nBalance : "<<bal;

cout<<"\nProfit : "<<profit;

cout<<"\nLoss : "<<loss;

};

int main()

Bank b;

b.assign();

cout<<"\n\nClerk can see : ";

b.clerck();

2 Chandrakant Sir (M) 7744822228/7743822228


Janaee Plaza 2nd Floor, Bhande Plot Sq., Umred Road, Nagpur 440024
Asterisc Computer Institute C++
cout<<"\n\nManage r can see : ";

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.

-Data encapsulation led to the important OOP concept of data hiding.

#include <iostream>

using namespace std;

class A

private :

int id;

string name;

doublesal;

public:

voidsetId(int id2)

id=id2;

3 Chandrakant Sir (M) 7744822228/7743822228


Janaee Plaza 2nd Floor, Bhande Plot Sq., Umred Road, Nagpur 440024
Asterisc Computer Institute C++

voidsetName(string name2)

name=name2;

voidsetSal(double sal2)

sal=sal2;

intgetId()

return id;

stringgetName()

return name;

doublegetSal()

returnsal;

};

int main()

4 Chandrakant Sir (M) 7744822228/7743822228


Janaee Plaza 2nd Floor, Bhande Plot Sq., Umred Road, Nagpur 440024
Asterisc Computer Institute C++
{

A a1,a2;

a1.setId(1);

a1.setName("Ashish");

a1.setSal(5000);

a2.setId(2);

a2.setName("Suraj");

a2.setSal(7000);

cout<<"\nFirst User : ";

cout<<"\nId : "<<a1.getId();

cout<<"\nName : "<<a1.getName();

cout<<"\nSalary : "<<a1.getSal();

cout<<"\n\nSecond User : ";

cout<<"\nId : "<<a2.getId();

cout<<"\nName : "<<a2.getName();

cout<<"\nSalary : "<<a2.getSal();

return 0;

5 Chandrakant Sir (M) 7744822228/7743822228


Janaee Plaza 2nd Floor, Bhande Plot Sq., Umred Road, Nagpur 440024
Asterisc Computer Institute C++

Difference between Encapsulation and abstraction.


Abstraction means hiding the internal details and just exposing the functionality. For
Example:- When user is using send SMS application he doesn't know internal working of
send SMS function but he is using that functionality without knowing internal details.

Encapsulation is wrapping, just hiding properties and methods. Encapsulation is used


for hide the code and data in a single unit to protect the data from the outside the world.
Class is the best example of encapsulation.

Thus encapsulation is hiding data and abstraction is hiding implementation.

Polymorphism

#include <iostream>
usingnamespacestd;
classcompile
{
public:

// function with 1 int parameter


voidfunc(intx)
{
cout<< "value of x is "<< x <<endl;
}

// function with same name but 1 double parameter


voidfunc(doublex)
{
cout<< "value of x is "<< x <<endl;
}

// function with same name and 2 int parameters


voidfunc(intx, inty)
{
cout<< "value of x and y is "<< x << ", "<< y <<endl;
}
};

intmain()
{
compile obj1;

// Which function is called will depend on the parameters passed


// The first 'func' is called
obj1.func(7);

// The second 'func' is called


obj1.func(9.132);

6 Chandrakant Sir (M) 7744822228/7743822228


Janaee Plaza 2nd Floor, Bhande Plot Sq., Umred Road, Nagpur 440024
Asterisc Computer Institute C++
// The third 'func' is called
obj1.func(85,64);
return0;

Polymorphism is by far the most important and widely used concept in object oriented

programming. Polymorphism is amechanism that allows you to implement a function in

different ways.

Real life example of polymorphism, a person at a same time can have different

characteristic. Like a man at a same time is a father, a husband, a employee. So a same

person posses have different behavior in different situations. This is called

polymorphism.

In C++ polymorphism is mainly divided into two types:


 Compile time Polymorphism
 Runtime Polymorphism

Compile time polymorphism: This type of polymorphism is achieved by function


overloading or operator overloading.
 Function Overloading: When there are multiple functions with same name but
different parameters then these functions are said to be overloaded. Functions
can be overloaded by change in number of arguments or/and change in type
of arguments.

ExampleofPolymorphism
Virtual Function

#include <iostream>

using namespace std;

class A

public:

virtual void display(int x)

7 Chandrakant Sir (M) 7744822228/7743822228


Janaee Plaza 2nd Floor, Bhande Plot Sq., Umred Road, Nagpur 440024
Asterisc Computer Institute C++
cout<<"\nValue of x : "<<x;

};

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;

8 Chandrakant Sir (M) 7744822228/7743822228


Janaee Plaza 2nd Floor, Bhande Plot Sq., Umred Road, Nagpur 440024
Asterisc Computer Institute C++
B b;

C c;

a1=&a;

a1->display(10);

a1=&b;

a1->display(10);

a1=&c;

a1->display(10);

return 0;

9 Chandrakant Sir (M) 7744822228/7743822228


Janaee Plaza 2nd Floor, Bhande Plot Sq., Umred Road, Nagpur 440024

You might also like