0% found this document useful (0 votes)
7 views4 pages

Complete OOP Concepts in CPP

The document provides a comprehensive guide to Object-Oriented Programming (OOP) concepts in C++, focusing on its four key principles: encapsulation, abstraction, inheritance, and polymorphism. It explains the difference between classes and objects, the implementation of encapsulation using access specifiers, the roles of constructors and destructors, and the purpose of access specifiers in controlling member visibility. Various code examples illustrate these concepts effectively.

Uploaded by

joelvkoshy7.ix
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

Complete OOP Concepts in CPP

The document provides a comprehensive guide to Object-Oriented Programming (OOP) concepts in C++, focusing on its four key principles: encapsulation, abstraction, inheritance, and polymorphism. It explains the difference between classes and objects, the implementation of encapsulation using access specifiers, the roles of constructors and destructors, and the purpose of access specifiers in controlling member visibility. Various code examples illustrate these concepts effectively.

Uploaded by

joelvkoshy7.ix
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Comprehensive Guide to OOP Concepts in C++

1. Four Pillars of Object-Oriented Programming (OOP)

Object-Oriented Programming (OOP) is based on four key principles that define how objects and
classes interact with each other. These principles are:

- **Encapsulation**: This principle involves bundling data (variables) and methods (functions) that
operate on that data into a single unit, a class. It also prevents direct access to some data,
restricting it through access specifiers like `private`, `public`, and `protected`.

**Example:**
```cpp
class Employee {
private:
int salary;
public:
void setSalary(int s) { salary = s; }
int getSalary() { return salary; }
};
```

- **Abstraction**: This principle hides the complex implementation details from the user and exposes
only the necessary functionalities. It helps in reducing complexity.

- **Inheritance**: This principle allows a new class (derived class) to acquire properties and behavior
from an existing class (base class). It promotes code reuse and hierarchical classification.

- **Polymorphism**: This principle allows the same function to behave differently in different
contexts, achieved through function overloading and method overriding.

2. Difference Between a Class and an Object

A **class** is a user-defined data type that serves as a blueprint for creating objects, whereas an
**object** is an instance of a class.

| Feature | Class | Object |


|------------|--------|--------|
| Definition | A blueprint for creating objects. | An instance of a class. |
| Memory Allocation | No memory is allocated until an object is created. | Memory is allocated when
instantiated. |
| Example | `class Car { ... };` | `Car myCar;` |

**Example Code:**
```cpp
class Car {
public:
string brand;
void showBrand() { cout << "Brand: " << brand << endl; }
};

int main() {
Car myCar; // Object
myCar.brand = "Toyota";
myCar.showBrand();
}
```

3. Encapsulation in C++

Encapsulation is implemented in C++ using **access specifiers** such as `private`, `public`, and
`protected`. It prevents unintended modifications to data and enhances security.

**Example Code:**
```cpp
class Account {
private:
double balance;
public:
void setBalance(double b) { balance = b; }
double getBalance() { return balance; }
};
```

4. Constructor vs Destructor

A **constructor** is a special function that initializes an object when it is created. A **destructor** is


a function that is automatically called when an object is destroyed.

| Feature | Constructor | Destructor |


|---------------|------------|------------|
| Purpose | Initializes an object. | Cleans up resources before object is destroyed. |
| Name | Same as class name. | Same as class name prefixed with `~`. |
| Parameters | Can have parameters. | No parameters. |
| Calling | Called automatically when object is created. | Called automatically when object is
destroyed. |

**Example Code:**
```cpp
class Demo {
public:
Demo() { cout << "Constructor Called\n"; } // Constructor
~Demo() { cout << "Destructor Called\n"; } // Destructor
};

int main() {
Demo obj; // Constructor is called here
} // Destructor is called automatically when obj goes out of scope
```

5. Purpose of Access Specifiers

Access specifiers control the visibility of class members:


- **`private`**: Accessible only inside the class.
- **`public`**: Accessible from outside the class.
- **`protected`**: Accessible in derived classes but not outside.

**Example Code:**
```cpp
class Test {
private:
int x; // Only accessible within class
public:
int y; // Accessible everywhere
protected:
int z; // Accessible in derived class
};
```

You might also like