C++ Program to Explain Object-Oriented Programming Concept
Learn C++ step by step.
Explore blog under C++ - Object-Oriented Programming
Jan 23, 2026
## Chapter 11: Object-Oriented Programming
Until now, we have used C++ in a
procedural way.
Now we will use C++ in its
most powerful form: OOP
### What is OOP? (Simple Language)
OOP means:
Creating programs that
mimic real-world things
(objects)
Just like in the real world:
- Student
- Car
- Mobile
- Bank Account
Similarly, in a program,
we create objects.
**Two most important terms in OOP**
1. Class
A class is a blueprint/design.
For example:
A house plan
2. Object
An object is the
real thing created from that class.
For example:
A built house
The relationship between Class and Object
Plaintext
```
Class β Design
Object β Real Item
```
Many objects can be created
from a single class.
**Structure of a Class**
A class has two things:
1. Data (variables)
2. Behavior (functions)
**Example: Student Class**
C++
```
#include <iostream>
#include <string>
using namespace std;
// Creating a class
class Learner {
private:
string studentName;
int studentAge;
double score;
public:
// Constructor
Learner(string n, int a, double s) {
studentName = n;
studentAge = a;
score = s;
}
// Function to display information
void showProfile() {
cout << "Name: " << studentName
<< endl;
cout << "Age: " << studentAge <<
endl;
cout << "Score: " << score <<
endl;
}
// Behavior
void attendClass() {
cout << studentName << " is attending
class..." << endl;
}
// Getter
string getStudentName() {
return studentName;
}
// Setter
void updateScore(double newScore) {
if (newScore >= 0 && newScore <= 100) {
score = newScore;
}
}
};
int main() {
// Creating an object
Learner s1("Rahul", 19, 86.5);
Learner s2("Neha", 20, 91.0);
s1.showProfile();
s1.attendClass();
cout << endl;
s2.showProfile();
return 0;
}
```
Now let's understand this in simple terms:
**class Learner**
C++
```
class Learner
```
- This is a blueprint.
- Objects of students will be created from this.
**private section**
C++
```
private:
string studentName;
int studentAge;
double score;
```
- This data cannot be accessed directly from outside.
- This concept is called Encapsulation.
Protecting the data.
**public section**
Here you find:
- constructor
- methods
- getters / setters
This is what is accessed from the outside.
**What does the Constructor do?**
C++
```
Learner(string n, int a, double s)
```
- It is called automatically as soon as the object is
created.
- It gives initial values ββto the data.
For example:
Filling in details when a new student takes admission.
**Member Functions (Methods)**
C++
```
void showProfile()
```
- Shows the object's information.
C++
```
void attendClass()
```
- Shows the object's behavior.
**Why Getters and Setters?**
Getter:
C++
```
string getStudentName()
```
- To read private data in a safe way.
Setter:
C++
```
void updateScore(double newScore)
```
- To change the value with rules.
This prevents incorrect data from being entered.
**Creating an Object**
C++
```
Learner s1("Rahul", 19, 86.5);
```
- s1 is a real object.
- Created from the Learner class.
**Why is OOP important?**
| Advantage | Meaning |
| :--- | :--- |
| Security | Data remains safe |
| Reusability | Code can be reused |
| Clarity | Like the real world |
| Maintenance | Easy to change |
**Real-Life Examples**
- Bank Account class
- Game Player class
- Shopping Cart class
- Employee Management
**Beginner Tips**
- Name the class using a noun
- Name the function using a verb
- Keep data private
- Provide access through public methods
Remember in One Line
Class = design
Object = real thing
Private = data safety
Public = controlled access
**Core Concepts of OOP**
OOP is not limited to just classes and objects.
Its true power comes from its 4 core concepts.
These are often called the Four Pillars of OOP
The Four Pillars of OOP
1. Encapsulation
2. Inheritance
3. Polymorphism
4. Abstraction
Now let's understand them one by one, in simple language.
**1. Encapsulation**
What is Encapsulation?
Encapsulation means:
Binding together data and the methods
that operate on that data
and restricting direct access from the outside.
**Simple Example**
Imagine:
- An ATM machine has very complex logic inside
- But the user only sees:
- Withdrawing money
- Checking the balance
The internal logic is hidden.
There is controlled access from the outside.
This is Encapsulation.
How is Encapsulation achieved?
- Data members β private
- Access β public methods (getter/setter)
**Small Example**
C++
```
class Account {
private:
double balance;
public:
void setBalance(double amt) {
if (amt >= 0) {
balance = amt;
}
}
double getBalance() {
return balance;
}
};
```
No one can directly access the balance.
Access is only through methods.
**2. Inheritance**
What is Inheritance?
Inheritance means:
Creating a new class
from an existing class.
The new class:
- Automatically inherits the properties
- and methods
of the old class.
**Real-Life Example**
- Animal β Dog
- Vehicle β Car
- Employee β Manager
Dog:
- Has all the characteristics of an Animal
- Plus its own extra abilities
**Inheritance Example**
C++
```
#include <iostream>
#include <string>
using namespace std;
// Base class
class Vehicle {
protected:
string brand;
public:
Vehicle(string b) {
brand = b;
}
void start() {
cout << brand << " is starting."
<< endl;
}
};
// Derived class
class Bike : public Vehicle {
public:
Bike(string b) : Vehicle(b) {}
void ride() {
cout << brand << " bike is being
ridden." << endl;
}
};
int main() {
Bike myBike("Honda");
myBike.start(); // inherited
myBike.ride(); // own method
return 0;
}
```
What happened here?
- Bike inherited from Vehicle
- The start() method was automatically inherited
- ride() is Bike's own behavior
**3. Polymorphism**
What is Polymorphism?
Polymorphism means:
One name,
but different behavior
Simple Language
- The same function:
- on different objects
- works in different ways
**Real-Life Example**
βSpeakβ
- Human β speaks
- Dog β barks
- Cat β meows
(Understand the concept β details in later chapters)
C++
```
void sound() {
// Different output on different objects
}
```
This is polymorphism.
**4. Abstraction**
What is Abstraction?
Abstraction means:
Show the essential things,
hide the rest of the details
**Real-Life Example**
- While driving a car:
- Steering wheel
- Accelerator
- Brake
Knowing this much is enough.
How the engine works internally?
We don't need to know.
**How is Abstraction achieved?**
- Classes
- Interfaces
- (In C++: abstract classes & virtual functions)
**All four concepts at a glance**
| Concept | What it does |
| :--- | :--- |
| Encapsulation | Protects data |
| Inheritance | Reuses code |
| Polymorphism | Provides flexibility |
| Abstraction | Hides complexity |
**Beginner Tips**
- First understand Encapsulation
- Then Inheritance
- Polymorphism and Abstraction
will seem natural later
Remember in one line
Encapsulation β Safety
Inheritance β Reuse
Polymorphism β Flexibility
Abstraction β Simplicity
## Conclusion
In this article, we explored the core concepts of Explore blog under C++ - Object-Oriented Programming. Understanding these fundamentals is crucial for any developer looking to master this topic.
## Frequently Asked Questions (FAQs)
**Q: What is Explore blog under C++ - Object-Oriented Programming?**
A: Explore blog under C++ - Object-Oriented Programming is a fundamental concept in this programming language/topic that allows developers to perform specific tasks efficiently.
**Q: Why is Explore blog under C++ - Object-Oriented Programming important?**
A: It helps in organizing code, improving performance, and implementing complex logic in a structured way.
**Q: How to get started with Explore blog under C++ - Object-Oriented Programming?**
A: You can start by practicing the basic syntax and examples provided in this tutorial.
**Q: Are there any prerequisites for Explore blog under C++ - Object-Oriented Programming?**
A: Basic knowledge of programming logic and syntax is recommended.
**Q: Can Explore blog under C++ - Object-Oriented Programming be used in real-world projects?**
A: Yes, it is widely used in enterprise-level applications and software development.
**Q: Where can I find more examples of Explore blog under C++ - Object-Oriented Programming?**
A: You can check our blog section for more advanced tutorials and use cases.
**Q: Is Explore blog under C++ - Object-Oriented Programming suitable for beginners?**
A: Yes, our guide is designed to be beginner-friendly with clear explanations.
**Q: How does Explore blog under C++ - Object-Oriented Programming improve code quality?**
A: By providing a standardized way to handle logic, it makes code more readable and maintainable.
**Q: What are common mistakes when using Explore blog under C++ - Object-Oriented Programming?**
A: Common mistakes include incorrect syntax usage and not following best practices, which we've covered here.
**Q: Does this tutorial cover advanced Explore blog under C++ - Object-Oriented Programming?**
A: This article covers the essentials; stay tuned for our advanced series on this topic.