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

Related documents


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.

 Overuse of inheritance : Improper use of inheritance can lead to complex class

hierarchies and tightly coupled classes, making code maintenance and modifications

more difficult in the future.

 We cannot apply OOP everywhere as it is not a universal language. It is applied only when it is required. It is not suitable for all types of problems.  Programmers need to have brilliant designing skill and programming skill along with proper planning because using OOP is little bit tricky.

Differences between Procedural and Object-

Oriented Programming

The following table highlights the major differences between Procedural

Programming and Object-Oriented Programming −

Parameter Object Oriented

Programming

Procedural Programming

Definition Object-oriented

Programming is a

programming language that

uses classes and objects to

create models based on the

real-world environment.

In OOPs, it makes it easy to

maintain and modify existing

code as new objects are

created inheriting

characteristics from existing

ones.

Procedural Programming is a

programming language that

follows a step-by-step

approach to break down a

task into a collection of

variables and routines (or

subroutines) through a

sequence of instructions.

Each step is carried out in

order in a systematic manner

so that a computer can

understand what to do.

Approach In OOPs concept of objects

and classes is introduced

and hence the program is

divided into small chunks

called objects which are

instances of classes.

In procedural programming,

the main program is divided

into small parts based on the

functions and is treated as

separate program for

individual smaller program.

Access

modifiers

In OOPs access modifiers

are introduced namely as

Private, Public, and

Protected.

No such modifiers are

introduced in procedural

programming.

Security Due to abstraction in OOPs Procedural programming is

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."

In simple words, we all use the smartphone and very much familiar with its functions such as camera, voice-recorder, call-dialling, etc., but we don't know how these operations are happening in the background. Let's take another example - When we use the TV remote to increase the volume. We don't know how pressing a key increases the volume of the TV. We only know to press the "+" button to increase the volume. In Python, abstraction can be achieved by using abstract classes and interfaces. A class that consists of one or more abstract method is called the abstract class. Abstract methods do not contain their implementation. Abstract class can be inherited by the subclass and abstract method gets its definition in the subclass. Abstraction classes are meant to be the blueprint of the other class. An abstract class can be useful when we are designing large functions. An abstract class is also helpful to provide the standard interface for different implementations of components. Python provides the abc module to use the abstraction in the Python program. Let's see the following syntax.

  1. abstract base class work

  2. from abc import ABC, abstractmethod
  3. class Car(ABC): def mileage(self): pass
  4. class Tesla(Car): def mileage(self): print("The mileage is 30kmph")
  5. class Suzuki(Car): def mileage(self): print("The mileage is 25kmph ")
  6. t= Tesla () t.mileage()
  7. s = Suzuki() s.mileage() Output: The mileage is 30kmph The mileage is 25kmph

Explanation -

In the above code, we have imported the abc module to create the

abstract base class. We created the Car class that inherited the ABC class

and defined an abstract method named mileage(). We have then inherited

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.

String For strings len() returns the number of characters: Example x = "Hello World!" print(len(x)) Tuple For tuples len() returns the number of items in the tuple: Example mytuple = ("apple", "banana", "cherry") print(len(mytuple)) b) Operator polymorphism Example Polymorphism in addition operator We know that the + operator is used extensively in Python programs. But, it does not have a single usage. For integer data types, + operator is used to perform arithmetic addition operation. num1 = 1 num2 = 2 print(num1+num2) the above program outputs 3. Similarly, for string data types, + operator is used to perform concatenation. str1 = "Python" str2 = "Programming" print(str1+" "+str2) the above program outputs The above program outputs Python Programming. Here, we can see that a single operator + has been used to carry out different operations for distinct data types. This is one of the simplest occurrences of polymorphism in Python. c) Polymorphism and Inheritance Like in other programming languages, the child classes in Python also inherit methods and attributes from the parent class. We can redefine certain methods and attributes specifically to fit the child

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

decide what to do with the message. This can make it easier to add new options without modifying existing code. Message passing is a key concept in some models of concurrency and OOP, and it's used in modern computer software in many ways. For example, it can be used to allow objects and systems running on different computers to interact, such as over the internet.