Introduction to OOP in C++

0
86
OOP in C++

What is OOP in C++?

OOP in C++, or Object-Oriented Programming, is a programming paradigm that organizes code into objects—self-contained modules consisting of data and functions. Introduced to overcome the limitations of procedural programming, OOP emphasizes code reuse, data security, and real-world modeling.

In this tutorial by Kamlesh Singad, part of the Code With Kamlesh C++ course, we’ll guide you from theory to real-world examples to help you understand the power of OOP in C++.

Why Learn Object-Oriented Programming?

Object-Oriented Programming allows you to:

  • Build modular applications
  • Achieve code reusability through inheritance
  • Improve maintainability and scalability
  • Model real-world systems

From GUIs to gaming engines, OOP is the cornerstone of modern C++ development.

Also Read: Understanding Scope, Lifetime, and Storage Classes in C/C++

OOP in C++

Key Concepts of OOP

OOP in C++ revolves around four core pillars:

  • Encapsulation: Wrapping data and methods into a single unit
  • Abstraction: Showing only relevant details, hiding implementation
  • Inheritance: Reusing code from existing classes
  • Polymorphism: Same interface, different behaviors

Procedural vs Object-Oriented Programming

AspectProcedural ProgrammingObject-Oriented Programming
FocusFunctionsObjects
Data SecurityLess secureEncapsulated & secure
ReusabilityLimitedHigh (via inheritance)
Real-world ModelingWeakStrong

Defining a Class in C++

class Student {
public:
    int roll;
    string name;
    void display() {
        cout << roll << " - " << name << endl;
    }
};

Creating and Using Objects

Student s1;
s1.roll = 101;
s1.name = "Ravi";
s1.display();

Access Specifiers in C++

  • public: accessible from anywhere
  • private: accessible only within the class
  • protected: accessible in derived classes

Also Read: Inline Functions & Function Overloading in C++

OOP in C++

Constructors and Destructors

Constructor: Special function that runs on object creation
Destructor: Cleans up resources when the object is destroyed

class Person {
public:
    Person() {
        cout << "Constructor called!";
    }
    ~Person() {
        cout << "Destructor called!";
    }
};

Overloading Constructors

class Book {
public:
    Book() { cout << "Default\n"; }
    Book(string title) { cout << "Title: " << title; }
};

Object Arrays and Pointers

Student students[3];
Student *ptr = &students[0];
ptr->roll = 100;
OOP in C++

Friend Functions and Static Members

Friend functions access private members:

class A {
    int x;
    friend void show(A);
};

Static members are shared among all objects:

class Test {
    static int count;
};

Inheritance in C++

class Animal {
public:
    void speak() { cout << "Animal sound\n"; }
};

class Dog : public Animal {
public:
    void speak() { cout << "Bark\n"; }
};

Polymorphism

Compile-time:

void show(int x);
void show(double x);

Run-time (virtual functions):

class Base {
public:
    virtual void show() { cout << "Base\n"; }
};
class Derived : public Base {
    void show() override { cout << "Derived\n"; }
};
OOP in C++

Real-World Example: Bank Account System

class Account {
public:
    string name;
    double balance;
    void deposit(double amt) { balance += amt; }
    void display() { cout << name << ": " << balance; }
};

Best Practices

  • Keep data private, expose via accessors
  • Avoid deep inheritance hierarchies
  • Use virtual destructors in base classes

Also Read: Recursion in C/C++: Understanding Recursive Functions

FAQs

What is the main idea of OOP?
To model real-world entities through objects.

How is OOP different from procedural programming?
OOP organizes code into reusable, secure modules (objects).

What are constructors and destructors?
Constructors initialize objects, destructors clean up.

Can C++ use both OOP and procedural styles?
Yes, it supports both paradigms.

Is OOP useful in competitive programming?
While rare, OOP is useful in simulations and modular code.

Conclusion

With OOP in C++, you’ve now stepped into a world of scalable, maintainable, and modern programming. This foundation by Kamlesh Singad in the Code With Kamlesh series empowers you to tackle real-world software design.

Continue forward into advanced class structures, inheritance models, and C++ design patterns!

LEAVE A REPLY

Please enter your comment!
Please enter your name here