Kiran Borge

Mar 16, 2026 • 3 min read

C++ OOPS Programs and Concepts

Object Oriented Proagramming

Classes and Objects


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;
 }

Inheritance ?

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;
}

Types of Inheritance in C++

  1. Single Inheritance

A → B
  1. Multiple Inheritance

A
 \
 C
 /
B
  1. Multilevel Inheritance

A → B → C
  1. Hierarchical Inheritance

 A
 / \
 B C
  1. Hybrid Inheritance

Combination of different types.


Polymorphism ?

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 limit

Types of Polymorphism in C++

  1. Compile Time Polymorphism

    Also called:

    Function Overloading

    Same 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;
}
  1. Run Time Polymorphism

Also called:

Function Overriding

Here 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 ?

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.

Join Kiran on Peerlist!

Join amazing folks like Kiran and thousands of other builders on Peerlist.

peerlist.io/

It’s available... this username is available! 😃

Claim your username before it's too late!

This username is already taken, you’re a little late.😐

0

1

0