Unit 1

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

Object Oriented Concept (Through C++)

Course Code: BCA 206 (A)

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

Unit I OOP Paradigm


Characteristics of OOP, Comparison between functional programming and OOP approach,
characteristics of object oriented language - objects, classes, inheritance, reusability, user defined data
types, polymorphism, overloading.
Unit II Introduction to C++
Identifier and keywords, constants, C++ operators, type conversion, Variable declaration, statements,
expressions, input and output, conditional expression loop statements, break control statements,
Classes, member functions, objects, arrays of class objects, pointers and classes, nested classes,
constructors, destructors Inline member functions, static class member, friend functions, and dynamic
memory allocation.
Unit III Polymorphism and Inheritance
Function overloading, operator overloading, polymorphism, early binding, polymorphism with
pointers, virtual functions, late binding, pure virtual functions. Single inheritance, types of
inheritance, types of base classes, types of derivations, multiple inheritances, container classes,
member access control.
Unit IV Exceptions and Templates
Exception Syntax, Multiple Exceptions, Function Templates, Function Templates with multiple
argument templates.
Unit V File Handling in C++
C++ Streams, Console Stream Classes, Formatted And Unformatted Console I/O Operations,
manipulators, File Streams, Classes File Modes, File Pointers and Manipulations File I/O

Text book References: -


 M. Morris Mano, Computer System Architecture, Pearson Education.
 Herbert Schildt; C++ : The Complete Reference 4th Edn; TMH, 2003.
 Robert Lafore; Object Oriented Programming in C++ 4th Edition; Techmedia.
 Balagurusamy ; Object Oriented Programming in C++; 4th Edition TMH,2009.
 Venugopal, Rajkumar; Mastering C++; Tata Mcgrow Hill, 2006.
 Kanetkar Y.: LET US C++; BPB; 2009.
 Deitel and deitel; How to program C++, Addison Wesley, Pearson Education Aisa
 John R. Hubbard, Programming with C++, McGraw Hill Internatinal.
CONTENTS

Unit 1 OOP Paradigm


Unit 2 Introduction to C++
Unit 3 Polymorphism and Inheritance
Unit 4 Exceptions and Templates
Unit 5 File Management
UNIT 1: OBJECT ORIENTED PARADIGM

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

After studying this unit, you will be able to:


 Define and explain key Object-Oriented Programming (OOP) concepts, including
encapsulation, inheritance, and polymorphism.
 Demonstrate the ability to design and implement solutions using an object-oriented
approach.

1.1 Evolution of Programming

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.

1) Monolithic Programming Approach


2) Procedural Programming Approach
3) Structured Programming Approach
4) Object Oriented Programming Approach

Monolithic Programming Approach: In this approach, the program consists of sequence of


statements that modify data. All the statements of the program are Global throughout the whole
program. The program control is achieved through the use of jumps i.e. goto statements. In this
approach, code is duplicated each time because there is no support for the function. Data is not fully
protected as it can be accessed from any portion of the program. So this approach is useful for
designing small and simple programs. The programming languages like ASSEMBLY and BASIC
follow this approach.

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.

Structured Programming Approach: The basic principal of structured programming approach is to


divide a program in functions and modules. The use of modules and functions makes the program
more comprehensible (understandable). It helps to write cleaner code and helps to maintain control
over each function. This approach gives importance to functions rather than data. It focuses on the
development of large software applications. The programming languages: PASCAL and C 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.1 Advantages of Object Oriented Programing


 The programmes written with OOP are really easy to understand.
 Since everything is treated as objects, so we can model a real-world concept using OOP.
 OOP approach offers the reusability of classes. We can reuse the classes that are already
created without writing them again and again.
 Since the parallel development of classes is possible in OOP concept, It results in the quick
development of the complete programmes.
 Programmes written in OOP technique are marginally easier to test, manage as well as
maintain.
 It is a secured development technique since data is hidden and can’t be accessed by external
functions.

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 is the technique of programming that stress on an evaluation of functions


and developing the structure and elements of computer programs. Functional programming does not
work on executing commands or statements rather it evaluates expressions.

Functional Programming provides the great support to structured programming. Functional


programming is a great fit for data science work and R is the popular language among the data
scientists. FP languages can be translated well into an interactive environment, which makes the
understanding of code easier.

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.

OOP is referred to as object-oriented programming. Object-oriented programming is a programming


technique based on the creation of objects. The objects are the instances of classes, which are having
the data regarding fields, functions, and methods. In Object-oriented programming, Objects 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.

1.2.1 Key Difference Between Functional Programming v/s OOP

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.

1.2.2 Comparison Table Between Functional Programming v/s OOP

BASIS FOR
Functional Programming OOP
COMPARISON

Functional programming emphasizes Object-oriented programming


Definition
an evaluation of functions. based on a concept of objects.

Functional programming uses Object-oriented uses the mutable


Data
immutable data. data.

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.

In Functional programming, the


In OOPs, the statements should
Execution statements can be executed in any
be executed in particular order.
order.

In Functional programming, recursion In OOPs, loops are used for


Iteration
is used for iterative data. iterative data.

5
The basic elements of functional The basic elements of object-
Element programming are Variables and oriented programming are objects
Functions. and methods.

The functional programming is used Object-oriented programming is


Use only when there are few things with used when there are many things
more operations. with few operations.

1.3 Characteristics of Object Oriented Programming

Object-Oriented Programming (OOP) uses "objects" to model realworld objects. Object-Oriented


Programming (OOP) consist of some important concepts namely Encapsulation, Polymorphism,
Inheritance and Abstraction. These features are generally referred to as the OOPS concepts. If you are
new to object oriented approach for software development, An object in OOP has some state and
behavior. In Java, the state is the set of values of an object's variables at any particular time and the
behaviour of an object is implemented as methods. Class can be considered as the blueprint or a
template for an object and describes the properties and behavior of that object, but without any actual
existence. An object is a particular instance of a class which has actual existence and there can be
many objects (or instances) for a class. Static variables and methods are not purely object oriented
because they are not specific to instances (objects) but common to all instances.

Encapsulation & Data Abstraction


Encapsulation is the process of wrapping up of data (properties) and behavior (methods) of an object
into a single unit and the unit here is a Class (or interface). Encapsulate in plain English means to
enclose or be enclosed in or as if in a capsule. Encapsulation enables data hiding, hiding irrelevant
information from the users of a class and exposing only the relevant details required by the user. We
can expose our operations hiding the details of what is needed to perform that operation.
Creating new data types using encapsulated-items, that are well suited to an application to be
programmed, is known as data abstraction. The data types created by the data abstraction process are
known as Abstract Data Types (ADTs). Data abstraction is a powerful techniques, and its proper
usage will result in optimal, more readable, and flexible program.

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;

Will create an object mango belonging to the class fruit.

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.

1.5 Key Words / Abbreviations

OOP: Object Oriented Programming.

Polymorphism: Ability to take more than one form.

Encapsulation: Wrapping up of data and behaviour.

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.7 Unit End Questions (Short and Descriptive)

1.7.1 Short / Multiple Choice / Objective Type Questions


1 What is Object Oriented Paradigm
2 Define the following terms:
a) Encapsulation
b) Data abstraction
c) Inheritance
d) Multiple Inheritance
e) Polymorphism
f) Message Passing
g) Classes
h) Objects

1.7.2 Descriptive Type

1. Explain different programming approaches.


2. Compare between Object Oriented Programming and Functional Programming
3. Discuss different characteristics of Object Oriented Programming.

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.

You might also like