The Foundation of Object-Oriented Programming
Object-Oriented Programming is a paradigm that organizes software design around data (objects) rather than functions and logic.
A class is the blueprint. An object is the actual instance built from that blueprint.
A class is a user-defined data type that bundles data members (attributes) and member functions (methods) together.
class keyword;// Defining a Class class Student { public: // Data Members string name; int rollNo; float cgpa; // Member Function void displayInfo() { cout << "Name: " << name; cout << "Roll: " << rollNo; } };
An object is an instance of a class. When a class is defined, no memory is allocated โ memory is allocated only when an object is created.
Student s1;
Created on the stack. Automatically destroyed when out of scope.
Student *s2 = new Student();
Created on the heap. Must be manually deleted.
#include <iostream> using namespace std; class Student { public: string name; int rollNo; }; int main() { // Creating objects Student s1; // stack Student s2, s3; // multiple // Accessing members s1.name = "Alice"; s1.rollNo = 101; cout << s1.name; // Alice return 0; }
Access specifiers control the visibility of class members. C++ provides three access specifiers:
class BankAccount { private: double balance; // hidden from outside public: void deposit(double amt) { balance += amt; } double getBalance() { return balance; } };
A constructor is a special member function that is automatically called when an object is created. It initializes the object.
class Rectangle { private: int width, height; public: // Default Constructor Rectangle() { width = 0; height = 0; } // Parameterized Constructor Rectangle(int w, int h) { width = w; height = h; } // Copy Constructor Rectangle(const Rectangle &r) { width = r.width; height = r.height; } int area() { return width * height; } }; int main() { Rectangle r1; // default Rectangle r2(5, 10); // param Rectangle r3(r2); // copy }
A destructor is a special member function called automatically when an object goes out of scope or is deleted. It cleans up resources.
~ClassName()class FileHandler { private: int* data; public: // Constructor FileHandler() { data = new int[100]; cout << "File Opened\n"; } // Destructor ~FileHandler() { delete[] data; // free memory cout << "File Closed\n"; } }; int main() { FileHandler fh; // constructor // ... use fh ... } // destructor auto-called
this PointerMember functions define the behavior of a class. The this pointer is an implicit pointer that points to the current object.
this->name = object's namethis->age = object's ageReturnType ClassName::functionName() { }
Use :: (scope resolution operator)
class Person { private: string name; int age; public: // 'this' resolves name conflict void setData(string name, int age) { this->name = name; this->age = age; } // Defined outside class void display(); }; // Scope resolution operator :: void Person::display() { cout << "Name: " << this->name << ", Age: " << this->age; }
Static members belong to the class itself, not to any individual object. They are shared across all instances.
static keywordClassName::memberclass Counter { private: static int count; // shared public: Counter() { count++; } ~Counter() { count--; } static int getCount() { return count; } }; // Define outside class int Counter::count = 0; int main() { Counter c1, c2, c3; cout << Counter::getCount(); // Output: 3 }
#include <iostream> using namespace std; class BankAccount { private: // encapsulated string owner; double balance; static int totalAccounts; public: // Parameterized Constructor BankAccount(string name, double bal) { owner = name; balance = bal; totalAccounts++; } // Destructor ~BankAccount() { totalAccounts--; } // Member Functions void deposit(double amt) { if (amt > 0) balance += amt; }
void withdraw(double amt) { if (amt <= balance) balance -= amt; else cout << "Insufficient funds!\n"; } void display() { cout << "Owner: " << owner << "\n" << "Balance: " << balance << "\n"; } static int getTotal() { return totalAccounts; } }; int BankAccount::totalAccounts = 0; int main() { BankAccount acc1("Alice", 5000); BankAccount acc2("Bob", 3000); acc1.deposit(1000); acc2.withdraw(500); acc1.display(); cout << BankAccount::getTotal(); // 2 }