Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Object-Oriented Programming (OOP) in C++: Concepts and Benefits, Lecture notes of Software Development

A comprehensive introduction to object-oriented programming (oop) in c++, highlighting its key concepts, benefits, and differences with procedural programming. It delves into the core principles of oop, including classes, objects, inheritance, polymorphism, and message passing, explaining their significance and practical applications. The document also explores the advantages of oop, such as modularity, reusability, and code maintainability, while acknowledging potential drawbacks like performance overhead and dependency management. It concludes with a clear comparison table outlining the key differences between oop and procedural programming.

Typology: Lecture notes

2023/2024

Uploaded on 08/31/2024

anam-khurshid-1
anam-khurshid-1 🇮🇳

1 document

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Procedural programming
Procedural programming is a programming paradigm that views programs as a series of instructions
to be executed. It emphasizes breaking programs into named sets of instructions, called procedures,
which are similar to functions. Procedural programming focuses on the process of transforming input
into the desired output.
Some advantages of procedural programming include:
Modularity: Organizing procedures into separate modules, each with a specific purpose. This
can improve software quality by reducing the cognitive load of procedures and modules.
Code reusability: Programs can be compact and reusable.
Breaking down problems: Problems can be broken into smaller subproblems, making them
easier and faster to solve.
Flexibility: Procedural programming is more flexible than other options.
Some examples of procedural programming languages include:
C, Pascal, Fortran, ALGOL, COBOL, and BASIC.
What is Object Oriented Programming?
OOP stands for Object-Oriented Programming.
Object-oriented Programming is a programming language that uses classes and objects to create
models based on the real-world environment.
As the name suggests oops breaks the program on the basis of the objects. It mainly works on Class,
Object, Polymorphism, Abstraction, Encapsulation and Inheritance.
OOP aim is to bind together the data and functions and operate on them.
objects contain data in the form of attributes(properties) and program codes in the form of functions
(methods, behaviour).
We have several types of object-oriented programming languages, but the most popular are
Java, C++, C#, Python etc.
Some of the well-known object-oriented languages are Objective C, Perl, Java, Python, Modula, Ada,
Simula, C++, Smalltalk and some Common Lisp Object Standard. Here we are discussing its benefits
on C++.
Benefits of OOP
Modularity: OOP divides complex systems into smaller components, making the codebase
easier to comprehend, create, and maintain.
Reusability: Inheritance allows code reuse, improving code quality and saving time.
Encapsulation: Protects data integrity and privacy by restricting direct access and allowing
controlled access through methods.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Object-Oriented Programming (OOP) in C++: Concepts and Benefits and more Lecture notes Software Development in PDF only on Docsity!

Procedural programming

Procedural programming is a programming paradigm that views programs as a series of instructions to be executed. It emphasizes breaking programs into named sets of instructions, called procedures, which are similar to functions. Procedural programming focuses on the process of transforming input into the desired output. Some advantages of procedural programming include:  Modularity: Organizing procedures into separate modules, each with a specific purpose. This can improve software quality by reducing the cognitive load of procedures and modules.  Code reusability: Programs can be compact and reusable.  Breaking down problems: Problems can be broken into smaller subproblems, making them easier and faster to solve.  Flexibility: Procedural programming is more flexible than other options. Some examples of procedural programming languages include: C, Pascal, Fortran, ALGOL, COBOL, and BASIC.

What is Object Oriented Programming?

OOP stands for Object-Oriented Programming. Object-oriented Programming is a programming language that uses classes and objects to create models based on the real-world environment. As the name suggests oops breaks the program on the basis of the objects. It mainly works on Class, Object, Polymorphism, Abstraction, Encapsulation and Inheritance. OOP aim is to bind together the data and functions and operate on them. objects contain data in the form of attributes(properties) and program codes in the form of functions (methods, behaviour). We have several types of object-oriented programming languages, but the most popular are

  • Java , C++ , C# , Python etc. Some of the well-known object-oriented languages are Objective C, Perl, Java, Python, Modula, Ada, Simula, C++, Smalltalk and some Common Lisp Object Standard. Here we are discussing its benefits on C++. Benefits of OOPModularity: OOP divides complex systems into smaller components, making the codebase easier to comprehend, create, and maintain.Reusability: Inheritance allows code reuse, improving code quality and saving time.Encapsulation: Protects data integrity and privacy by restricting direct access and allowing controlled access through methods.

Flexibility and Scalability: OOP enables easy addition and modification of features without impacting the entire codebase.Code Organization: OOP promotes a structured approach, enhancing collaboration and code readability.Code Maintenance: Changes and bug fixes can be made to specific objects or classes without affecting other parts of the system, reducing errors and improving debugging.Code Reusability: OOP encourages the development of reusable code elements, saving time and improving system reliability.Better Problem Solving: OOP models real-world systems, allowing developers to create intuitive solutions that closely mimic real-world circumstances.  Upgradation: OOP systems can be easily upgraded from small to large systems.  Message passing techniques is used for communication between objects which makes the interface descriptions with external systems much simpler.  The data-centered design approach enables us to capture more details of model in an implementable form. While it is possible to incorporate all these features in an OOP, their importance depends upon the type of project and preference of the programmer. This technology is still developing and current products may be superseded quickly. Developing a software is easy to use makes it hard to build.

Disadvantages of OOP

 Increased code size: The length of the programmes developed using OOP language is

much larger than the procedural approach. Since the programme becomes larger in

size, it requires more time to be executed that leads to slower execution of the

programme.

 Steeper learning curve: OOPs take time to get used to it. The thought process

involved in object-oriented programming may not be natural for some people. OOP

introduces complex concepts like classes, objects, inheritance, and polymorphism,

making it harder for new programmers to grasp and apply them effectively.

Everything is treated as object in OOP so before applying it we need to have excellent

thinking in terms of objects.

 Increased complexity : OOP's emphasis on modularity and code organization can

make larger projects more challenging to understand and maintain.

 Performance overhead : OOP languages often have a performance cost compared to

procedural languages due to the additional abstraction layers introduced by objects

and encapsulation.

 Dependency management : Inheritance and code reuse in OOP can create

interdependencies among classes, making it difficult to manage changes and maintain

overall functionality.

data hiding is possible and

hence it is more secure

than POP.

less secure as compare to

OOPs.

Complexity OOPs due to modularity in

its programs is less

complex and hence new

data objects can be created

easily from existing objects

making object-oriented

programs easy to modify

There is no simple process to

add data in procedural

programming, at least not

without revising the whole

program.

Program

division

OOP divides a program into

small parts and these parts

are referred to as objects.

Procedural programming

divides a program into small

programs and each small

program is referred to as a

function.

Importance OOP gives importance to

data rather than functions

or procedures.

Procedural programming

does not give importance to

data. In POP, functions along

with sequence of actions are

followed.

Inheritance OOP provides inheritance in

three modes i.e. protected,

private, and public

Procedural programming

does not provide any

inheritance.

Examples C++, C#, Java, Python, etc.

are the examples of OOP

languages.

C, BASIC, COBOL, Pascal, etc.

are the examples POP

languages.

Classes and Objects

classes and objects are basic concepts of Object-Oriented Programming (OOPs) that are used to represent real-world concepts and entities.

Class

The class represents a group of objects having similar properties and behaviour. For example, the animal type Dog is a class while a particular dog is an object of the Dog class. Another Example: Consider the Class of Cars. There may be many cars with different names and brands but all of them will share some common properties like all of them will have 4 wheels , Speed Limit , Mileage range, etc. So here, the Car is the class, and wheels, speed limits, and mileage are their properties (attributes or behaviour). applying brakes , increasing speed are functions A class in is a set of objects which shares common characteristics/ behavior and common properties/ attributes.

It is a user-defined blueprint or prototype from which objects are created. For example, Student is a class while a particular student named Ravi is an object. A Class has data members and member functions.  Data members are the data variables and member functions are the functions used to manipulate these variables 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 be applying brakes , increasing speed, etc. But we cannot use the class as it is. We first have to create an object of the class to use its features. Properties of Classes

  1. Class is not a real-world entity. It is just a template or blueprint or prototype from which objects are created.
  2. Class does not occupy memory.
  3. Class is a group of variables of different data types and a group of functions.
  4. A Class can contain:  Data member  function  Constructor

What is object

An Object is an instance of a Class. An Object is an identifiable entity with some characteristics and behaviour. When a class is defined, no memory is allocated but when it is instantiated (i.e. an object is created) memory is allocated.

What is data abstraction in OOP?

The process by which data and functions are defined in such a way that only essential details can be seen and unnecessary implementations are hidden is called Data Abstraction. Data Abstraction in means providing only the essential details to the outside world and hiding the internal details, i.e., hiding the background details or implementation. Abstraction is a programming technique that depends on the separation of the interface and implementation details of the program The users only interact with the basic implementation of the function, but inner working is hidden. User is familiar with that "what function does" but they don't know "how it does."

the base class from the three different subclasses and implemented the

abstract method differently. We created the objects to call the abstract

method.

What is Data encapsulation

Encapsulation is a fundamental object-oriented principle in Python Encapsulation is defined as the wrapping up of data and information in a single unit. In Object Oriented Programming, Encapsulation is defined as binding together the data and the functions that manipulate them. Encapsulation protects your classes from accidental changes or deletions and promotes code reusability and maintainability It also restricts direct access to some of an object's components. Encapsulation is also known as data hiding. Two Important properties of Encapsulation

  1. Data Protection: Encapsulation protects the internal state of an object by keeping its data members private. Access to and modification of these data members is restricted to the class’s public methods, ensuring controlled and secure data manipulation.
  2. Information Hiding: Encapsulation hides the internal implementation details of a class from external code. Only the public interface of the class is accessible, providing abstraction and simplifying the usage of the class while allowing the internal implementation to be modified without impacting external code.

Inheritance

Inheritance is one of important pillars of Object-Oriented Programming (OOPs). Inheritance is the property of OOP that allows creation of new class from existing class (or classes) It is a feature that enables a class to inherit data and member functions of another class. In inheritance two classes are involved one is parent class and another is derived class (child class) What Are Child and Parent Classes? To clearly understand the concept of Inheritance, you must learn about two terms on which the whole concept of inheritance is based - Child class and Parent class.  Derived or Child class: The class that inherits the characteristics of another class is known as the child class or derived class. A child class will access the data members of the parent class according to the visibility mode specified during the declaration of the child class.  Parent class: The class from which the child class inherits its properties is called the parent class or base class.

In python, a derived class can inherit base class by just mentioning the base in the bracket after the derived class name. Consider the following syntax to inherit a base class into the derived class. Syntax

  1. class derived- class name (base class name ):
  2. < class -suite> A class can inherit multiple classes by mentioning all of them inside the bracket. Inheritance allows developers to reuse your code since the derived class or the child class can reuse the members of the base class by inheriting them. Different Types of Inheritance OOP supports six different types of inheritance as given below:
  3. Single Inheritance
  4. Multi-level Inheritance
  5. Multiple Inheritance
  6. Multipath Inheritance
  7. Hierarchical Inheritance
  8. Hybrid Inheritance

What is polymorphism in oops?

The word "polymorphism" means "many forms", and in programming it refers to methods/functions/operators with the same name that can be executed on many objects or classes. Polymorphism describes the ability of something to have or to be displayed in more than one form. The different forms arise because these entities can be assigned different meanings and used in various ways in multiple contexts.

a) Function Polymorphism

An example of a Python function that can be used on different objects is the len() function.

class, which is known as Method Overriding. Polymorphism allows us to access these overridden methods and attributes that have the same name as the parent class.

d) Class Polymorphism in Python

Polymorphism is a very important concept in Object-Oriented Programming. We can use the concept of polymorphism while creating class methods as Python allows different classes to have methods with the same name. We can then later generalize calling these methods by disregarding the object we are working with.

Dynamic binding

In object-oriented programming, the concept of dynamic binding is closely related to polymorphism. dynamic binding is the process of resolving a method or attribute at runtime, instead of at compile time. According to the polymorphism feature, different objects respond differently to the same method call based on their implementations. This behaviour is achieved through method overriding, where a subclass provides its implementation of a method defined in its superclass. The Python interpreter determines which is the appropriate method or attribute to invoke based on the object's type or class hierarchy at runtime. This means that the specific method or attribute to be called is determined dynamically, based on the actual type of the object. Example The following example illustrates dynamic binding in Python – class shape: def draw(self): print ("draw method") return class circle(shape): def draw(self): print ("Draw a circle") return class rectangle(shape): def draw(self):

print ("Draw a rectangle") return shapes = [circle(), rectangle()] for shp in shapes: shp.draw() It will produce the following output − Draw a circle Draw a rectangle For the same statement in loop i.e. shp. draw() the draw() method is bound dynamically to the corresponding implementation based on the object's type. This is how dynamic binding is implemented in Python.

Message passing

In the realm of object-oriented programming, the concept of communication between objects is pivotal for building robust and modular software systems. One of the fundamental mechanisms facilitating this communication is message passing. A request for an object to perform one of its operations is called a message. Operation means method/function. All communication between objects done via message is called message passing Derived from the principles of encapsulation and interaction, message passing allows objects to interact by invoking methods or functions on each other. message passing is a mechanism that allows objects to communicate and interact with each other by sending messages typically in the form of method or function calls. Hence Message passing is the exchange of information between two or more objects via a logical entity known as a message. Message passing involves calling the functions of objects, which can lead to the exchange of information, execution of a specific behaviour, or modification of an object's state. This mechanism enables objects to interact and collaborate, facilitating the accomplishment of desired objectives. Message passing is different from conventional programming, where a process, subroutine, or function is directly invoked by name. In oops function member of the class cannot be invoked independently with its name only but it can be invoked with object name followed by (.) operator (member access operator) and function name along with arguments in the parenthesis. This method of invoking a member function of an object is somewhat like passing a message to object In message passing, each object acts as an independent cell and the only way it can communicate with other objects is by sending a message. For example, a request for an object to perform one of its operations is called a message. The sender will not know about the receiver, and the receiver will