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
Example: 10101010
Assembly Language (2GL): Uses mnemonics instead of binary instructions.
Example:
Example:
MOV AX, 5Procedural Programming (3GL): Focuses on functions and step-by-step procedures.
Example: C language functions like
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
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:
Example:
class Student { string name; int age; };Object: An object is an instance of a class that holds actual values.
Example:
Example:
Student s1;Encapsulation: Combining data and methods into a single unit (class) and protecting data using access modifiers.
Example:
Example:
private: int balance;Abstraction: Showing only important details and hiding internal working.
Example: ATM machine shows withdraw button but hides backend process.
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:
Example:
Student(){}Parameterized Constructor: Accepts values during object creation.
Example:
Example:
Student(string n){name=n;}Copy Constructor: Creates a new object as a copy of another object.
Example:
Example:
Student(const Student &s)Destructor: A special function that runs automatically when an object is destroyed and is used for memory cleanup.
Example:
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:
Example:
class Dog : public AnimalMultiple Inheritance: One child class inherits from multiple parent classes.
Example:
Example:
class C : public A, public BMultilevel 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:
Example:
add(int,int) and add(double,double)Operator Overloading: Redefining operators for objects.
Example: Using
Example: Using
+ for complex numbers.Virtual Functions: Used for runtime polymorphism.
Example:
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:
Example:
cin >> name; cout << name;ifstream: Used to read data from files.
Example:
Example:
ifstream file(\"data.txt\");ofstream: Used to write data to files.
Example:
Example:
ofstream file(\"data.txt\");File Modes:
ios::in, ios::out, ios::appException 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:
Example:
throw age;catch: Handles the thrown exception.
Example:
Example:
catch(int x){}Full Example:
try{ if(age<18) throw age; } catch(int x){ cout<<\"Invalid\"; }
0 Comments