UNIT-I: Principles of Object-Oriented Programming
1.0 Introduction to Object-Oriented Programming (OOP)
Object-Oriented Programming (OOP) is a paradigm based on "objects," which bundle data (attributes) and
code (methods). Its primary objective is to bind data with the functions that operate on it, restricting
external access. This approach facilitates the development of manageable, reusable, and scalable software
by modeling real-world entities.
Significance:
• Real-World Modeling: Models real-world problems intuitively by mapping them to software
objects.
• Code Reusability: Reduces development time by enabling code reuse through concepts like
inheritance.
• Data Security: Enhances system integrity and security by restricting data access through
encapsulation.
• Maintainability and Scalability: Improves maintainability and scalability due to the modular nature
of objects.
1.1 The Object-Oriented Approach
The object-oriented approach shifts focus from procedural logic to data itself, decomposing problems into
self-contained objects. Each object encapsulates its own data (attributes) and behaviors (methods),
representing a distinct system entity. Objects communicate via messages through well-defined interfaces,
which reduces inter-dependencies and creates a more robust, flexible design.
Key Characteristics:
• Emphasis is on data rather than procedure.
• Programs are divided into objects.
• Data structures are designed to characterize objects.
• Functions that operate on an object's data are tied together.
• Data is hidden and cannot be accessed by external functions.
• Objects communicate with each other through functions.
• Follows a bottom-up approach in program design.
1.2 Relation of the Object-Oriented Paradigm to Other Paradigms
1.2.1 Comparison with Procedural Programming Procedural programming organizes a program as a
sequence of procedures. The primary focus is on functions, and data is often stored in globally accessible
variables.
• Focus: Procedural programming focuses on algorithms ("how"), while OOP focuses on objects and
their interactions ("what").
• Data Model: Procedural programming separates data and functions; OOP bundles them within
objects.
• Data Security: Procedural programming offers limited protection for global data. OOP provides
strong security via encapsulation.
• Design Approach: Procedural uses a top-down design; OOP employs a bottom-up approach.
1.2.2 Functional Programming Functional programming treats computation as the evaluation of
mathematical functions, avoiding mutable data and state changes.
• State Management: OOP manages state within objects. Functional programming minimizes state,
emphasizing immutability.
• Data and Operations: In OOP, data and operations are co-located in objects. In functional
programming, functions are first-class citizens, separate from data.
1.2.3 Data Decomposition Problem decomposition differs significantly between paradigms. Procedural uses
algorithmic decomposition (breaking down into steps), whereas Object-Oriented uses object
decomposition (identifying autonomous entities).
Summary:
• OOP models problems as a collection of interacting objects.
• OOP manages state within objects, unlike the stateless emphasis of functional programming.
• OOP's decomposition strategy is based on objects, not algorithms.
1.3 Basic Concepts and Terminologies
1.3.1 Abstraction Definition: Abstraction is the representation of essential features while hiding
background details. It focuses on an object's observable behavior, achieved in C++ through class interfaces
(public methods) that conceal complex implementation. Significance: It simplifies complex systems by
allowing programmers to focus on essential aspects.
Example:
// We only need to know how to use the 'Car' class, not how
// startEngine() is implemented.
class Car {
public:
void startEngine();
void accelerate();
private:
void igniteFuel();
void regulateVoltage();
};
1.3.2 Encapsulation Definition: Encapsulation bundles data (attributes) and their operating methods into a
single unit (a class). This protective wrapper, also known as data hiding, prevents direct external access to
data. Significance: It enhances security by controlling access to an object's internal state and improves
modularity.
Example:
class Employee {
private:
int salary; // Data is hidden
public:
void setSalary(int s) { if (s > 0) salary = s; }
int getSalary() { return salary; }
};
1.3.3 Inheritance Definition: Inheritance is a mechanism allowing a derived (child) class to acquire the
properties and behaviors of a base (parent) class. The derived class can extend or modify these features.
Significance: It is a cornerstone of code reusability and promotes an "is-a" relationship (e.g., a Manager is
an Employee).
• Common Misconception: Do not use inheritance for "has-a" relationships (e.g., a Car has an
Engine). Use composition instead.
Example:
// Base class
class Vehicle {
public:
void setModel(const char* m) { /*...*/ }
};
// Derived class 'Car' inherits from 'Vehicle'
class Car : public Vehicle {
public:
void startEngine() { /*...*/ }
};
1.3.4 Polymorphism Definition: Polymorphism ("many forms") is the ability of an object or function to take
multiple forms. It enables a single interface to represent a general class of actions. Significance: It provides
flexibility and extensibility, allowing new objects to be added to a system with minimal code changes.
Types of Polymorphism in C++:
1. Compile-time (Static Binding): Resolved at compile time.
o Function Overloading: Functions with the same name but different parameters.
o Operator Overloading: Assigning multiple operations to an operator.
2. Run-time (Dynamic Binding): Resolved at runtime via virtual functions.
o Function Overriding: A derived class provides a specific implementation for a virtual
function from its base class.
• Common Mistake: Forgetting to declare the base class function as virtual, which prevents run-time
polymorphism.
Example (Run-time Polymorphism):
class Shape {
public:
virtual void draw() { /* Draw generic shape */ }
};
class Circle : public Shape {
public:
void draw() override { /* Draw a circle */ }
};
Summary of OOP Concepts:
• Abstraction: Hides complexity, shows essential features.
• Encapsulation: Binds data and methods, protects data.
• Inheritance: Reuses and extends existing classes.
• Polymorphism: One interface, multiple forms.
1.4 Review of C and Key Differences Between C and C++
C++ is an extension of C that incorporates object-oriented features and other fundamental improvements.
Key Differences:
• Paradigm: C is procedural; C++ is multi-paradigm (procedural, OOP, generic).
• Data Security: C offers limited data hiding; C++ has robust protection via private and protected.
• Features: C++ adds classes, objects, inheritance, polymorphism, templates, and exception handling.
• Type Checking: Stricter in C++ than in C.
• Memory Management: C uses malloc()/free(); C++ uses type-safe new/delete which call
constructors/destructors.
• Input/Output: C uses printf()/scanf(); C++ uses cout/cin streams.
• Function Overloading: Not supported in C; supported in C++.
1.4.1 cin and cout cin and cout are C++ stream objects for standard input and output. They provide type
safety and extensibility over C's printf and scanf. cout uses the insertion operator (<<), and cin uses the
extraction operator (>>).
Example:
#include <iostream>
int main() {
int age;
std::cout << "Enter your age: ";
std::cin >> age;
std::cout << "You are " << age << " years old." << std::endl;
return 0;
1.4.2 new and delete Operators These operators handle dynamic memory allocation. new allocates
memory and calls the constructor, while delete calls the destructor and de-allocates memory. delete[] is
required for de-allocating arrays.
Example:
int* p_int = new int(10); // Allocate and initialize an integer
delete p_int; // Deallocate the integer
int* p_arr = new int[50]; // Allocate an array
delete[] p_arr; // Deallocate the array
Advantages over malloc/free:
• Type Safety: new returns a properly typed pointer without casting.
• Constructors/Destructors: Ensures proper object initialization and cleanup.
• Operator Overloading: new and delete can be overloaded by classes.
Summary of Differences:
• C++ is fundamentally object-oriented, whereas C is procedural.
• C++ offers stronger type checking and enhanced data security.
• C++ introduces type-safe I/O and memory management that integrate with the object lifecycle.