Object Oriented Programming
Object Oriented Programming
OOP is a technique in which programs are written on the basis of Objects. An object is a
collection of data and functions. Object may represent a person, thing, or place in a real world.
In OOP, data and all possible functions on data are grouped together. Object-Oriented
programming is easier to learn and modify. C++ and Java are most popular object-Oriented
Languages. Example
A person can be an example of an object. It has some properties or characteristics that describe
it. Some properties of person object can be as follows.
A. Name
B. Age
C. Height
The person object also has some functions that describes what it can do. Some functions of
person object can be as follows.
A. Walk
B. Talk
C. Eat
Class: A class is a blueprint or template for creating objects. It defines the data members
(variables) and member functions (methods) that an object will have. Or Class, a set of
blueprints/templates for creating objects that define their properties and behaviors.
Generally, you can say that a class is a collection of similar objects. Once you have defined a
class then you can create/define as many objects as you need of that class easily. It may be
noted that defining a class does not reserve memory space. Memory space is reserved when an
object is created. The real-world entities such as automobile, aircraft, employee, rectangle,
complex numbers, etc., each one can be a class in a program.
The data within an object is called a data member, whereas the function within an object is
called a member function or method. The data member and member functions are collectively
called members of an object. These members represent the properties. Behaviors and actions
of an object. The data member is usually kept hidden so that it cannot be accessed from the
outside directly. It is accessed through member functions. So, the data is safe.
1. Encapsulation:
• Encapsulation is the bundling of data (attributes) and methods
(functions) that operate on the data into a single unit, known as a
class. It restricts direct access to some of the object's components,
which can prevent the accidental modification of data. This is
typically achieved through access modifiers (public, private,
protected).
2. Abstraction:
• Abstraction is the concept of hiding the complex reality while
exposing only the necessary parts. It allows programmers to focus
on interactions at a higher level without needing to understand the
intricate details of the implementation. This is often implemented
through abstract classes and interfaces.
3. Inheritance:
• Inheritance is a mechanism where a new class (subclass or derived
class) can inherit properties and behaviors (methods) from an
existing class (superclass or base class). This promotes code
reusability and establishes a hierarchical relationship between
classes.
4. Polymorphism:
• Polymorphism allows methods to do different things based on the
object it is acting upon, even if they share the same name. This can
be achieved through method overriding (where a subclass provides
a specific implementation of a method already defined in its
superclass) and method overloading (where multiple methods have
the same name but differ in parameters).
5. Classes and Objects:
• A class is a blueprint for creating objects, defining a set of
attributes and methods that the created objects will have. An
object is an instance of a class, representing a specific
implementation of the class with its own state and behavior.
6. Message Passing:
• Objects communicate with each other through message passing,
which involves sending and receiving messages (method calls) to
invoke behaviors or request data.
These features work together to provide a structured approach to programming,
making it easier to manage and maintain complex software systems.
An object is an instance of a class. It has its own copy of the class's data members and can use
the member functions.Minimal C++ Program with Class, Object, and Comments
Constructors
A constructor is a special member function of a class that is automatically called when an object
of that class is created (instantiated). Its primary purpose is to initialize the object's attributes
(member variables).
Destructors
A destructor is also a special member function of a class that is automatically called when an
object of that class is destroyed (goes out of scope or is explicitly deleted). Its primary purpose is
to perform cleanup operations, such as releasing dynamically allocated memory.
4. Functions
In C++ Object-Oriented Programming (OOP), functions play a crucial role in defining the
behavior of objects. They are used to manipulate data, perform actions, and implement core
OOP principles like encapsulation, abstraction, inheritance, and polymorphism.
1. Member Functions
These are functions declared inside a class. They can access both public and private members
of the class.
2. Constructor
A constructor is a special function that is automatically called when an object is created. It has
the same name as the class and no return type.
3. Destructor
A destructor is automatically called when an object is destroyed. It also has the same name as
the class but is prefixed with a tilde (~).
4. Static Function
Declared with the static keyword inside a class. Unlike regular member functions, a static
function can be called without creating an object.
• Purpose: Shared function for all objects; does not access non-static data
5. Inline Function
Defined inside the class definition. The compiler may replace the function call with the function
code, making it faster (for small functions).
6. Friend Function
A non-member function that has access to the private and protected members of a class.
• Purpose: Access private data from outside the class
7. Virtual Function
Declared with the virtual keyword in a base class. It enables runtime polymorphism, allowing
the derived class's version to be called via a base class pointer.
5. Pointers
A pointer is a variable that stores the memory address of another variable. Instead of holding a
direct value (like an int holding 5), a pointer holds the location in memory where a value is
stored.