0% found this document useful (0 votes)
13 views9 pages

Object Oriented Programming

Object-Oriented Programming (OOP) is a programming paradigm that uses objects and classes to structure code, emphasizing concepts such as encapsulation, abstraction, inheritance, and polymorphism. A class serves as a blueprint for creating objects, which are instances containing data members and member functions. Key features of OOP facilitate easier management and maintenance of complex software systems.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views9 pages

Object Oriented Programming

Object-Oriented Programming (OOP) is a programming paradigm that uses objects and classes to structure code, emphasizing concepts such as encapsulation, abstraction, inheritance, and polymorphism. A class serves as a blueprint for creating objects, which are instances containing data members and member functions. Key features of OOP facilitate easier management and maintenance of complex software systems.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

1.

Object Oriented Programming (OOP)


Object-oriented programming is a technique in which programs are written based on objects
and classes.

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.

A class is a general format, plan, or template of an object. It specifies the characteristics,


behaviors, and actions of the object belonging to that class. These characteristics, behaviors,
and actions are incorporated in a class in the form of data members and member functions.

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.

Objects: An object is a unit that contains functions and data together.

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.

Object-Oriented Programming (OOP) is a programming paradigm that uses "objects" to design


applications and computer programs. The main features of OOP include:

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.

2. Class and Objects


In object-oriented programming (OOP), classes and objects are fundamental
concepts that help in organizing and structuring code. Here’s a brief overview of
both:
Classes
• Definition: A class is a user-defined data type that acts as a blueprint for
creating objects. It can contain data members (variables) and member
functions (methods).
• Components:
• Attributes: These are the properties or characteristics of the class.
They represent the state of an object.
• Methods: These are functions defined within a class that describe
the behaviors of the objects created from the class.
What is an Object?

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

This simple program shows:

• How to define a class


• How to create an object
• How to call a function using the object
3. Constructors and Destructors with small examples

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

Functions in C++ (OOP Perspective)

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.

• Purpose: Define object behavior

• Accessed using: Object of the class (obj.function())

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.

• Purpose: Initialize object variables

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 (~).

• Purpose: Clean up resources (memory, files, etc.)

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).

• Purpose: Improve performance

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.

• Purpose: Achieve dynamic method binding in inheritance

Why Functions Matter in OOP

OOP Concept Role of Functions

Encapsulation Member functions control access to data

Abstraction Functions hide internal implementation

Inheritance Functions can be inherited and reused

Polymorphism Virtual functions allow dynamic behavior

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.

Think of it like this:

• A regular variable is like a house, containing people (its value).


• A pointer is like a piece of paper with the house's address written on it. It doesn't contain
the people, but it tells you exactly where to find them.

Basic Pointer Concepts:


• Use * to declare a pointer.
• Use & to get the address of a variable.
• Use * to access the value stored at the address (dereferencing).

You might also like