Oop L-1

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

What is Object Oriented Programming?

Older programming languages like C followed the Procedural Programming


approach. The program written using these languages used to be a series of step by
step instructions. They made use of procedures/subroutines for making the
program modular. This programming paradigm focused on logic more than data
and the program used to combine both of them together.

Modern programming languages like C++, Java, C# etc. follow the Object
Oriented approach. In object oriented programming, importance is given to data
rather than just writing instructions to complete a task. An object is a thing or
idea that you want to model in your program. An object can be anything, example,
employee, bank account, car etc.

Class, Object
For getting started with object oriented programming we would have to know what
is a class and object and the difference between them. A class is a blueprint for
creating an object. It is similar to the blue print of a house.

A class defines attributes and behavior. If we are to model a car in our application
then we could define attributes of the car like, model, fuel, make and behaviors
like start, break, accelerate etc.. If you notice, the attributes and behavior that we
are specifying are not specific to just one model of car. We are trying to generalize
a car by stating that the car which we are going to model in our program will have
these number of attributes and behavior.
Example, using the same class "Car" we can create different objects having
variation in model, fuel type and make year while having the same common
behavior.

Object 1 Object 2

Model Volkswagen Polo Model Volkswagen Vento

Fuel Petrol Fuel Diesel

Make 2017 Make 2017

Start() Start()
Break() Break()
Accelerate() Accelerate()

In this way, Object oriented programming allows you to easily model real world
complex system behavior. With OOP, data and functions (attributes and methods)
are bundled together within the object. This prevents the need for any shared or
global data with OOP, which is a core difference between the object oriented and
procedural approaches.

 A Class is a user defined data-type which has data members and member
functions.
 Data members are the data variables and member functions are the
functions used to manipulate these variables and together these data
members and member functions defines the properties and behavior of
the objects in a Class.

An Object is an instance of a Class. When a class is defined, no memory is


allocated but when it is instantiated (i.e. an object is created) memory is allocated.

From the same Car class, we created three individual objects with the name
of: Mercedes, Bmw, and Audi.
Properties of Object
State

 The state of an object consists of a set of data fields (its properties) with their
current values.

Behavior

 “Behavior is how an object acts and reacts in terms of its state changes and
message passing.”

Identity

 “Identity is that property of an object which distinguishes it from all others.”

 Every instance of a class has its own memory to hold its state.

What are the main principles of Object-Oriented Programming?

Inheritance

Inheritance is a feature of object-oriented programming that allows code


reusability when a class includes property of another class. Considering
HumanBeing a class, which has properties like hands, legs, eyes, mouth, etc, and
functions like walk, talk, eat, see etc.

Man and Woman are also classes, but most of the properties and functions are
included in HumanBeing. Hence, they can inherit everything from class
HumanBeing using the concept of Inheritance.

Abstraction

Abstraction means, showcasing only the required things to the outside world while
hiding the details. Continuing our example, Human Being‟s can talk, walk, hear,
eat, but the details of the muscles mechanism and their connections to the brain are
hidden from the outside world.

The concept of abstraction focuses on what an object does, instated of how an


object is represented or “how it works.” Thus, data abstraction is often used for
managing large and complex programs.

Encapsulation

Encapsulation means that we want to hide unnecessary details from the user. For
example, when we call from our mobile phone, we select the number and press call
button. But the entire process of calling or what happens from the moment we
press or touch the call button to the moment we start having a phone conversation
is hidden from us.

Polymorphism

Polymorphism is a concept, which allows us to redefine the way something works,


by either changing how it is done or by changing the parts used to get it done.
Procedural Programming Languages:

These languages programs in such a way that the program executes statement by
statement, reading and modifying a shared memory.the program communicate
with the OS are through a set of procedure, or function, which in turn is a group of
specific instructions executed one after the other. The data was quite
separate from the procedures and the challenge was to keep track of the order in
which the functions are called and what data was changed.

Ex: Pascal, Fortran , COBOL

Structured Programming languages:

These are based on the top down methodology in which a system is


further divided into subsystem these are Typically Called as Functions. The
structured programming concept was formalized in the year 1966 by Corrado
Böhm and Giuseppe Jacopini.

Structured programming is not only limited to the top down approach. It employs
methods using:-

1. Top down analysis for problem solving: It focuses on dividing the problem
into sub parts and hence simplifies the problem solving.

2. Modularization for program structure and organization: It organizes large


instructions by breaking them into separate and smaller section of modules,
sub routines and subprograms.

3. Structured code for the individual modules Control structures are used to
determine the exact order in which the set of instructions are to be executed.

4. By Introducing Modularity maintaining of Code and Debugging of Codes is


became Easy.and Readability of Code also Increased.

Ex: C.
Structured Programming Object Oriented
Programming
Structured Programming is designed which focuses on process/ logical Object Oriented Programming is designed which focuses
structure and then data required for that process. on data.

Structured programming follows top-down approach. Object oriented programming follows bottom-up approach.

Structured Programming is also known as Modular Programming and a Object Oriented Programming supports inheritance,
subset of procedural programming language. encapsulation, abstraction, polymorphism, etc.

In Structured Programming, Programs are divided into small self In Object Oriented Programming, Programs are divided into
contained functions. small entities called objects.

Structured Programming is less secure as there is no way of data hiding. Object Oriented Programming is more secure as having data
hiding feature.

Structured Programming can solve moderately complex programs. Object Oriented Programming can solve any complexprograms.

Structured Programming provides less reusability, more function Object Oriented Programming provides more reusability, less
dependency. function dependency.

Less abstraction and less flexibility. More abstraction and more flexibility.

Advantage of OOPs over Procedure-oriented programming language

1) OOPs makes development and maintenance easier whereas in a procedure-


oriented programming language it is not easy to manage if code grows as project
size increases.

2) OOPs provides data hiding whereas in a procedure-oriented programming


language a global data can be accessed from anywhere.

3) OOPs provides the ability to simulate real-world event much more effectively.
We can provide the solution of real word problem if we are using the Object-
Oriented Programming language.
What is the difference between an object-oriented programming language and
object-based programming language?

Object-based programming language follows all the features of OOPs except


Inheritance. JavaScript and VBScript are examples of object-based programming
languages.

Advantages of OOP
Moving to the advantages of OOP, we would like to say that there are many as this
is one of the core development approaches which is widely accepted. Let‟s see
what are the advantages of OOP offers to its users.

1. Re-usability

It means reusing some facilities rather building it again and again. This is done
with the use of a class. We can use it „n‟ number of times as per our need.

2. Data Redundancy

This is a condition created at the place of data storage (you can say
Databases)where the same piece of data is held in two separate places. So the data
redundancy is one of the greatest advantages of OOP. If a user wants a similar
functionality in multiple classes he/she can go ahead by writing common class
definitions for the similar functionalities and inherit them.

3. Code Maintenance

This feature is more of a necessity for any programming languages, it helps users
from doing re-work in many ways. It is always easy and time-saving to maintain
and modify the existing codes with incorporating new changes into it.

4. Security

With the use of data hiding and abstraction mechanism, we are filtering out limited
data to exposure which means we are maintaining security and providing necessary
data to view.
5. Design Benefits

If you are practicing on OOPs the design benefit a user will get is in terms of
designing and fixing things easily and eliminating the risks (if any). Here the
Object Oriented Programs forces the designers to have a longer and extensive
design phase, which results in better designs and fewer flaws. After a time when
the program has reached some critical limits, it is easier to program all the non-
OOP‟s one separately.

6. Better productivity

with the above-mentioned facts of using the application definitely enhances its
users overall productivity. This leads to more work done, finish a better program,
having more inbuilt features and easier to read, write and maintain. An OOP
programmer cans stitch new software objects to make completely new programs. A
good number of libraries with useful functions in abundance make it possible.

7. Polymorphism Flexibility

Let‟s see a scenario to better explain this behavior.

You behave in a different way if the place or surrounding gets change. A person
will behave like a customer if he is in a market, the same person will behave like a
student if he is in a school and as a son/daughter if put in a house. Here we can see
that the same person showing different behavior every time the surroundings are
changed. This means polymorphism is flexibility and helps developers in a number
of ways.

 It‟s simplicity

 Extensibility

8. Problems solving

Decomposing a complex problem into smaller chunks or discrete components is a


good practice. OOP is specialized in this behavior, as it breaks down your software
code into bite-sized – one object at a time. In doing this the broken components
can be reused in solutions to different other problems (both less and more
complex) or either they can be replaced by the future modules which relate to the
same interface with implementations details.

Disadvantages of Object Oriented Programming Language :

 Sometimes, the relation among the classes become artificial in nature.


 Designing a program in OOP concept is a little bit tricky.

 The programmer should have a proper planning before designing a program


using OOP approach.

 Since everything is treated as objects in OOP, the programmers need proper skill
such as design skills, programming skills, thinking in terms of objects etc.

 The size of programmes developed with OOP is larger than the procedural
approach.

 Since larger in size, that means more instruction to be executed, which results in
the slower execution of programmes.
C Program to Swap Numbers Using Temporary
Variable
#include <stdio.h>
int main()
{
double firstNumber, secondNumber, temporaryVariable;

printf("Enter first number: ");


scanf("%lf", &firstNumber);

printf("Enter second number: ");


scanf("%lf",&secondNumber);

// Value of firstNumber is assigned to temporaryVariable


temporaryVariable = firstNumber;

// Value of secondNumber is assigned to firstNumber


firstNumber = secondNumber;

// Value of temporaryVariable (which contains the initial value of


firstNumber) is assigned to secondNumber
secondNumber = temporaryVariable;

printf("\nAfter swapping, firstNumber = %.2lf\n", firstNumber);


printf("After swapping, secondNumber = %.2lf", secondNumber);

return 0;
}

Output
Enter first number: 1.20

Enter second number: 2.45

After swapping, firstNumber = 2.45

After swapping, secondNumber = 1.20

C++ : Swap Numbers (Using Temporary Variable)


#include <iostream>
using namespace std;

int main()
{
int a = 5, b = 10, temp;

cout << "Before swapping." << endl;


cout << "a = " << a << ", b = " << b << endl;

temp = a;
a = b;
b = temp;

cout << "\nAfter swapping." << endl;


cout << "a = " << a << ", b = " << b << endl;

return 0;
}
C++ addition program using class
1. #include <iostream>
2.
3. using namespace std;
4.
5. class Mathematics {
6. int x, y;
7.
8. public:
9. void input() {
10. cout << "Input two integers\n";
11. cin >> x >> y;
12. }
13.
14. void add() {
15. cout << "Result: " << x + y;
16. }
17.
18. };
19.
20. int main()
21. {
22. Mathematics m; // Creating an object of the
class
23.
24. m.input();
25. m.add();
26.
27. return 0;
28. }

You might also like