Object Oriented Proagramming
Atm example for class , classes and encapsulation
// Atm example for class and objects
#include <iostream> //input and output
#include <string>
using namespace std; // without writing ::std
class Atm // class name
{
private: // access modifier - Can be accessed only class
int AtmPin;
public: // access modifier - Can be accessed from anywhere
string name;
long long AtmNumber; // big digit data type ... long long
//setter
void setPin(int p)
{
AtmPin = p;
}
//getter
void getPin()
{
return AtmPin;
}
};
int main()
{
Atm acnum; // obj2 = acnum
acnum.AtmNumber = 235435234142;
acnum.setPin(4565);
cout << acnum.getPin() << endl;
cout<< "Your acccout name: " << acnum.AtmNumber; // print obj function
// return 0;
}When a class derives from another class is called Inheritance. Inheritance is a feature or a process in which, new classes are created form the existing classes. The new class created is called 'derived class' or 'child class' and the existing class is known as the 'base class' or 'parent class'.
One class depends on another class. Reusability - Many time used code.
Now different types of accounts exist:
Savings Account
Current Account
Both reuse the properties of Account class.
class ChildClass : access_specifier ParentClass
{
};class SavingsAccount : public Account#include <iostream>
using namespace std;
class Animal // base class (parent class)
{
public:
void eat()
{
cout << "Animal is eating" << endl;
}
};
class Dog : public Animal // Child Class
{
public:
void bark()
{
cout << "Dog is barking" << endl;
}
};
int main()
{
Dog d; // Object Creation
d.eat();
d.bark();
return 0;
}Single Inheritance
A → BMultiple Inheritance
A
\
C
/
BMultilevel Inheritance
A → B → CHierarchical Inheritance
A
/ \
B CHybrid Inheritance
Combination of different types.
One name, many forms.
One function can behave differently in different situations.
Polymorphism is an OOP concept where the same function name can perform different tasks depending on the object or parameters.
Real Life Example (ATM Machine) - Think about Withdraw in ATM.
Withdraw works differently for:
Savings Account
Current Account
Example:
Savings Account → withdraw with limit
Current Account → withdraw without limitCompile Time Polymorphism
Also called:
Function OverloadingSame function name but different parameters.
#include <iostream>
using namespace std;
class Calculator
{
public:
int add(int a, int b)
{
return a + b;
}
int add(int a, int b, int c)
{
return a + b + c;
}
};
int main()
{
Calculator c;
cout << c.add(5, 3) << endl;
cout << c.add(2, 3, 4);
return 0;
}Run Time Polymorphism
Also called:
Function OverridingHere child class changes behavior of parent function.
#include <iostream>
using namespace std;
class Account
{
public:
void withdraw()
{
cout << "Normal withdrawal" << endl;
}
};
class SavingsAccount : public Account
{
public:
void withdraw()
{
cout << "Savings withdrawal with limit" << endl;
}
};
int main()
{
SavingsAccount acc;
acc.withdraw();
return 0;
}Abstraction means displaying only essential information and hiding the details. Data abstraction refers to providing only essential information about the data to the outside world, hiding the background details or implementation.
Example : code same we work with access modifier eg. public, private and protected
Website - only (UI) Front End part visible from the user and all backend part hide from the user.

0
1
0