Unit 1
Unit 1
Unit 1
Course Outcome: -
To have a thorough understanding between object-oriented programming and procedural
programming.
Define and explain key Object-Oriented Programming (OOP) concepts, including
encapsulation, inheritance, and polymorphism.
Master the syntax and usage of C++ language features, including classes, objects,
constructors, destructors, and operators.
Develop proficiency in memory management, dynamic memory allocation, and deallocation
using new and delete operators.
Apply principles of abstraction and encapsulation to create well-designed and modular
classes.
Understand the concepts of inheritance and polymorphism and apply them to create
hierarchies of classes.
Implement function overloading, overriding, and virtual functions for achieving polymorphic
behavior.
Use exception handling mechanisms in C++ to write robust and error-tolerant code.
Identify and handle exceptions gracefully within the context of object-oriented programs.
Course Goal
The primary goal of the Object-Oriented Programming through C++ course is to equip students with a
comprehensive understanding of the principles, concepts, and practical implementation of Object-
Oriented Programming (OOP) using the C++ programming language. By the end of the course,
students should be proficient in designing, implementing, and maintaining object-oriented software
solutions, demonstrating a strong foundation in C++ language features and OOP principles.
Syllabus
Structure: 1-20
1.0 Learning Objective
1.1 Evolution of OOP
1.1.1 Advantages
1.1.2 Disadvantages
1.2 Comparison Between Functional Programming and OOP Approach
1.2.1 Key Difference Between Functional Programming v/s OOP
1.2.2 Comparison Table Between Functional Programming v/s OOP
1.3 Characteristics of Object Oriented
1.4 Summary
1.5 Key Words/Abbreviations
1.6 Learning Activity
1.7 Unit End Questions (MCQ and Descriptive)
1.8 References
1.0 Learning Objectives
As many software experts point out, the complexity of software is essential property, not an accidental
one. This inherent complexity is derived from the following four elements:
The complexity of the problem domain
The difficulty of managing the development process
The flexibility possible through software
The problems of characterizing the behavior of discrete system
The OOP (Object Oriented Programming) approach is most commonly used approach now a days.
OOP is being used for designing large and complex applications. Before OOP many programming
approaches existed which had many drawbacks.
These programming approaches have been passing through revolutionary phases just like computer
hardware. Initially for designing small and simple programs, the machine language was used. Next
came the Assembly Language which was used for designing larger programs. Both machine and
Assembly languages are machine dependent. Next came Procedural Programming Approach which
enabled us to write larger and hundred lines of code. Then in 1970, a new programming approach
called Structured Programming Approach was developed for designing medium sized programs. In
1980's the size of programs kept increasing so a new approach known as OOP was invented.
2
Procedural Programming Approach: This approach is top down approach. In this approach, a
program is divided into functions that perform a specific task. Data is global and all the functions can
access the global data. Program flow control is achieved through function calls and goto statements.
This approach avoids repetition of code which is the main drawback of Monolithic Approach. The
basic drawback of Procedural Programming Approach is that data is not secured because data is
global and can be accessed by any function. This approach is mainly used for medium sized
applications. The programming languages: FORTRAN and COBOL follow this approach.
Object Oriented Programming Approach: The OOP approach came into existence to remove the
drawback of conventional approaches. The basic principal of the OOP approach is to combine both
data and functions so that both can operate into a single unit. Such a unit is called an Object. This
approach secures data also. Now a days this approach is used mostly in applications. The
programming languages: C++ and JAVA follow this approach. Using this approach we can write any
lengthy code.
1.1.2 Disadvantages
The runtime cost of dynamic binding mechanism is the major disadvantage of object-oriented
languages. The following were the demerits of adopting object-orientation in software development in
the early days of computing:
Compiler overhead
Runtime overhead
Re-orientation of software developer to object-oriented thinking
Requires the mastery over the following areas:
Software Engineering
Programming methodologies
Benefits only in the long run while managing large software projects, atleast moderately large
ones.
3
1.2 Comparison Between Functional Programming vs OOP
Functional programming provides the advantages like efficiency, lazy evaluation, nested functions,
bug-free code, parallel programming. In simple language, functional programming is to write the
function having statements to execute a particular task for the application. The function can be easily
invoked and reused at any point. It also helps the code to be managed and the same thing or
statements does not need to write again and again.
Functional Programming based on different concepts is 1. High Order Functions (HOF). 2. Pure
functions. 3. Recursion. 4. Strict and Non-strict Evaluation. 5. Type systems. 6. Referential
Transparency. In functional programming, functions are referred as first-class citizens.
Object-oriented programming based on the main features that are: 1. Abstraction: It helps in letting
the useful information or relevant data to a user, which increases the efficiency of the program and
make the things simple. 2. Inheritance. It helps in inheriting the methods, functions, properties, and
fields of a base class in derived class. 3. Polymorphism: It helps in doing one task in many ways with
help of overloading and overriding which is also known as compile time and run time polymorphism
respectively. 4. Encapsulation: It helps in hiding the irrelevant data from a user and prevents the user
from unauthorized access.
Object-oriented programming languages are C++, C#, Java, Python, Ruby, PHP, Perl, Objective-C,
Swift, Dart, Lisp etc. In an object-oriented application, objects can be easily reused in another
application. New objects can be easily created for the same class and code can be easily maintained
and altered. It also has the feature of memory management. It provides a great benefit in designing
large programs, which can be easily divided into smaller parts and helps in distinguishing the
components or phases that need to be executed or planned in a certain way.
4
Below is the list of points describe the Difference between Functional Programming and OOP
Functional programming is used for performing many different operations for which the data
is fixed. Object-oriented programming used for performing few operations which are having
common behavior and different variants.
Functional programming is having a stateless programming model. Object-oriented
programming is having a stateful programming model.
In functional programming, a state does not exist. In object-oriented programming, the state
exists.
In functional programming, a function is the primary manipulation unit. In object-oriented, an
object is the primary manipulation unit.
In functional programming, its functions have no side effects means does not make any
impact on code that is running on multiple processors. In Object-oriented programming, its
methods can have side effects and may put an impact on processors.
In functional programming, the main focus of programming is what are we doing. In object-
oriented programming, the main focus of programming is how are we doing.
Functional programming mainly supports abstraction over data and abstraction over behavior.
Object-oriented programming mainly supports abstraction over data only.
Functional programming provides high performance in processing the large data for the
applications. Object-oriented programming is not good for big data processing.
Functional programming does not support conditional statements. In Object-oriented
programming, conditional statements can be used like if-else statements and switch
statement.
BASIS FOR
Functional Programming OOP
COMPARISON
Object-oriented programming
Functional programming does follow a
Model does follow an imperative
declarative programming model.
programming model.
Object-oriented programming
Parallel programming supported by
Support does not support parallel
Functional Programming.
programming.
5
The basic elements of functional The basic elements of object-
Element programming are Variables and oriented programming are objects
Functions. and methods.
Inheritance
Inheritance describes the parent child relationship between two classes. A class can get some of its
characteristics from a parent class and then add more unique features of its own. For example,
consider a Vehicle parent class and a child class Car. Vehicle class will have properties and
functionalities common for all vehicles. Car will inherit those common properties from the Vehicle
class and then add properties which are specific to a car. Vehicle parent class is known as base class
or superclass. Car is known as derived class, Child class or subclass.
It allows the extension and reuse of existing code without having to rewrite the code from scratch.
Inheritance involves the creation of new class (derived class) from the existing one (base class), thus
enabling the creation of a hierarchy of classes that simulate the class and subclass concept of the real
world.
Polymorphism
In the real world, the meaning of an operation varies with context and the same operation may behave
differently, in different situations.
Polymorphism means ability to take more than one form. An operation may exhibit different
behaviors in different instances. The behavior depends upon the types of data used in the operation.
The following are the different ways of achieving polymorphism in a c++ program:
Function overloading
6
Operator Overloading
Dynamic Binding
Polymorphism permits the programmer to generate high level reusable components that can be
tailored to fit different applications, by changing their low level parts.
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.
Persistence
The phenomenon where the object (data) outlives the program execution time and exists between
executions of a program is known as persistence.
Objects
Objects are the basic run time entities in an object-oriented system. They may represent a person, a
place, a bank account, a table of data or any item that the program has to handle. They may also
represent user-defined data such as vectors, time and lists. Programming problem is analyzed in term
of objects and the nature of communication between them. Program objects should be chosen such
that they match closely with the real-world objects. Objects take up space in the memory and have an
associated address like a record in Pascal, or a structure in c.
When a program is executed, the objects interact by sending messages to one another. E.g example, if
“customer” and “account” are to object in a program, then the customer object may send a message to
the count object requesting for the bank balance. Each object contain data, and code to manipulate
data. Objects can interact without having to know details of each other’s data or code. It is a sufficient
to know the type of message accepted, and the type of response returned by the objects.
OBJECTS: STUDENT
DATA
Name
Date-of-birth
Marks
FUNCTIONS
Total
Average
Display
………
Classes
We just mentioned that objects contain data, and code to manipulate that data. The entire set of data
and code of an object can be made a user-defined data type with the help of class. In fact, objects are
variables of the type class. Once a class has been defined, we can create any number of objects
belonging to that class. Each object is associated with the data of type class with which they are
created. A class is thus a collection of objects similar types. For examples, Mango, Apple and orange
members of class fruit. Classes are user-defined that types and behave like the built-in types of a
programming language. The syntax used to create an object is not different then the syntax used to
create an integer object in C. If fruit has been defines as a class, then the statement
7
Fruit Mango;
1.4 Summary
This unit serves as a foundational exploration into the principles and concepts of Object-Oriented
Programming (OOP). It begins by tracing the Evolution of OOP, highlighting the historical
development and motivation behind the paradigm. The OOP Paradigm is introduced, emphasizing its
significance in modern software development.
The advantages of OOP are thoroughly examined, illustrating how it enhances code organization,
promotes modularity, and facilitates code reuse. A comparative analysis between the functional
programming paradigm and the OOP approach provides insights into the strengths and weaknesses of
each, establishing the context for choosing OOP as a programming paradigm.
8
1.6 Learning Activity
What is software crisis? Justify the need for a new programming paradigm. Explain how object-
oriented paradigm overcomes this software crisis.
1.8 References
Reference:
M. Morris Mano, Computer System Architecture, Pearson Education.
Robert Lafore; Object Oriented Programming in C++ 4th Edition; Techmedia.
Venugopal, Rajkumar; Mastering C++; Tata Mcgrow Hill, 2006.
Kanetkar Y.: LET US C++; BPB; 2009.