Notes – C++ Object Oriented Programming

Object-Oriented Programming (OOP) is a programming approach that organizes code into objects, combining data and functions that operate on that data.

C++ is a multi-paradigm language, but it strongly supports OOP features.


Why Use OOP?

  • To model real-world entities using code
  • Helps write modular, reusable, and maintainable programs
  • Makes large-scale software easier to manage

Four Main Pillars of OOP


ConceptDescription
EncapsulationBundling data and functions into a single unit (class)
AbstractionHiding internal details and showing only necessary info
InheritanceReusing code by deriving new classes from existing ones
PolymorphismAbility of one function to behave differently based on context

Key OOP Terms in C++


TermMeaning
ClassBlueprint or template for creating objects
ObjectInstance of a class
MethodFunction defined inside a class
Data MemberVariable inside a class

Example: Simple Class and Object

#include <iostream>
using namespace std;

class Car {
public:
string brand;

void start() {
cout << brand << " is starting...\n";
}
};

int main() {
Car c1;
c1.brand = "Toyota";
c1.start();
return 0;
}

Access Specifiers


SpecifierAccess Level
publicAccessible from anywhere
privateAccessible only within the class
protectedAccessible in the class and derived classes

OOP in Action


FeatureExample Use Case
EncapsulationHiding data in classes
InheritanceCreating a Bike class from a Vehicle class
PolymorphismOverloading functions or overriding methods
AbstractionUsing abstract classes/interfaces