0% found this document useful (0 votes)
12 views6 pages

Complete OOP Concepts in CPP Full

This document provides a comprehensive guide to Object-Oriented Programming (OOP) concepts in C++, covering the four pillars of OOP: encapsulation, abstraction, inheritance, and polymorphism. It explains key distinctions between classes and objects, the role of access specifiers, constructors and destructors, and various types of inheritance. Additionally, it includes examples to illustrate concepts such as data hiding, function overloading, and the use of friend functions.

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)
12 views6 pages

Complete OOP Concepts in CPP Full

This document provides a comprehensive guide to Object-Oriented Programming (OOP) concepts in C++, covering the four pillars of OOP: encapsulation, abstraction, inheritance, and polymorphism. It explains key distinctions between classes and objects, the role of access specifiers, constructors and destructors, and various types of inheritance. Additionally, it includes examples to illustrate concepts such as data hiding, function overloading, and the use of friend functions.

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/ 6

Comprehensive Guide to OOP Concepts in C++

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

OOP is based on four fundamental principles:

- **Encapsulation**: Wrapping data and functions into a single unit (class). Prevents direct access to
data.
- **Abstraction**: Hiding complex implementation details and exposing only necessary
functionalities.
- **Inheritance**: Allows a class to derive properties and behavior from another class.
- **Polymorphism**: The ability to use a function in different ways (overloading, overriding).

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

2. Difference Between a Class and an Object

**Class** is a blueprint, while an **object** is an instance of a class.

| Feature | Class | Object |


|---------|-------|--------|
| Definition | Blueprint | Instance of class |
| Memory | No memory allocated | Memory allocated when created |

**Example:**
```cpp
class Car {
public:
string brand;
void showBrand() { cout << brand; }
};
Car myCar; // Object
```

3. Encapsulation in C++

Encapsulation ensures data security by restricting direct access.

**Example:**
```cpp
class Account {
private:
double balance;
public:
void deposit(double amount) { balance += amount; }
double getBalance() { return balance; }
};
```

4. Constructor vs Destructor

A **constructor** initializes an object when created, while a **destructor** cleans up memory.

| Feature | Constructor | Destructor |


|---------|------------|------------|
| Name | Same as class | `~ClassName` |
| Parameters | Can have | No parameters |

**Example:**
```cpp
class Demo {
public:
Demo() { cout << "Constructor called"; }
~Demo() { cout << "Destructor called"; }
};
```

5. Purpose of Access Specifiers

**Access specifiers** control access to class members:


- **`private`**: Accessible only within the class.
- **`public`**: Accessible everywhere.
- **`protected`**: Accessible in derived classes.

**Example:**
```cpp
class Test {
private: int x;
public: int y;
protected: int z;
};
```

6. Data Hiding in OOP

Data hiding prevents direct access to class members, enforcing security and abstraction.

**Example:**
```cpp
class BankAccount {
private:
double balance;
public:
void deposit(double amount) { balance += amount; }
};
```

7. Can a C++ Program Have Multiple Constructors?

Yes, through **constructor overloading**.

**Example:**
```cpp
class Rectangle {
public:
Rectangle() {}
Rectangle(int w, int h) {}
};
```

8. Types of Constructors

- **Default Constructor**: No parameters.


- **Parameterized Constructor**: Takes arguments.
- **Copy Constructor**: Copies values from another object.

**Example:**
```cpp
class Demo {
public:
Demo(int a) {}
Demo(const Demo &obj) {}
};
```

9. Static Members in a Class

A **static member** belongs to the class, not an object.

**Example:**
```cpp
class Counter {
public:
static int count;
};
```

10. Friend Function in C++

A **friend function** can access private members of a class.

**Example:**
```cpp
class A {
private:
int x;
friend void show(A obj);
};
```

11. Inheritance in C++

Inheritance allows a class to acquire properties of another class.

**Types:** Single, Multiple, Multilevel, Hierarchical, Hybrid.

**Example:**
```cpp
class A {};
class B : public A {};
```

12. Multiple Inheritance

A class inheriting from multiple base classes.

**Example:**
```cpp
class A {};
class B {};
class C : public A, public B {};
```

13. Virtual Base Class

Avoids duplicate base class instances in multiple inheritance.

**Example:**
```cpp
class A {};
class B : virtual public A {};
```

14. Types of Inheritance

| Type | Description |
|------|------------|
| Public | Keeps visibility same |
| Private | Makes all inherited members private |
| Protected | Makes public and protected members protected |

15. Function Overloading vs Overriding

**Overloading:** Same function name, different parameters.

**Overriding:** Redefining base class function in derived class.

**Example:**
```cpp
class A { virtual void show(); };
class B : public A { void show() override; };
```

You might also like