Mastering C++: From Fundamentals to Advanced Programming

0 0 0 0 0

Chapter 6: Object-Oriented Programming (OOP) in C++

🔹 1. Introduction

Object-Oriented Programming (OOP) is a programming paradigm based on the concept of "objects", which contain both data (attributes) and functions (methods). C++ was one of the first mainstream languages to implement OOP, and it remains one of the most powerful tools for modular, scalable, and reusable software design.


🔹 2. Core Principles of OOP

Principle

Description

Encapsulation

Bundles data and functions into a single unit (class)

Abstraction

Hides internal details, exposes only necessary parts

Inheritance

Enables classes to acquire properties of other classes

Polymorphism

Same function behaves differently for different classes


🔹 3. Classes and Objects

Class: Blueprint for creating objects

class Car {

public:

    string brand;

    void honk() {

        cout << "Beep Beep!";

    }

};

Object: Instance of a class

int main() {

    Car myCar;

    myCar.brand = "Toyota";

    myCar.honk(); // Output: Beep Beep!

}


🔹 4. Access Specifiers

Specifier

Accessible from…

public

Anywhere

private

Only inside the class

protected

Inside class and derived classes


🔹 5. Constructors & Destructors

Constructor: Initializes object automatically

class Person {

public:

    string name;

    Person(string n) {

        name = n;

    }

};

Destructor: Cleans up when object is destroyed

~Person() {

    cout << "Object destroyed";

}


🔹 6. Inheritance in C++

Inheritance allows one class to derive features from another.

Example:

class Animal {

public:

    void sound() {

        cout << "Animal sound\n";

    }

};

 

class Dog : public Animal {

public:

    void bark() {

        cout << "Dog barks\n";

    }

};

Use:

Dog d;

d.sound();  // Inherited

d.bark();   // Own method


🔹 7. Types of Inheritance

Type

Syntax Example

Single

class B : public A

Multilevel

A → B → C

Multiple

class C : public A, public B

Hierarchical

Multiple classes inherit one class


🔹 8. Polymorphism

Function Overloading (Compile-Time Polymorphism)

class Math {

public:

    int add(int a, int b) {

        return a + b;

    }

    float add(float a, float b) {

        return a + b;

    }

};

Function Overriding (Run-Time Polymorphism)

class Base {

public:

    virtual void greet() {

        cout << "Hello from Base\n";

    }

};

 

class Derived : public Base {

public:

    void greet() override {

        cout << "Hello from Derived\n";

    }

};

Virtual Functions

Allow dynamic dispatch during runtime (used with base class pointers).


🔹 9. Encapsulation and Abstraction

Encapsulation is the practice of keeping variables private and accessing them via public methods.

class Account {

private:

    int balance;

 

public:

    void deposit(int amount) {

        balance += amount;

    }

    int getBalance() {

        return balance;

    }

};

This protects internal details and exposes only what's necessary (abstraction).


🔹 10. Real-World Example: Student Management

class Student {

private:

    string name;

    int rollNo;

 

public:

    Student(string n, int r) {

        name = n;

        rollNo = r;

    }

 

    void display() {

        cout << "Name: " << name << ", Roll No: " << rollNo;

    }

};


🔹 Summary Table

Concept

Description / Example

Class/Object

class Car, Car myCar;

Constructor

Car(string brand)

Inheritance

class Dog : public Animal

Overloading

add(int a, int b) vs add(float a, b)

Overriding

virtual void show() → overridden

Encapsulation

Private members + public accessors



Back

FAQs


1. What is C++ used for today?

C++ is used in game development, operating systems, compilers, IoT, robotics, and performance-critical applications

2. Is C++ beginner-friendly?

  1. Yes — it’s a great first language, though it requires understanding of memory management and syntax.

3. What’s the difference between C and C++?

C is procedural; C++ supports both procedural and object-oriented programming

4. How is memory managed in C++?

Using pointers, references, and manual memory control with new and delete.

5. What is the STL in C++?

The Standard Template Library is a powerful collection of classes like vector, set, map, and queue.

6. Is C++ still relevant in 2025 and beyond?

Absolutely. It's critical in industries like gaming, defense, automotive, and finance

7. Can I build web apps with C++?

Not commonly — C++ is not typically used for frontend or backend web apps. However, it powers backend engines in browsers.

8. Is C++ used in machine learning?

Yes, many machine learning libraries (like TensorFlow) have C++ backends for performance.

9. How hard is it to switch from C++ to Python?

Very easy — once you’ve mastered C++, most modern languages feel simpler.