0% found this document useful (0 votes)
16 views

Object Oriented Programming (3)

Uploaded by

bhartivandana198
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Object Oriented Programming (3)

Uploaded by

bhartivandana198
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 36

Object Oriented

rogramming
What is OOP?

OOP stands for Object-Oriented Programming.

Procedural programming is about writing procedures or functions that


perform operations on the data, while object-oriented programming is about
creating objects that contain both data and functions.

Object-oriented programming has several advantages over procedural


programming:

● OOP is faster and easier to execute


● OOP provides a clear structure for the programs
What are Classes and Objects?

What are Classes and Objects?


Classes and objects are the two main aspects of object-oriented programming.

Look at the following illustration to see the difference between class and objects:

Create a Class
To create a class, use the class keyword
Example
Create a class called "MyClass":

class MyClass { // The class

public: // Access specifier

int myNum; // Attribute (int variable)

string myString; // Attribute (string variable)

};

● The class keyword is used to create a class called MyClass.


● The public keyword is an access specifier, which specifies that members (attributes
and methods) of the class are accessible from outside the class
Create an Object
In C++, an object is created from a class. We have already created the
class named MyClass, so now we can use this to create objects.

To create an object of MyClass, specify the class name, followed by the


object name.

To access the class attributes (myNum and myString), use the dot syntax
(.) on the object:
Multiple Objects
You can create multiple objects of one class:
Example
// Create a Car class with some attributes
class Car {
public:
string brand;
string model;
int year;
};

int main() {
// Create an object of Car
Encapsulation
Encapsulation in C++ is defined as the wrapping up of data and information in a single
unit. In Object Oriented Programming, 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. Now,

● 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.
Two Important property of Encapsulation

1. Data Protection: Encapsulation protects the internal state of an object by keeping


its data members private
2. Information Hiding: Encapsulation hides the internal implementation details of a
class from external code. Only the public interface of the class is accessible, providing
abstraction and simplifying the usage of the class while allowing the internal
implementation to be modified without impacting external code.
Features of Encapsulation

1. We can not access any function from the class directly. We need an object to
access that function that is using the member variables of that class.
2. The function which we are making inside the class must use.
3. If we don’t make a function inside the class which is using the member variable
of the class then we don’t call it encapsulation.
4. Encapsulation improves readability, maintainability, and security by grouping
data and methods together.
Information Hiding

In object-oriented programming, information hiding (by way of nesting


of types) reduces software development risk by shifting the code's
dependency on an uncertain implementation (design decision) onto a
well-defined interface
Polymorphism

Polymorphism is one of the most important concepts in OOP. It describes the


ability of something to have or to be displayed in more than one form.

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 real-life example of polymorphism is a person who 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 exhibits different
behavior in different situations
Types of Polymorphism
● Compile-time Polymorphism
● Runtime Polymorphism

1. Compile-Time Polymorphism
This type of polymorphism is achieved by function overloading or operator
overloading.
A. Function Overloading

When there are multiple functions with the same name but different
parameters, then the functions are said to be overloaded, hence this is
known as Function Overloading.
Functions can be overloaded by changing the number of arguments
or/and changing the type of arguments.
B. Operator Overloading

C++ has the ability to provide the operators with a special meaning for a
data type, this ability is known as operator overloading. For example, we
can make use of the addition operator (+) for string class to concatenate
two strings

2. Runtime Polymorphism- This type of polymorphism is


achieved by Function Overriding. Late binding and dynamic
polymorphism are other names for runtime polymorphism
A. Function Overriding

Function Overriding occurs when a derived class has a definition for one
of the member functions of the base class. That base function is said to
be overridden.
Unified Modeling Language (UML)
Unified Modeling Language (UML) is a general-purpose modeling language. The main aim of UML is to
define a standard way to visualize the way a system has been designed. It is quite similar to blueprints
used in other fields of engineering. UML is not a programming language , it is rather a visual language.

UML helps in specifying, visualizing, constructing, and documenting the artifacts of software systems.

● We use UML diagrams to portray the behavior and structure of a system.


● UML helps software engineers, businessmen, and system architects with modeling, design, and
analysis.
● The Object Management Group (OMG) adopted Unified Modelling Language as a standard in 1997.
It’s been managed by OMG ever since.
Why do we need UML?

● Complex applications need collaboration and planning from multiple teams


and hence require a clear and concise way to communicate amongst them.
● Businessmen do not understand code. So UML becomes essential to
communicate with non-programmers about essential requirements,
functionalities, and processes of the system.
Different Types of UML Diagrams
Structural UML Diagrams

Class Diagram

The most widely use UML diagram is the class diagram. It is the building
block of all object oriented software systems. We use class diagrams to
depict the static structure of a system by showing system’s classes, their
methods and attributes. Class diagrams also help us identify relationship
between different classes or objects.
Composite Structure Diagram

● A composite structure diagram represents relationship between


parts and their configuration which determine how the classifier
(class, a component, or a deployment node) behaves.

They represent internal structure of a structured classifier making the


use of parts, ports, and connectors
UML- Architecture
Software architecture is all about how a software system is built at its highest level.
It is needed to think big from multiple perspectives with quality and design in mind.
The software team is tied to many practical concerns, such as:

○ The structure of the development team.


○ The needs of the business.
○ Development cycle.
○ The intent of the structure itself.
ftware architecture provides a basic design of a complete software system. It defines the elements included
the system, the functions each element has, and how each element relates to one another. In short, it is a
g picture or overall structure of the whole system, how everything works together.

form an architecture, the software architect will take several factors into consideration:

○ What will the system be used for?


○ Who will be using the system?
○ What quality matters to them?
○ Where will the system run?
tware architecture is useful to people such as software developers, the project manager, the client,
e end-user. Each one will have different perspectives to view the system and will bring different
s to a project. Also, it provides a collection of several views. It can be best understood as a collection
views:

se case view
esign view
mplementation view
ocess view
evelopment view
UNIT- 2 Basic Structural Modeling

The main aim of OOP is to bind together the data and the functions that operate on them so that no other part
of the code can access this data except that function.
There are some basic concepts that act as the building blocks of OOPs i.e.

1. Class
2. Objects
3. Encapsulation
4. Abstraction
5. Polymorphism
6. Inheritance
7. Dynamic Binding
8. Message Passing
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.
t
ct is an identifiable entity with some characteristics and behavior. An Object is an
e of a Class. When a class is defined, no memory is allocated but when it is
ated (i.e. an object is created) memory is allocated.
apsulation
rmal terms, Encapsulation is defined as wrapping up data and information under a
e unit. In Object-Oriented Programming, Encapsulation is defined as binding together
ata and the functions that manipulate them.

der a real-life example of encapsulation, in a company, there are different sections


he accounts section, finance section, sales section, etc.
action
straction is one of the most essential and important features of object-oriented
mming in C++. Abstraction means displaying only essential information and hiding the
Data abstraction refers to providing only essential information about the data to the
world, hiding the background details or implementation
orphism
polymorphism means having many forms. In simple words, we can define polymorphism as
y of a message to be displayed in more than one form. A person at the same time can have
characteristics. A man at the same time is a father, a husband, and an employee. So the
son possesses different behavior in different situations. This is called polymorphism.
orts operator overloading and function overloading.

perator Overloading: The process of making an operator exhibit different behaviors in


fferent instances is known as operator overloading.
nction Overloading: Function overloading is using a single function name to perform
fferent types of tasks. Polymorphism is extensively used in implementing inheritance
ritance
pability of a class to derive properties and characteristics from another class is called
ance. Inheritance is one of the most important features of Object-Oriented Programming.

Sub Class: The class that inherits properties from another class is called Sub class or
Derived Class.
Super Class: The class whose properties are inherited by a sub-class is called Base Class or
Superclass.
Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to create
a new class and there is already a class that includes some of the code that we want, we can
derive our new class from the existing class
amic Binding
amic binding, the code to be executed in response to the function call is decided at
e. C++ has virtual functions to support this. Because dynamic binding is flexible, it
the drawbacks of static binding, which connected the function call and definition at build

You might also like