Object Oriented Programming
OOPs = Object oriented programming structure
OOPs is a programming model or paradigm which revolves around
the concept of “OBJECTS”.
Q. What is an Object?
Answer: An object is an instance of a class. It contains the data members and
member functions defined in the class.
Key Points:
1. Object is an instance of a class.
2. It contains data and functions.
3. Objects occupy memory space.
4. Objects have properties and behaviors.
5. Objects are created from classes.
Example:
Class: Student
Objects: Rahul, Priya, Amit
Here, Student is the class, and Rahul, Priya, and Amit are objects.
Q. What is a Class?
Answer:
A class is a template or blueprint used to create objects. It defines the data
members (variables) and member functions (methods) that an object will have.
1. It does not occupy memory until objects are created.
2. Multiple objects can be created from a single class.
3. Objects inherit the properties and behaviors of the class.
There are several benefits to using object-oriented programming
(OOP) in software development:
Modularity: OOP allows you to break a large and complex problem down
into smaller and more manageable pieces, or "objects." This makes it
easier to write, test, and maintain your code.
Reusability: OOP allows you to reuse code by creating new objects that
are similar to existing ones. This can save time and effort when
developing software.
Extensibility: OOP allows you to easily add new features to an existing
codebase by creating new objects or modifying existing ones.
Maintainability: OOP makes it easier to track and fix errors in your
code, as objects are self-contained and can be isolated and tested
more easily than procedural code.
Scalability: OOP allows you to build software that can grow and evolve
over time, as new objects can be created or added to the existing
codebase as needed.
Oops, are based on a bottom-up approach, unlike the structural
programming paradigm, which uses a top-down approach.
Q. What are the Programming Paradigms other than OOP?
Answer:
A Programming Paradigm is a style or method of programming used to solve
problems. Programming languages are classified based on these paradigms.
There are two main types of programming paradigms:
1. Imperative Programming Paradigm
It focuses on how a program should perform a task by specifying step-by-step
instructions.
Types:
a) Procedural Programming
Program is divided into procedures or functions.
Instructions are executed in sequence.
b) Object-Oriented Programming (OOP)
Program is organized around objects and classes.
Objects contain data and functions.
c) Parallel Programming
A task is divided into smaller subtasks.
Multiple subtasks execute simultaneously.
2. Declarative Programming Paradigm
It focuses on what the program should accomplish without specifying how to
do it.
Types:
a) Logical Programming
Based on facts and rules of formal logic.
Used to solve problems through logical reasoning.
b) Functional Programming
Programs are built using functions.
Emphasizes function composition and evaluation.
c) Database Programming
Used for managing and manipulating data stored in databases.
Data is organized as fields, records, and files.
Conclusion
Thus, besides OOP, important programming paradigms include Procedural,
Parallel, Logical, Functional, and Database Programming, which are broadly
classified under Imperative and Declarative paradigms.
Q. Why do we need a Constructor in OOP? / What is a Constructor?
Answer:
A constructor is a special member function of a class that is automatically
called when an object is created. Its main purpose is to initialize the object's
data members with appropriate values.
Without a constructor, objects may contain uninitialized or garbage values.
Constructors ensure that an object is created in a valid state from the
beginning.
Features of Constructor:
1. It is automatically invoked when an object is created.
2. It has the same name as the class.
3. It does not have any return type, not even void.
4. It is used to initialize data members of an object.
5. It helps create objects with desired initial values.
Example:
C++
class Student
{
public:
Student() // Constructor
{
cout << "Object Created";
}
};
Advantages:
Initializes objects automatically.
Reduces coding effort.
Prevents uninitialized data.
Makes programs more reliable.
Conclusion:
Thus, a constructor is a special function used to initialize objects automatically
when they are created, ensuring that objects start with proper values.
Class Object
A class is a blueprint or template for
An object is an instance of a class.
creating objects.
It defines data members and member It contains actual data and can access
functions. member functions.
Memory is not allocated when a class Memory is allocated when an object is
is declared. created.
It is a logical entity. It is a physical entity.
One class can create many objects. An object belongs to a particular class.
class Student // Class
{
};
Student s1; // Object
Q. What are the Four Pillars of OOP?
Answer:
The four pillars of Object-Oriented Programming (OOP) are:
1. Encapsulation
Encapsulation is the process of binding data and methods into a single unit
called a class. It also restricts direct access to data, providing data security.
Example: Using private data members in a class.
2. Abstraction
Abstraction means hiding implementation details and showing only the
essential features to the user.
Example: A user drives a car without knowing how the engine works internally.
3. Inheritance
Inheritance is the mechanism by which one class acquires the properties and
behaviors of another class.
Example: A Car class inheriting features from a Vehicle class.
4. Polymorphism
Polymorphism means "many forms." It allows the same function or operator to
perform different tasks depending on the context.
Example: Function overloading and function overriding.
Diagram
Abstraction (Short Note)
Abstraction is one of the four pillars of OOP. It is the process of hiding
implementation details and showing only the essential features of an object to
the user.
It helps reduce complexity and improves security by allowing users to access
only the necessary information.
Example:
When driving a car, we use the steering wheel, brake, and accelerator without
knowing the internal working of the engine. This is abstraction.
Advantages:
1. Hides unnecessary details.
2. Improves security.
3. Reduces complexity.
4. Makes code easier to maintain.
Conclusion:
Thus, abstraction means displaying only important information while hiding
internal implementation details from the user.
Encapsulation (Short Note)
Encapsulation is the process of binding data (variables) and methods
(functions) together into a single unit. It also restricts direct access to data by
using access specifiers such as private, protected, and public.
Encapsulation is used to achieve data hiding and data security.
Example:
A bank account hides its balance from direct access. The balance can be
accessed or modified only through methods like deposit() and withdraw().
Advantages:
1. Provides data security.
2. Prevents unauthorized access.
3. Improves code maintainability.
4. Achieves data hiding.
Conclusion:
Thus, encapsulation combines data and functions into a single unit and
protects data from unauthorized access.
Polymorphism (Short Note)
Polymorphism is one of the four pillars of OOP. The word polymorphism
means "many forms." It allows the same function, method, or operator to
perform different tasks in different situations.
Polymorphism increases flexibility and reusability in programs.
Types of Polymorphism:
1. Compile-Time Polymorphism – Achieved through Function Overloading
and Operator Overloading.
2. Run-Time Polymorphism – Achieved through Function Overriding using
Inheritance.
Example:
A function named add() can add two integers or two floating-point numbers
depending on the arguments passed.
Advantages:
1. Increases code reusability.
2. Improves flexibility.
3. Makes programs easier to maintain.
4. Reduces code duplication.
Conclusion:
Thus, polymorphism allows a single interface to represent multiple forms,
making programs more flexible and efficient.
Inheritance (Short Note)
Inheritance is one of the four pillars of OOP. It is the mechanism of deriving a
new class from an existing class.
The existing class is called the Base Class (Parent Class), and the new class is
called the Derived Class (Child Class).
Inheritance promotes code reusability and reduces code duplication.
Example:
C++
class Vehicle // Base Class
{
};
class Car : public Vehicle // Derived Class
{
};
Here, the Car class inherits the properties of the Vehicle class.
Advantages:
1. Code reusability.
2. Reduces code duplication.
3. Improves maintainability.
4. Supports hierarchical classification.
Conclusion:
Thus, inheritance allows a derived class to use the properties and methods of a
base class, making programs more reusable and efficient.
The five types of inheritance are:
1. Single Inheritance
2. Multiple Inheritance
3. Multilevel Inheritance
4. Hierarchical Inheritance
5. Hybrid Inheritance
These types help achieve code reusability and flexibility in OOP.