⬅ Previous Next ➡

Object-Oriented Programming Basics

Object-Oriented Programming Basics in C++
  • OOP (Object-Oriented Programming) organizes code using classes and objects.
  • Main OOP ideas: encapsulation, abstraction, inheritance, and polymorphism.
  • C++ OOP basics include classes, objects, access specifiers, constructors, destructors, this pointer, and static members.

1) Class and Object

  • Class is a blueprint (defines data + functions).
  • Object is an instance of a class.
  • Members:
    • Data members (variables)
    • Member functions (methods)
#include <iostream>
using namespace std;

class Student {
public:
    int id;
    void show() {
        cout << "ID: " << id << endl;
    }
};

int main() {
    Student s;
    s.id = 101;
    s.show();
    return 0;
}

2) Access Specifiers

  • public: accessible from anywhere.
  • private: accessible only inside the class (default in class).
  • protected: accessible in class and derived classes.
#include <iostream>
using namespace std;

class Account {
private:
    double balance;

public:
    void setBalance(double b) {
        balance = b;
    }
    void showBalance() {
        cout << "Balance: " << balance << endl;
    }
};

int main() {
    Account a;
    a.setBalance(5000);
    a.showBalance();
    return 0;
}

3) Constructors

  • Constructor is a special function with same name as class.
  • Called automatically when object is created.
  • Types: default, parameterized, copy constructor.
3.1) Default Constructor
#include <iostream>
using namespace std;

class Demo {
public:
    Demo() {
        cout << "Default Constructor called" << endl;
    }
};

int main() {
    Demo d;
    return 0;
}
3.2) Parameterized Constructor
#include <iostream>
using namespace std;

class Student {
    int id;
public:
    Student(int x) {
        id = x;
    }
    void show() {
        cout << "ID: " << id << endl;
    }
};

int main() {
    Student s(101);
    s.show();
    return 0;
}
3.3) Copy Constructor
#include <iostream>
using namespace std;

class Box {
public:
    int size;

    Box(int s) {
        size = s;
    }

    Box(const Box &obj) { // copy constructor
        size = obj.size;
    }
};

int main() {
    Box b1(10);
    Box b2 = b1;

    cout << b2.size << endl;
    return 0;
}

4) Destructor

  • Destructor is called automatically when object is destroyed.
  • Used for cleanup (free resources, close files, etc.).
  • Syntax: ~ClassName()
#include <iostream>
using namespace std;

class Demo {
public:
    Demo() {
        cout << "Constructor" << endl;
    }
    ~Demo() {
        cout << "Destructor" << endl;
    }
};

int main() {
    Demo d;
    return 0;
}

5) this Pointer

  • this is a special pointer that stores address of current object.
  • Used to resolve name conflicts and return current object.
#include <iostream>
using namespace std;

class Student {
    int id;
public:
    void setId(int id) {
        this->id = id; // this refers to current object
    }
    void show() {
        cout << "ID: " << id << endl;
    }
};

int main() {
    Student s;
    s.setId(101);
    s.show();
    return 0;
}

6) Static Members

  • static data member is shared among all objects of the class.
  • static member function can access only static members.
  • Static members are accessed using ClassName::member.
#include <iostream>
using namespace std;

class Counter {
public:
    static int count;

    Counter() {
        count++;
    }

    static void showCount() {
        cout << "Count = " << count << endl;
    }
};

int Counter::count = 0; // define static member

int main() {
    Counter a, b, c;
    Counter::showCount();
    return 0;
}

7) Quick Notes

  • Class = blueprint, Object = instance.
  • private data is protected from outside access (encapsulation).
  • Constructors initialize objects; destructors clean resources.
  • this refers to current object.
  • static members are shared across objects.
⬅ Previous Next ➡