Вы находитесь на странице: 1из 26

Introduction of Programming Paradigms 

Paradigm​ can also be termed as method to solve some problem or do some task. 

Programming paradigm is an approach to solve problem using some programming 

language or also we can say it is a method to solve a problem using tools and 

techniques that are available to us following some approach. There are lots for 

programming language that are known but all of them need to follow some strategy 

when they are implemented and this methodology/strategy is paradigms. Apart from 

varieties of programming language there are lots of paradigms to fulfil each and every 

demand. They are discussed below: 

1. Imperative programming paradigm: 

It is one of the oldest programming paradigm. It features close relation relation to 

machine architecture. It is based on Von Neumann architecture. It works by changing 

the program state through assignment statements. It performs step by step task by 

changing state. The main focus is on how to achieve the goal. The paradigm consist of 

several statements and after execution of all the result is stored. 

 
 
Advantage: 

1. Very simple to implement 


2. It contains loops, variables etc. 

Disadvantage: 

1. Complex problem cannot be solved 


2. Less efficient and less productive 
3. Parallel programming is not possible 

Examples of ​Imperative​ programming paradigm:

C​ : developed by Dennis Ritchie and Ken Thompson


Fortan​ : developed by John Backus for IBM

Basic​ : developed by John G Kemeny and Thomas E Kurtz 


// average of five number in C

int marks[5] = { 12, 32, 45, 13, 19 } int sum = 0;

float average = 0.0;

for (int i = 0; i < 5; i++) {

sum = sum + marks[i];

average = sum / 5;

Imperative programming is divided into three broad categories: Procedural, OOP and 

parallel processing. These paradigms are as follows: 

Procedural programming paradigm – 


This  paradigm  emphasizes  on  procedure  in  terms  of  under  lying machine model. There 
is  no  difference  in  between  procedural  and  imperative  approach.  It  has  the  ability  to 
reuse  the  code  and  it  was  boon  at  that  time  when  it  was  in  use  because  of  its 
reusability. 
Examples of ​Procedural​ programming paradigm:

C​ : developed by Dennis Ritchie and Ken Thompson

C++​ : developed by Bjarne Stroustrup

Java​ : developed by James Gosling at Sun Microsystems


#include <iostream>

using namespace std;

int main()

int i, fact = 1, num;

cout << "Enter any Number: ";

cin >> number;

for (i = 1; i <= num; i++) {

fact = fact * i;

cout << "Factorial of " << num << " is: " << fact <<
endl;

return 0;

Then comes OOP, 


1. Object oriented programming – 
The program is written as a collection of classes and object which are 
meant for communication. The smallest and basic entity is object and all 
kind of computation is performed on the objects only. More emphasis is on 
data rather procedure. It can handle almost all kind of real life problems 
which are today in scenario. 
Advantages: 
○ Data security 
○ Inheritance 
○ Code reusability 
○ Flexible and abstraction is also present 
Examples of ​Object Oriented​ programming paradigm:

Simula​ : first OOP language

Java​ : developed by James Gosling at Sun Microsystems

C++​ : developed by Bjarne Stroustrup

Visual Basic .NET​ : developed by Microsoft

Python​ : developed by Guido van Rossum

Ruby​ : developed by Yukihiro Matsumoto

Smalltalk​ : developed by Alan Kay, Dan Ingalls, Adele Goldberg 

Parallel processing approach – 


Parallel processing is the processing of program instructions by dividing them among 
multiple processors. A parallel processing system posses many numbers of processor 
with the objective of running a program in less time by dividing them. This approach 
seems to be like divide and conquer. Examples are NESL (one of the oldest one) and 
C/C++ also supports because of some library function. 

Object Oriented Modeling and Design 

Intention of object oriented modeling and design is to learn how to apply object 

-oriented concepts to all the stages of the software development life 

cycle.Object-oriented modeling and design is a way of thinking about problems using 

models organized around real world concepts. The fundamental construct is the object, 

which combines both data structure and behavior. 

Purpose of Models: 

1. Testing a physical entity before building it 


2. Communication with customers 
3. Visualization 
4. Reduction of complexity 
Types of Models: 

There are 3 types of models in the object oriented modeling and design are: Class 

Model, State Model, and Interaction Model. These are explained as following below. 

 
 
1. Class Model: 
The class model shows all the classes present in the system. The class 
model shows the attributes and the behavior associated with the objects. 
The class diagram is used to show the class model.The class diagram 
shows the class name followed by the attributes followed by the functions 
or the methods that are associated with the object of the class.Goal in 
constructing class model is to capture those concepts from the real world 
that are important to an application. 
2. State Model: 
State model describes those aspects of objects concerned with time and 
the sequencing of operations – events that mark changes, states that 
define the context for events, and the organization of events and 
states.Actions and events in a state diagram become operations on objects 
in the class model. State diagram describes the state model. 
3. Interaction Model: 
Interaction model is used to show the various interactions between objects, 
how the objects collaborate to achieve the behavior of the system as a 
whole. 
The following diagrams are used to show the interaction model: 
○ Use Case Diagram 
○ Sequence Diagram 
○ Activity Diagram  

 
Object Oriented Design 

Object oriented design​ started right from the moment computers were invented. 

Programming was there, and programming approaches came into the picture. 

Programming is basically giving certain instructions to the computer. 

At the beginning of the computing era, programming was usually limited to machine 

language programming. Machine language means those sets of instructions that are 

specific to a particular machine or processor, which are in the form of 0’s and 1’s. These 

are sequences of bits (0100110…). But it’s quite difficult to write a program or develop 

software in machine language. 

It’s actually impossible to develop software used in today’s scenarios with sequences of 

bits. This was the main reason programmers moved on to the next generation of 

programming languages, developing assembly languages, which were near enough to 

the English language to easily understand. These assembly languages were used in 

microprocessors. With the invention of the microprocessor, assembly languages 

flourished and ruled over the industry, but it was not enough. Again, programmers came 

up with something new, i.e., structured and procedural programming. 

 
 

Structured Programming – 

The basic principle of the structured programming approach is to divide a program into 

functions and modules. The use of modules and functions makes the program more 

understandable and readable. It helps to write cleaner code and to maintain control over 

the functions and modules. This approach gives importance to functions rather than 

data. It focuses on the development of large software applications, for example, C was 

used for modern operating system development. The programming languages: PASCAL 

(introduced by Niklaus Wirth) and C (introduced by Dennis Ritchie) follow this approach. 

Procedural Programming Approach – 

This approach is also known as the top-down approach. In this approach, a program is 

divided into functions that perform specific tasks. This approach is mainly used for 

medium-sized applications. Data is global, and all the functions can access global data. 

The basic drawback of the procedural programming approach is that data is not 

secured because data is global and can be accessed by any function. Program control 

flow is achieved through function calls and goto statements. The programming 

languages: FORTRAN (developed by IBM) and COBOL (developed by Dr Grace Murray 

Hopper) follow this approach. 

These programming constructs were developed in the late 1970s and 1980s. There 

were still some issues with these languages, though they fulfilled the criteria of 

well-structured programs, software etc. They were not as structured as the 

requirements were at that time. They seem to be over-generalised and don’t correlate 

with real-time applications. 


To solve such kinds of problems, OOP, an object-oriented approach was developed as a 

solution. 

The Object-Oriented Programming (OOP) Approach – 

The OOP concept was basically designed to overcome the drawback of the above 

programming methodologies, which were not so close to real-world applications. The 

demand was increased, but still, conventional methods were used. This new approach 

brought a revolution in the programming methodology field. 

Object-oriented programming (OOP) is nothing but that which allows the writing of 

programs with the help of certain classes and real-time objects. We can say that this 

approach is very close to the real-world and its applications because the state and 

behaviour of these classes and objects are almost the same as real-world objects. 

Let’s go deeper into the general concepts of OOP, which are given below: 

 
What Are Class & Object? 

It is the basic concept of OOP; an extended concept of the structure used in C. It is an 

abstract and user-defined data type. It consists of several variables and functions. The 

primary purpose of the class is to store data and information. The members of a class 

define the behaviour of the class. A class is the blueprint of the object, but also, we can 

say the implementation of the class is the object. The class is not visible to the world, 

but the object is. 

Class car
{
int car_id;
char colour[4];
float engine_no;
double distance;

void distance_travelled();
float petrol_used();
char music_player();
void display();
}
Here,  the  class  car  has  properties  car_id,  colour,  engine_no  and  distance.  It  resembles 
the  real-world  car  that  has  the  same  specifications,  which  can  be  declared  public 
(visible  to  everyone  outside  the  class),  protected  and  private  (visible  to  none).  Also, 
there  are  some  methods  such as distance_travelled(), petrol_used(), music_player() and 
display(). In the code given below, the car is a class and c1 is an object of the car. 
 
class car {

public:
int car_id;
double distance;

void distance_travelled();

void display(int a, int b)


{
cout << "car id is=\t" << a << "\ndistance travelled
=\t" << b + 5;
}
};
int main()
{
car c1; // Declare c1 of type car
c1.car_id = 321;
c1.distance = 12;
c1.display(321, 12);
return 0;
}

Data Abstraction – 

Abstraction refers to the act of representing important and special features without 

including the background details or explanation about that feature. Data abstraction 

simplifies database design. 

 
 

1. Physical Level: 
It describes how the records are stored, which are often hidden from the 
user. It can be described with the phrase, “block of storage.” 
2. Logical Level: 
It describes data stored in the database and the relationships between the 
data. The programmers generally work at this level as they are aware of the 
functions needed to maintain the relationships between the data. 
3. View Level: 
Application programs hide details of data types and information for security 
purposes. This level is generally implemented with the help of GUI, and 
details that are meant for the user are shown. 

Encapsulation – 

Encapsulation is one of the fundamental concepts in object-oriented programming 

(OOP). It describes the idea of wrapping data and the methods that work on data within 

one unit, e.g., a class in Java. This concept is often used to hide the internal state 

representation of an object from the outside. 


Inheritance – 

Inheritance is the ability of one class to inherit capabilities or properties of another 

class, called the parent class. When we write a class, we inherit properties from other 

classes. So when we create a class, we do not need to write all the properties and 

functions again and again, as these can be inherited from another class which 

possesses it. Inheritance allows the user to reuse the code whenever possible and 

reduce its redundancy. 

 
 

 
 

Polymorphism – 

Polymorphism is the ability of data to be processed in more than one form. It allows the 

performance of the same task in various ways. It consists of method overloading and 

method overriding, i.e., writing the method once and performing a number of tasks 

using the same method name. 

 
#include <iostream>
using namespace std;

void output(float);
void output(int);
void output(int, float);

int main()
{
cout << "\nGfG!\n";
int a = 23;
float b = 2.3;

output(a);
output(b);
output(a, b);

return 0;
}

void output(int var)


{ // same function name but different task
cout << "Integer number:\t" << var << endl;
}

void output(float var)


{ // same function name but different task
cout << "Float number:\t" << var << endl;
}

void output(int var1, float var2)


{ // same function name but different task
cout << "Integer number:\t" << var1;
cout << " and float number:" << var2;
}
 

 
Some important points to know about OOP: 

1. OOP treats data as a critical element. 


2. Emphasis is on data rather than procedure. 
3. Decomposition of the problem into simpler modules. 
4. Doesn’t allow data to freely flow in the entire system, ie localized control 
flow. 
5. Data is protected from external functions. 

Advantages of OOPs – 

● It models the real world very well. 


● With OOP, programs are easy to understand and maintain. 
● OOP offers code reusability. Already created classes can be reused without 
having to write them again. 
● OOP facilitates the quick development of programs where parallel 
development of classes is possible. 
● With OOP, programs are easier to test, manage and debug. 

Disadvantages of OOP – 

● With OOP, classes sometimes tend be over-generalised. 


● The relations among classes become superficial at times. 
● The OOP design is tricky and requires appropriate knowledge. Also, one 
needs to do proper planning and design for OOP programming. 
● To program with OOP, the programmer needs proper skills such as that of 
design, programming and thinking in terms of objects and classes etc. 

 
 

Characteristics of an Object Oriented Programming language 

Class​: The building block of C++ that leads to Object-Oriented programming is a Class. 

It is a user-defined data type, which holds its own data members and member functions, 

which can be accessed and used by creating an instance of that class. A class is like a 

blueprint for an object. 

For Example: Consider the Class of Cars. There may be many cars with different names 

and brand but all of them will share some common properties like all of them will have 4 
wheels, Speed Limit, Mileage range etc. So here, Car is the class and wheels, speed 

limits, mileage are their properties. 

● 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 define the properties and behaviour 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. 

We can say that a ​Class in C++​ is a blue-print representing a group of objects which 

shares some common properties and behaviours. 

Object: A
​ n Object is an identifiable entity with some characteristics and behaviour. 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. 

class person
{
char name[20];
int id;
public:
void getdetails(){}
};

int main()
{
person p1; // p1 is a object
}

Object take up space in memory and have an associated address like a record in pascal 

or structure or union in C. 


When a program is executed the objects interact by sending messages to one another. 

Each object contains data and code to manipulate the data. Objects can interact 

without having to know details of each other’s data or code, it is sufficient to know the 

type of message accepted and type of response returned by the objects. 

Encapsulation​: In normal terms, 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 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. 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. Now there may arise a situation when for some reason an 

official from the 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 the 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 the sales section and the 

employees that can manipulate them are wrapped under a single name “sales section”. 

Encapsulation also leads to ​data abstraction or hiding​. As using encapsulation also 

hides the data. In the above example, the data of any of the section like sales, finance or 

accounts are hidden from any other section. 

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 accelerators will increase the speed of the car or applying brakes will stop the car 
but he does not know about how on pressing accelerator the speed is actually 

increasing, he does not know about the inner mechanism of the car or the 

implementation of 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. 

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 person at the same time can have different characteristic. Like a man at the same 

time is a father, a husband, an employee. So the same person posses different 

behaviour in different situations. This is called polymorphism. 

An operation may exhibit different behaviours in different instances. The behaviour 

depends upon the types of data used in the operation. 

C++ supports operator overloading and function overloading. 

● Operator Overloading​: ​The process of making an operator to exhibit 


different behaviours in different instances is known as operator 
overloading. 
● Function Overloading​:​ Function overloading is using a single function name 
to perform different types of tasks. 
Polymorphism is extensively used in implementing inheritance. 

Example​: Suppose we have to write a function to add some integers, some times there 

are 2 integers, some times there are 3 integers. We can write the Addition Method with 

the same name having different parameters, the concerned method will be called 

according to parameters. 

 
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.

● 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 sub class is called 
Base Class or Super class. 
● 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. By doing this, we are reusing the fields and methods of the existing 
class. 
Example​: Dog, Cat, Cow can be Derived Class of Animal Base Class. 

 
 

Dynamic Binding:​ In dynamic binding, the code to be executed in response to function 

call is decided at runtime. C++ has ​virtual functions​ to support this. 

Message Passing:​ Objects communicate with one another by sending and receiving 

information to each other. A message for an object is a request for execution of a 

procedure and therefore will invoke a function in the receiving object that generates the 

desired results. Message passing involves specifying the name of the object, the name 

of the function and the information to be sent. 

Difference between Abstraction and Encapsulation in C++ 

Abstraction: 

In OOPs, Abstraction is that the method of getting info. the information needed 

will be taken in such the simplest way that solely the required components are 

extracted, and also the ones that are considered less significant are unnoticed. 

Or the unessential info will be drained dead set keep solely the sensitive 

information intact. during this case, most of the classes don’t have any sort of 

implementation, and most of the problem-solving method is completed at the 

interface stage. one thing that doesn’t exist and is just a concept is named 

Abstraction. In abstraction, implementation complexities are hidden using 

abstract classes and interfaces. 

 
Example of Abstraction: 

#include <iostream>
using namespace std;

class Summation {
private:
// private variables
int a, b, c;
public:
void sum(int x, int y)
{
a = x;
b = y;
c = a + b;
cout<<"Sum of the two number is : "<<c<<endl;
}
};
int main()
{
Summation s;
s.sum(5, 4);
return 0;

Output: 
 
 
Sum of the two number is: 9
In the this example, we can see that abstraction has achieved by using class. The 

class 'Summation' holds the private members a, b and c, which are only 

accessible by the member functions of that class. 

Encapsulation: 

Encapsulation is the process or method to contain the information. The info, it 

provides is that the solely the one that is critical and every one the opposite data 

that is unsuitable is hidden already. during this case, the matter determination is 

completed at the stage of implementation. Encapsulation is a method to hide the 

data in a single entity or unit along with a method to protect information from 

outside. Encapsulation keeps something in an exceedingly capsule and showing 

of solely the essential options of a product. for example, once someone is 
mistreatment software package they are doing not recognize what the secret 

writing is, they solely use the mandatory functions that result from the secret 

writing that is unbroken safe within the files. 

Example of Encapsulation: 

#include <iostream>
using namespace std;

class EncapsulationExample {
private:
// we declare a as private to hide it from outside
int a;

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

// get() function to return the value of a


int get()
{
return a;
}
};

// main function
int main()
{
EncapsulationExample e1;

e1.set(10);

cout<<e1.get();
return 0;
}
Output: 

10
In the this program, the variable a is made private so that this variable can be 

accessed and manipulated only by using the methods get() and set() that are 

present within the class. Therefore we can say that, the variable a and the 

methods set() as well as get() have binded together that is nothing but 

encapsulation. 

##Difference between Abstraction and Encapsulation: 

S.NO  ABSTRACTION  ENCAPSULATION 

Abstraction is the  While encapsulation is the process 


1
process or method of  or method to contain the 

gaining the information.  information. 

In abstraction, problems  While in encapsulation, problems 


2
are solved at the design  are solved at the implementation 

or interface level.  level. 

Whereas encapsulation is a method 


Abstraction is the 
3 to hide the data in a single entity or 
method of hiding the 
.  unit along with a method to protect 
unwanted information. 
information from outside. 

4
We can implement  Whereas encapsulation can be 

abstraction using  implemented using by access 
abstract class and  modifier i.e. private, protected and 

interfaces.  public. 

In abstraction, 

implementation  While in encapsulation, the data is 


5
complexities are hidden  hidden using methods of getters 

using abstract classes  and setters. 

and interfaces. 

The objects that help to  Whereas the objects that result in 
6
perform abstraction are  encapsulation need not be 

encapsulated.  abstracted. 

Вам также может понравиться