Object Oriented Programming | PDF | Object Oriented Programming | Inheritance (Object Oriented Programming)
0% found this document useful (0 votes)
40 views39 pages

Object Oriented Programming

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
40 views39 pages

Object Oriented Programming

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 39

object-oriented programming (OOP)

What is object-oriented programming?


Object-oriented programming (OOP) is a computer programming model that
organizes software design around data, or objects, rather than functions and
logic. An object can be defined as a data field that has unique attributes and
behavior.
OOP focuses on the objects that developers want to manipulate rather
than the logic required to manipulate them. This approach to
programming is well-suited for programs that are large, complex and
actively updated or maintained. This includes programs for
manufacturing and design, as well as mobile applications; for example,
OOP can be used for manufacturing system simulation software
What are the main principles of OOP?
Object-oriented programming is based on the following principles.
1:Encapsulation. Encapsulation: Encapsulation refers to the bundling of
data and the methods or functions that operate on that data into a single
unit called an object. This principle ensures that the data is hidden and
can only be accessed or modified through the defined methods,
providing data abstraction and protection
Continued……
2:Abstraction. Objects only reveal internal mechanisms that are
relevant for the use of other objects, hiding any unnecessary
implementation code. The derived class can have its functionality
extended. This concept can help developers more easily make
additional changes or additions over time.
3:Inheritance. Inheritance allows objects to inherit properties
and behaviors from other objects or classes. It establishes a
hierarchical relationship between classes, where a subclass can
inherit attributes and methods from a superclass. Inheritance
promotes code reuse, extensibility, and the creation of
specialized classes based on existing ones.
4:Polymorphism. Polymorphism enables objects of different classes to
be treated as objects of a common superclass. It allows methods to be
defined in a generic way, and different implementations of these
methods can be used depending on the specific object being accessed.
Polymorphism promotes flexibility and modularity in design.
What are examples of object-oriented
programming languages?
• While Simula is credited as being the first object-oriented
programming language, many other programming languages are used
with OOP today. But some programming languages pair with OOP
better than others. For example, programming languages considered
pure OOP languages treat everything as objects. Other programming
languages are designed primarily for OOP, but with some procedural
processes included.
For example, popular pure OOP languages include.
 Ruby
 Scala
 JADE
 Emerald
 Programming languages designed primarily for OOP include:
• Java
• Python
• C++
Other programming languages that pair with OOP include:
 Visual Basic .NET
 PHP
 JavaScript
What are the benefits of OOP?
• Benefits of OOP include:
1. Modularity. Encapsulation enables objects to be self-contained, making troubleshooting and
collaborative development easier.
2. Reusability. Code can be reused through inheritance, meaning a team does not have to write
the same code multiple times.
3. Productivity. Programmers can construct new programs quicker through the use of multiple
libraries and reusable code.
4. Easily upgradable and scalable. Programmers can implement system functionalities
independently.
5. Interface descriptions. Descriptions of external systems are simple, due to message passing
techniques that are used for objects communication.
6. Security. Using encapsulation and abstraction, complex code is hidden, software maintenance is
easier and internet protocols are protected.
7. Flexibility. Polymorphism enables a single function to adapt to the class it is placed in. Different
objects can also pass through the same interface.
Criticism of OOP

• The object-oriented programming model has been criticized by


developers for multiple reasons.
The largest concern is that OOP overemphasizes the data component of
software development and does not focus enough on computation or
algorithms. Additionally, OOP code may be more complicated to write
and take longer to compile.
C++ 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
• OOP helps to keep the C++ code DRY "Don't Repeat Yourself", and makes the code
easier to maintain, modify and debug
• OOP makes it possible to create full reusable applications with less code and shorter
development time
• Object-oriented programming – As the name suggests uses objects in
programming. Object-oriented programming aims to implement real-
world entities like inheritance, hiding, polymorphism, etc. in
programming. 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
So, a class is a template for objects, and an object is an instance of a
class.
When the individual objects are created, they inherit all the variables
and functions from the class.
C++ Classes and Objects
• In previous lectures, we learned about functions and variables. Sometimes it's
desirable to put related functions and data in one place so that it's logical and
easier to work with.
• Suppose, we need to store the length, breadth, and height of a rectangular
room and calculate its area and volume.
• To handle this task, we can create three variables, say, length, breadth, and
height along with the functions calculateArea() and calculateVolume().
• However, in C++, rather than creating separate variables and functions, we can
also wrap these related data and functions in a single place (by
creating objects). This programming paradigm is known as object-oriented
programming.

But before we can create objects and use them in C++, we first need to learn
about classes.
C++ Class

A class is a blueprint for the object.


We can think of a class as a sketch (prototype) of a house. It contains all the details
about the floors, doors, windows, etc. Based on these descriptions we build the house.
House is the object.
Create a Class
A class is defined in C++ using keyword class followed by the name of the class.
The body of the class is defined inside the curly brackets and terminated by a semicolon
at the end.

class className {
// some data
// some functions
};
Here, we defined a class named Room.

The variables length, breadth, and height declared inside the class are known as data members. And, the
functions calculateArea() and calculateVolume() are known as member functions of a class.
C++ Objects

 When a class is defined,


only the specification for
the object is defined; no
memory or storage is
allocated.
 To use the data and access
functions defined in the
class, we need to create
objects.
C++ Access Modifiers

One of the main features of object-oriented programming languages


such as C++ is data hiding.
Data hiding refers to restricting access to data members of a class. This
is to prevent other functions and classes from tampering with the class
data.
However, it is also important to make some member functions and
member data accessible so that the hidden data can be manipulated
indirectly.
The access modifiers of C++ allows us to determine which class
members are accessible to other classes and functions, and which are
not.
Types of C++ Access Modifiers

In C++, there are 3 access modifiers:


1. public
2. private
3. Protected
public Access Modifier
The public keyword is used to create public members (data and functions).
The public members are accessible from any part of the program.
private Access Modifier

• The private keyword is used to create private members (data and


functions).
• The private members can only be accessed from within the class.
• However, friend classes and friend functions can access private
members.
protected Access Modifier

• Before we learn about the protected access specifier, make sure you
know about inheritance in C++.
• The protected keyword is used to create protected members (data
and function).
• The protected members can be accessed within the class and from
the derived class.
Inheritance

 In C++, it is possible to inherit attributes and methods


from one class to another. We group the "inheritance
concept" into two categories:
derived class (child) - the class that inherits from another class
base class (parent) - the class being inherited from
 To inherit from a class, use the : symbol.
 In the example below, the Car class (child) inherits the attributes and
methods from the Vehicle class (parent):
Why And When To Use "Inheritance"?

• - It is useful for code reusability: reuse attributes and methods of an


existing class when you create a new class.
Multilevel Inheritance

A class can also be derived from one class, which is already derived
from another class.

In the following example, MyGrandChild is derived from class


MyChild (which is derived from MyClass).
Multiple Inheritance
• A class can also be derived from more than one base
class, using a comma-separated list:

You might also like