Complete OOP Study Guide - BSCS

Object-Oriented Programming (OOP) — Complete BSCS Notes

Evolution of OOP

Machine Language (1GL): The earliest programming language written in binary digits (0s and 1s). It is directly understood by the computer hardware.
Example: 10101010
Assembly Language (2GL): Uses mnemonics instead of binary instructions.
Example: MOV AX, 5
Procedural Programming (3GL): Focuses on functions and step-by-step procedures.
Example: C language functions like void add()
Object-Oriented Programming: Organizes code around classes and objects to model real-world entities.
Example: Student class with name, rollNo, and display() method

What is OOP?

OOP is a programming paradigm based on classes and objects. Each object contains data (attributes) and functions (methods).

Example: Car → color, speed, start(), stop()

Core OOP Concepts

Class: A class is a blueprint or template used to create objects. It defines data members and methods.
Example: class Student { string name; int age; };
Object: An object is an instance of a class that holds actual values.
Example: Student s1;
Encapsulation: Combining data and methods into a single unit (class) and protecting data using access modifiers.
Example: private: int balance;
Abstraction: Showing only important details and hiding internal working.
Example: ATM machine shows withdraw button but hides backend process.
Data Hiding: Restricting direct access to sensitive data using private and protected.

OOP Principles (4 Pillars)

  • Encapsulation
  • Abstraction
  • Inheritance
  • Polymorphism

OO Program Design Process

Requirement Analysis
Identify Classes and Objects
Define Attributes and Methods
Relationships and Testing

Constructors and Destructors

Constructor Definition: A special member function that runs automatically when an object is created.
Default Constructor: A constructor with no parameters.
Example: Student(){}
Parameterized Constructor: Accepts values during object creation.
Example: Student(string n){name=n;}
Copy Constructor: Creates a new object as a copy of another object.
Example: Student(const Student &s)
Destructor: A special function that runs automatically when an object is destroyed and is used for memory cleanup.
Example: ~Student(){}

Inheritance & Derived Classes

Definition: Inheritance allows one class to use properties and methods of another class.
Single Inheritance: One child class inherits one parent class.
Example: class Dog : public Animal
Multiple Inheritance: One child class inherits from multiple parent classes.
Example: class C : public A, public B
Multilevel Inheritance: A chain like A → B → C.
Hierarchical Inheritance: Multiple child classes from one parent.
Hybrid Inheritance: Combination of two or more inheritance types.

Polymorphism

Definition: One function or operator behaves differently in different situations.
Function Overloading: Same function name with different parameters.
Example: add(int,int) and add(double,double)
Operator Overloading: Redefining operators for objects.
Example: Using + for complex numbers.
Virtual Functions: Used for runtime polymorphism.
Example: virtual void show()
Method Overriding: Child class redefines parent method.

I/O and File Processing

Input/Output: Used to take input from user and display output.
Example: cin >> name; cout << name;
ifstream: Used to read data from files.
Example: ifstream file(\"data.txt\");
ofstream: Used to write data to files.
Example: ofstream file(\"data.txt\");
File Modes: ios::in, ios::out, ios::app

Exception Handling

Definition: Exception handling is used to manage runtime errors so the program does not crash unexpectedly.
try: Contains code that may produce an error.
throw: Used to generate an exception.
Example: throw age;
catch: Handles the thrown exception.
Example: catch(int x){}
Full Example: try{ if(age<18) throw age; } catch(int x){ cout<<\"Invalid\"; }