Object Oriented Programming CS Class XII

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

2

Computer Science & Application (CSA)


Chapter 2 (Object Oriented Programming)
 Programming Paradigm: A programming paradigm defines the methodology of designing and implementing
programs using the key features and building block of a program.

 Procedural Programming
Procedural programming is a programming paradigm that allows you to design your code using a structured approach. It
mainly focuses on a sequence of procedures, routines, or subroutines to perform a specific task or to solve a particular
problem. It follows Top-down approach. In procedural programming, the program is divided into small parts called
functions.

 Object Based Programming


Object-based languages need not support inheritance or subtyping, but those that do are also termed object-oriented.
Object-based languages that do not support inheritance or subtyping are usually not considered to be true object-oriented
languages.

 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.
In object-oriented programming, the program is divided into small parts called objects. It follows bottom-up approach.

 Object: An object is an identifiable entity with some characteristics and behavior.


 Class: A Class is a group of objects that share common properties and relationship.

Basic Concepts of OOP


Object oriented programming is a type of programming which uses objects and classes its functioning. The object
oriented programming is based on real world entities like inheritance, polymorphism, data hiding, etc.
Some basic concepts of object oriented programming are –

 ENCAPSULATION
 POLYMORPHISM
 INHERITANCE
 ABSTRACTION
 MODULARITY
Page 1|6
2

 DATA ABSTRACTION: Abstraction refers to the act of


representing essential features without including the background
details or explaination. 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 the accelerator, brakes, etc in the car.

 DATA ENCAPSULATION: Encapsulation is


defined as wrapping up of data and information under a
single unit. In Object Oriented Programming, Encapsulation
is defined as binding together the data and the functions that
manipulates them.
Consider a real life example of encapsulation, in a
company there are different sections like the accounts,
finance and sales section etc. The finance section handles all
the financial transactions, the sales section handles all the
sales related activities and keep records of all the sales. Now,
for some reason an official from finance section needs all the
data about sales in a particular month. In this case, he is not
allowed to directly access the data of sales section. He will
first have to contact some other officer in the sales section
and then request him to give the particular data. This is what encapsulation is. Here the data of sales section and the
employees that can manipulate them are wrapped under a single name “sales section”.
We can not access any function from class directly. We need an object to access that function which is using
the member the variable of that class.
In C++ encapsulation can be implemented using Class and access modifiers. Here is a program to show the
implementation of encapsulation.

#include<iostream>
using namespace std;
class Encapsulation
{
private:
int x; // data hidden from outside world

public:

void set(int a)
{
x =a; // function to set value of variable x
}

int get()
{
return x; // function to return value of variable x
}
};
Page 2|6
2

// driver code
int main()
{
Encapsulation obj;
obj.set(5);
cout<<obj.get();
return 0;
}

OUTPUT: 5
In the above program the variable x is made private. This variable can be accessed and manipulated only using the
functions get() and set() which are present inside the class. Thus we can say that here, the variable x and the functions
get() and set() are binded together which is nothing but encapsulation.

 MODULARITY: Modularity is the property of a system that has been decomposed into a set of cohesive
and loosely coupled modules. Modularity is closely tied with encapsulation; think of modularity as a way of mapping
encapsulated abstractions into real, physical modules.

 INHERITANCE: The capability of a class to derive properties and


characteristics from another class is called Inheritance. It 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. The derived class is the specialized class for the base
class.

 Sub Class: The class that inherits properties from another class is called
Subclass or Derived Class.
 Super Class: The class whose properties are inherited by a subclass is called Base Class or Super Class.

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.
To avoid the chances of errors and data
redundancy, 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. 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).

 POLYMORPHISM: This word is the combination of "poly," which means many + "morphs," which
means forms, which together means many forms. Polymorphism in C++ is when the behavior of the same object or
function is different in different contexts. Let's take a real-world example of the word right can mean different things in a
different context. Polymorphism is the capability for a data to be processed in more than one form.
Page 3|6
2

I was right.
In the above sentence, the word right means "correct".
Please take a right turn. The word right refers to the "right direction" in the above sentence.
Example Of Polymorphism

The + operator in C++ is one of the best examples of polymorphism.


#include <iostream>
using namespace std;
int main() {
int a = 10;
int b = 32;
cout << "Value of a + b is: " << a + b;
return 0;
}
Output:
Value of a + b is: 42

However, + is also used to concatenate two string operands.


#include <iostream>
using namespace std;
int main() {
string a = "poly";
string b = "morphism";
cout << "Value of a + b is: " << a + b;
return 0;
}
Output:
Value of a + b is: polymorphism

 DATA HIDING & DATA ENCAPSULATION


 What’s the Difference Between Data Hiding and Data Encapsulation?
Since both data protection concepts work together for a common purpose, namely the auxiliary gatekeepers of
sensitive information, the terms are frequently used interchangeably in object-oriented programming.

 The main difference between data hiding in C++ and encapsulation is that data hiding focuses on improving data
security within the program, while encapsulation deals with hiding the program’s complexity.
 Hidden data must be designated as private only. The data in data encapsulation can be public or private.
 Data encapsulation focuses on wrapping (i.e., encapsulating) complex data to offer a simplified perspective to the
users, while data hiding focuses on restricting a program’s data use to ensure data security.
 Data encapsulation targets how the data is accessed and how different objects behave. Data hiding focuses on object
member accessibility within a class, and information hiding is the most common encapsulation method. The
programmer conceals the object member's structure and its implementation methods.
 Data encapsulation is defined as a sub-process of data hiding, and data hiding is both a process and an overall strategy.
So, all data encapsulation is considered data hiding, but not all data hiding is considered data encapsulation.

 Applications of Data Hiding in C++


 Academic records and grade transcripts
 Bank account numbers and balances
 Medical records
 Corporate financial data

Page 4|6
2

 Benefits of Data Hiding in C++


 Data hiding minimizes data complexity and unpredictability
 Data hiding increases the program’s reusability
 It reduces system complexity while increasing the system’s robustness by minimizing interdependencies between its
software components
 It conceals the data’s physical hoarding architecture, which aids in clearly defining the interface, as it improves
reading and comprehension
 Data hiding protects data from unauthorized access and data corruption. To put it simpler, data hiding helps conceal
sensitive information, ensuring enhanced security measures against intruders and hackers.
 The encapsulated classes are simple, easily manageable, and make it easier to develop future applications

 ADVANTAGES OF OOP

 Productivity of software development increased: Object-arranged writing computer programs is measured, as it


gives detachment of obligations in object-based program advancement. It is additionally extensible, as articles can
be stretched out to incorporate new qualities and practices. Items can likewise be reused inside and across
applications. Due to these three variables – particularity, extensibility, and reusability – object-situated
programming gives further developed programming advancement usefulness over conventional strategy based
programming methods.
 Software maintenance improved: For the reasons referenced above, object-oriented programming is likewise
simpler to keep up with. Since the plan is secluded, a piece of the framework can be refreshed if there should arise
an occurrence of issues without a need to roll out huge scope improvements.
 Quicker improvement: Reuse empowers quicker advancement. Object-situated programming dialects accompany
rich libraries of articles, and code created during projects is additionally reusable in later ventures.
 Cost of development lowered: The reuse of programming likewise brings down the expense of advancement.
Normally, more exertion is placed into the article situated examination and plan, which brings down the general
expense of improvement.
 Good quality software: Faster improvement of programming and lower cost of advancement permits additional
time and assets to be utilized in the confirmation of the product. Albeit quality is reliant upon the experience of the
groups, object-situated programming will in general bring about greater programming.

 DISADVANTAGES OF OOP

 Steep expectation to learn and adapt: The perspective engaged with object-situated programming may not be
normal for certain individuals, and it can invest in some opportunity to become accustomed to it. It is complex to
make programs in view of the cooperation of articles. A portion of the key programming procedures, like
inheritance and polymorphism, can be tested to appreciate at first.
 Bigger program size: Object-arranged programs commonly include more lines of code than procedural projects.
 More slow projects: Object-arranged programs are normally slower than procedure-based programs, as they
ordinarily require more guidelines to be executed.
 Not appropriate for a wide range of issues: There are issues that loan themselves well to useful programming
style, rationale programming style, or strategy based programming style, and applying object-arranged
programming in those circumstances will not bring about effective projects.

Advantages Disadvantages
We can reuse the code multiple times using class Size is larger than other programs
Inherit the class to subclass for data redundancy It required a lot of effort to create
It is easy to maintain and modify It is slower than other programs
It maintains the security of data It is not suitable for some sorts of problems
Low-cost development It takes time to get used to it.

Page 5|6
2

 Disadvantages of Data Hiding in C++


There’s only one disadvantage to the process of data hiding. It requires extra coding, so programmers
must write more extended codes. However, considering the weight of all its benefits, it’s a small price to pay in
the long run.

Page 6|6

You might also like