OOPS Notes - Unit-1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

Course Name: Object Oriented Programming Systems (OOPS)

Course Code : CS/IT103

Credits: 4

Prepared by: Dr. Rakesh Chowdhury

Assistant Professor (EEE)


Email: [email protected]
Course Structure and Evaluation Scheme
Pre-requisite: C

Exam Markings
• OOPS Theory (Minor + Major) Exam: 60% Weightage

• OOPS Lab (Minor + Major) Exam: 30% Weightage

• Assignment/Attendance/Discipline: 10% Weightage

Why OOPS?????
• OOP encourages a systematic and modular approach to
problem-solving.
• You'll develop the ability to break down complex problems
into manageable components using classes and objects.
• You'll learn how to design and implement reusable code
through the use of classes and inheritance, promoting
efficiency and maintainability in software development.
• You'll be adept at creating abstract representations of real-
world entities and organizing code into modular,
manageable components.
• You'll have a clear understanding of inheritance hierarchies,
polymorphic behavior, and the advantages of code reuse
and flexibility they provide.
• The course serves as a foundation for more advanced topics
in software development, such as design patterns, advanced
C++ features, and software architecture.
UNIT-1: Introduction to Object Oriented
Programming (OOP)
Unit-1: Introduction to OOP

• Till now, You have studied C Programming Language in previous semester.


• In this course, You will study C++ Programming Language.

Set of Instructions given to computer

• C++ means C with Classes

Concepts of Objects Object oriented programming (OOP)

• Java is another example of object-oriented programming (OOP)

• C language is Procedural oriented programming (POP).

• Example of POP: FORTRAN, C, PASCAL etc.


Fig. The relationship between C and C++.
Unit-1: Introduction to OOP

Why do we need Object oriented programming (OOP) ??


• OOP was developed because of limitations in traditional approach in programming.

• To appreciate what OOP does, we need to understand what these limitations are and how they arose from traditional
programming languages such as Procedural languages.

Procedural Languages:

• C, Pascal, FORTRAN, and similar languages are procedural languages. That is, each statement in the
language tells the computer to do something: Get some input, add these numbers, divide by six,
display that output. A program in a procedural language is a list of instructions.

• For very small programs, no other organizing principle (often called a paradigm) is needed. The
programmer creates the list of instructions, and the computer carries them out

• Division into Functions: When programs become larger, a single list of instructions becomes difficult.

• For this reason the function was adopted as a way to make programs more comprehensible to their
human creators.

• A procedural program is divided into functions, and (ideally, at least) each function has a clearly defined
Suitable for small program.
purpose and a clearly defined interface to the other functions in the program
Unit-1: Introduction to OOP

• The idea of breaking a program into functions can be further extended by grouping a
number of functions together into a larger entity called a module (which is often a file),
but the principle is similar: a grouping of components that execute lists of instructions.
Dividing a program into functions and modules is one of the cornerstones of structured
programming

• Dividing a program into functions and modules is one of the cornerstones of structured
programming. As programs grow ever larger and more complex, even the structured
programming approach begins to show signs of strain

• No matter how well the structured programming approach is implemented, large


programs become excessively complex.

What are the reasons for these problems with procedural languages?

There are two main related problems


• First, functions have unrestricted access to global data

• Second, unrelated functions and data, the basis of the procedural paradigm, provide a poor model of the real world.
Unit-1: Introduction to OOP

(a) Unrestricted Access:

• In a procedural program, one written in C for example, there are two


kinds of data/variable
(i) Local variable
(ii) Global variable

(i) Local variable:


• Local data is hidden inside a function, and is used exclusively by
the function
• Local data is closely related to its function and is safe from
modification by other functions
Fig. The arrangement of Global and local variables
(ii) Global variable:

• However, when two or more functions must access the same data—and this is true of the most important data
in a program—then the data must be made global

• In a large program, it is difficult to identify what data is used by which function, in case we need to revise external data
structure, we also need to revise all the functions that access the data.
• There are many functions and many global data items. The problem with the procedural paradigm is that this leads to an
even larger number of potential connections between functions and data
Unit-1: Introduction to OOP

Fig. Example of C program. Fig. The arrangement of Functions, Global and local variables

In a large project, This large number of connections causes problems in several ways.

• First, it makes a program’s structure difficult to conceptualize. (the action or process of forming a concept or idea of
something.)

• Second, it makes the program difficult to modify.


Unit-1: Introduction to OOP

• Example of ATM Machine


• Let this ATM application has some functions such as
FUNCTIONS GLOBAL VARIABLES

WITHDRAWL ()
BALANCE

CHECK_BALANCE ()
PIN
GEN_PIN ()

NAME
MINI_STATEMENT ()
Unit-1: Introduction to OOP

• A change made in a global data item may necessitate rewriting all the functions that access that item.

• For example, in our example, someone may decide that the PIN should be changed from 4 digits to 12 digits. This may
necessitate a change from a short to a long data type.

• Now all the functions that operate on the data must be modified to deal with a long instead of a short.

• It’s similar to what happens when your local supermarket moves the bread from aisle 4 to aisle 7. Everyone who
patronizes the supermarket must then figure out where the bread has gone, and adjust their shopping habits
accordingly.

• When data items are modified in a large program it may not be easy to tell which functions access the data, and even
when you figure this out, modifications to the functions may cause them to work incorrectly with other global data
items.

• Everything is related to everything else, so a modification anywhere has far-reaching, and often unintended,
consequences.

ROOT CAUSE: VARIABLE AND FUNCTIONS ARE SEPARATE FROM EACH OTHER
Unit-1: Introduction to OOP

(b) Real World Modelling


• The second and more important problem with the procedural paradigm is that its arrangement of separate data and
functions does a poor job of modeling things in the real world.

• In the physical world we deal with objects such as people and cars. Such objects aren’t like data and they aren’t like
functions. Complex real-world objects have both attributes and behavior

Attributes:
(i) Examples of attributes (sometimes called characteristics) are, for people, eye color and job title; and, for cars,
horsepower and number of doors.

(ii) As it turns out, attributes in the real world are equivalent to data in a program: they have a certain specific values,
such as blue (for eye color) or four (for the number of doors).
Behaviour:
(i) Behavior is something a real-world object does in response to some stimulus. If you ask your boss for a raise, she will
generally say yes or no. If you apply the brakes in a car, it will generally stop. Saying something and stopping are
examples of behavior.
(ii) Behavior is like a function: you call a function to do something (display the inventory, for example) and it does it.
So, neither data(Variables) nor functions, by themselves, model real-world objects effectively.
Unit-1: Introduction to OOP

How OOP solves this problem • In the physical world we deal with objects such as people and cars. Such objects
aren’t like data and they aren’t like functions. Complex real-world objects have both
CLASS attributes and behavior
VARIABLE
+
FUNCTIONS

Object
Class Name: ATM
BALANCE
BALANCE
PIN
PIN
NAME
NAME
+
MINI_STATEMENT ()
MINI_STATEMENT ()
WITHDRAWL ()
WITHDRAWL ()
GEN_PIN ()
GEN_PIN () Fig. The object-oriented paradigm.
CHECK_BALANCE ()
CHECK_BALANCE ()

Each customer has their own objects


Unit-1: Introduction to OOP
Unit-1: Introduction to OOP
Unit-1: Introduction to OOP

Fig. House Blueprint (CLASS)


Unit-1: Introduction to OOP

Fig. Real House (Object)

Fig. Real House (Object)


Unit-1: Introduction to OOP

Source: https://www.geeksforgeeks.org/differences-between-procedural-and-object-oriented-programming/
Unit-1: Features of Object-Oriented Programming
Unit-1: Introduction to OOP

1. Concept of Class and Object


• When you approach a programming problem in an object-oriented language, you no longer ask how the problem will be divided into
functions, but how it will be divided into objects

• Thinking in terms of objects, rather than functions, has a surprisingly helpful effect on how easily programs can be designed. This results
from the close match between objects in the programming sense and objects in the real world

• Classes In OOP we say that objects are members of classes. What does this mean? Let’s look at an analogy.
• Almost all computer languages have built-in data types. For instance, a data type int, meaning integer, is predefined in C++ You can declare
as many variables of type int as you need in your program:
int day;
int count;
int divisor;
int answer;
• In a similar way, you can define many objects of the same class. A class serves as a plan, or blueprint. It specifies what data and what
functions will be included in objects of that class. Defining the class doesn’t create any objects, just as the mere existence of data type int
doesn’t create any variables.
• A class is thus a description of a number of similar objects. This fits our non-technical understanding of the word class. Prince, Sting, and
Madonna are members of the rock musician class.
• There is no one person called “rock musician,” but specific people with specific names are members of this class if they possess certain
characteristics. An object is often called an “instance” of a class.
Unit-1: Introduction to OOP
2. Inheritance

• The capability of a class to derive properties and characteristics from another class is called Inheritance.
Inheritance is one of the most important features of Object-Oriented Programming.

• Inheritance is a feature or a process in which, new classes are created from the existing classes. The new
class created is called “derived class” or “child class” and the existing class is known as the “base class” or
“parent class”. The derived class now is said to be inherited from the base class.

• When we say derived class inherits the base class, it means, the
derived class inherits all the properties of the base class, without
changing the properties of base class and may add new features to its
own. These new features in the derived class will not affect the base
class.
Unit-1: Introduction to OOP
Why and when to use inheritance?
• Consider a group of vehicles. You need to create classes for
Bus, Car, and Truck. The methods fuelAmount(), capacity(),
applyBrakes() will be the same for all three classes. If we
create these classes avoiding inheritance then we have to
write all of these functions in each of the three classes as
shown below figure:
• You can clearly see that the above process results in
duplication of the same code 3 times. This increases the
chances of error and data redundancy. To avoid this type of
situation, inheritance is used. If we create a class Vehicle
and write these three functions in it and inherit the rest of
the classes from the vehicle class, then we can simply avoid
the duplication of data and increase re-usability.
• Look at the below diagram in which the three classes are
inherited from vehicle class:

• Using inheritance, we have to write the functions only one


time instead of three times as we have inherited the rest of
the three classes from the base class (Vehicle).
Unit-1: Introduction to OOP

3. 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 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. This is called polymorphism.
Polymorphism is considered one of the important features of Object-Oriented Programming.

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.
Unit-1: Introduction to OOP

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. In simple terms,
it is a feature of object-oriented programming providing many functions that have the same name but distinct
parameters when numerous tasks are listed under one function name. There are certain Rules of Function
Overloading that should be followed while overloading a function.
Unit-1: Introduction to OOP

Operator Overloading: The process of making an operator exhibit different behaviors in different instances is known as operator
overloading.
One of the benefits of objects is that they give the programmer a convenient way to construct new data types. Suppose
you work with two-dimensional positions (such as x and y coordinates, or latitude and longitude) in your program. You
would like to express operations on these positional values with normal arithmetic operations, such as

position1 = position2 + origin

where the variables position1, position2, and origin each represent a pair of independent numerical quantities. By
creating a class that incorporates these two values, and declaring position1, position2, and origin to be objects of this
class, we can, in effect, create a new data type. Many features of C++ are intended to facilitate the creation of new data
types in this manner.

Note that the = (equal) and + (plus) operators, used in the position arithmetic shown above, don’t act the same way they do in
operations on built-in types such as int. The objects position1 and so on are not predefined in C++, but are programmer-defined
objects of class Position.
How do the = and + operators know how to operate on objects?
The answer is that we can define new behaviors for these operators. These operations will be member functions of the Position
class. Using operators or functions in different ways, depending on what they are operating on, is called polymorphism (one
thing with several distinct forms). When an existing operator, such as + or =, is given the capability to operate on a new data
type, it is said to be overloaded. Overloading is a kind of polymorphism;
Unit-1: Introduction to OOP

4. Abstraction
Data abstraction is one of the most essential and important features of object-oriented programming in C++.
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.
Unit-1: Introduction to OOP

Advantages of Data Abstraction

•Helps the user to avoid writing the low-level code

•Avoids code duplication and increases reusability.

•Can change the internal implementation of the class


independently without affecting the user.

•Helps to increase the security of an application or program


as only important details are provided to the user.

•It reduces the complexity as well as the redundancy of the


code, therefore increasing the readability.
Unit-1: Introduction to OOP

5. Encapsulation

Encapsulation in OOPs is the concept of binding fields (object state) and methods
(behavior) together as a single unit. Programming languages such as Java use encapsulation
in the form of classes. A class allows programmers to create objects with variables (data)
and behaviors (methods or functions). Since a class consists of data and methods packed
into a single unit, it is an example of encapsulation.

You might also like