Oops Assignment - Kartik Bhardwaj
Oops Assignment - Kartik Bhardwaj
Oops Assignment - Kartik Bhardwaj
SUBJECT – OOPS
• The building block of C++ that leads to Object-Oriented programming is a Class. It is a user-defined data type, which
holds its own data members and member functions, which can be accessed and used by creating an instance of that
class. A class is like a blueprint for an object. For Example: Consider the Class of Cars. There may be many cars with
different names and brands but all of them will share some common properties like all of them will have 4 wheels,
Speed Limit, Mileage range, etc. So here, the Car is the class, and wheels, speed limits, and mileage are their
properties.
• A Class is a user-defined data type that has data members and member functions.
• Data members are the data variables and member functions are the functions used to manipulate these variables
together these data members and member functions define the properties and behavior of the objects in a Class.
• In the above example of class Car, the data member will be speed limit, mileage, etc and member functions can apply
brakes, increase speed, etc.
• We can say that a Class in C++ is a blueprint representing a group of objects which shares some common properties
and behaviors.
WHAT IS OBJECT ?
• Encapsulation is defined as binding together the data and the functions that
manipulate them. Consider a real-life example of encapsulation, in a company, there are
different sections like the accounts section, finance section, sales section, etc. The
finance section handles all the financial transactions and keeps records of all the data
related to finance. Similarly, the sales section handles all the sales-related activities and
keeps records of all the sales. Now there may arise a situation when for some reason an
official from the finance section needs all the data about sales in a particular month. In
this case, he is not allowed to directly access the data of the sales section. He will first
have to contact some other officer in the sales section and then request him to give the
particular data. This is what encapsulation is. Here the data of the sales section and the
employees that can manipulate them are wrapped under a single name “sales section”.
WHAT IS ABSTRACTION ?
• Abstraction means displaying only essential information and hiding the details. Data abstraction refers to
providing only essential information about the data to the outside world, hiding the background details or
implementation. Consider a real-life example of a man driving a car. The man only knows that pressing the
accelerator will increase the speed of the car or applying brakes will stop the car but he does not know how on
pressing the accelerator the speed is actually increasing, he does not know about the inner mechanism of the car
or the implementation of an accelerator, brakes, etc. in the car. This is what abstraction is.
• Abstraction using Classes: We can implement Abstraction in C++ using classes. The class helps us to group data
members and member functions using available access specifiers. A Class can decide which data member will be
visible to the outside world and which is not.
• Abstraction in Header files: One more type of abstraction in C++ can be header files. For example, consider the
pow() method present in math.h header file. Whenever we need to calculate the power of a number, we simply
call the function pow() present in the math.h header file and pass the numbers as arguments without knowing the
underlying algorithm according to which the function is actually calculating the power of numbers.
WHAT IS POLYMORPHISM ?
• The word polymorphism means having many forms. In simple words, we can define polymorphism as the ability of a
message to be displayed in more than one form. A person at the same time can have different characteristics. A
man at the same time is a father, a husband, and an employee. So the same person possesses different behavior in
different situations. This is called polymorphism. An operation may exhibit different behaviors in different instances.
The behavior depends upon the types of data used in the operation. C++ supports operator overloading and
function overloading.
• Operator Overloading: The process of making an operator exhibit different behaviors in different instances is known
as operator overloading.
• Function Overloading: Function overloading is using a single function name to perform different types of tasks.
Polymorphism is extensively used in implementing inheritance.
• Example: Suppose we have to write a function to add some integers, sometimes there are 2 integers, and
sometimes there are 3 integers. We can write the Addition Method with the same name having different parameters,
the concerned method will be called according to parameters.
WHAT IS DYANAMIC BINDING ?
• Objects communicate with one another by sending and receiving information. A message for an object is a
request for the execution of a procedure and therefore will invoke a function in the receiving object that
generates the desired results. Message passing involves specifying the name of the object, the name of the
function, and the information to be sent.
• Message passing is a fundamental concept in object-oriented programming and distributed systems. Here’s a
brief overview of how it works:Objects as Communicators: In object-oriented programming, objects
communicate by sending messages to each other. Each object can send and receive messages, typically in the
form of method calls.Sending a Message: When an object needs to request some action or information from
another object, it sends a message. This involves calling a method on the recipient object, often with some
parameters.Receiving a Message: The recipient object has defined methods (or handlers) to process the
message. When a message is received, the appropriate method is invoked to handle the request.Dynamic
Dispatch: In many languages, message passing uses dynamic dispatch, where the method to be executed is
determined at runtime based on the actual type of the object. This allows for polymorphic behavior, where the
same message can trigger different behaviors depending on the object.