Chapter 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

Name of staff:- Prof. Bhandare P.S.

Chapter 1
Principles of Object Oriented
Programming
-by-
Prof. Bhandare P. S.
SVERI’s COE(Poly), Pandharpur

1
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.

Syllabus:
 1.1 Its need & requirement, Procedure Oriented Programming (POP)
verses Object Oriented Programming (OOP), Basic concepts of Object
Oriented Programming, Object Oriented Languages, Applications of
OOP.

 1.2 Beginning with C++: What is C++? , keywords, variables, constants


basic data types, operators, scope resolution operator, memory
management operators, console input/output, structure of C++ program.

Assignment No 1.
 List two memory management operators available in C++ and state its
use in one line.(2M)

 State any four applications of OOP.(4M)

 Describe syntax of ‘cin’ and ‘cout’ with example. (4M)

 With suitable diagram describe structure of C++ program. (4M)

 List any four features of POP. (2M)

 State any four application of object oriented programming. (4M)

 Differentiate between OOP and POP. (4M)

 Explain : (4M)

 (i) Scope resolution operator

 (ii) Memory management operator

 List any four object oriented languages. (4M)

 Write any four features of object oriented programming. (2M)

2
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.

About title of chapter :

 The title of chapter Principles of Object Oriented Programming gives idea


about the different Object oriented principals.

 In this chapter we will get information about OOP Principals.

 In this chapter students are going to learn new programming approach.

Central Idea of chapter

 Central Idea behind including this chapter in subject is that the student
will understand new programming approach.

 Also Student will learn how programming can be organized in C++


language by studying this chapter.

Importance of chapter :

 Studying this chapter is important is because this chapter introduce new


programming style to student.

 Also this chapter include different OOP concepts.

 Also it will include structure of C++ program.

Objectives of the chapter

 To study OOP concepts.

 To study difference between OOP & POP.

 To study C++ program structure.

 To study Operators in C++.

 To study different applications of OOP.

3
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.

 Introduction
 Object-Oriented Programming (OOP) is an approach to program
organization and development that attempts to eliminate some of the
pitfalls of conventional programming methods by incorporating the best
of structured programming features with several powerful new concepts.

 It is a new way of organizing and developing programs and has nothing


to do with any particular language.

 However, not all languages are suitable to implement the OOP concepts
easily.

Layer of computer s/w


A Look at Procedure-Oriented Programming
Conventional programming, using high level languages such as COBOL, FORTRAN
and C, is commonly known as procedure oriented programming (POP).

In the procedure-oriented approach, the problem is viewed as a sequence of things to


be done such as reading, calculating and printing.

A number of functions are written to accomplish these tasks. The primary focus is on
functions.

A typical program structure for procedural programming is shown in Fig. below.

4
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.

The technique of hierarchical decomposition has been used to specify the tasks to be
completed for solving a problem.

Procedure-oriented programming basically consists of writing a list of instructions for


actions for the computer to follow, and organizing these instructions into groups
known as functions.

We normally use a flowchart to organize these actions and represent the flow of
control from one action to another.

In a multi-function program, many important data items are placed as global so that
they may be accessed by all the functions. Each function may have its own local data.

Figure shows the relationship of data and functions in a procedure-oriented program.

5
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.

 Characteristics of OOP:
 Emphasis is on doing things (algorithms).
 Large programs are divided into smaller programs known as functions.
 Most of the functions share global data.
 Data move openly around the system from function to function.
 Functions transform data from one form to another.
 Employs top-down approach in program design.

 Object-Oriented Programming Paradigm


 OOP treats data as a critical element in the program development and does not
allow it to flow freely around the system.
 It ties data more closely to the functions that operate on it, and protects it from
accidental modification from outside functions.
 OOP allows decomposition of a problem into a number of entities called
objects and then builds data and functions around these objects.
 The organization of data and functions in object-oriented programs is shown in
Fig.

Organization of data & function in OOP


 The data of an object can be accessed only by the functions associated with that
object.
 However, functions of one object can access the functions of other objects.
6
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.

 Some of the striking features of object-oriented


programming are:
 Emphasis is on data rather than procedure.

 Programs are divided into what are known as objects.

 Data structures are designed such that they characterize the objects.

 Functions that operate on the data of an object are tied together in the data
structure.

 Data is hidden and cannot be accessed by external functions.

 Objects may communicate with each other through functions.

 New data and functions can be easily added whenever necessary.

 Follows bottom-up approach in program design.

 Basic Concepts of Object-Oriented Programming


 Objects

 Classes

 Data abstraction and encapsulation

 Inheritance

 Polymorphism

 Dynamic binding

 Message passing

7
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.

 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 terms 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.

 Each object contains data, and code to manipulate the data.

 For example, if "customer’ and 'account" are two objects in a program, then
the customer object may send a message to the account object requesting for
the bank balance.

 Fig. shows two notations that are popularly used in object-oriented analysis and
design.

8
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.

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 a class.

 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 of similar type.

 E.g mango, apple and orange are members of the class fruit.

 Classes are user-defined data types and behave like the built-in types of a
programming language.

 The syntax used to create an object is no different than the syntax used to
create an integer object in C.

 If fruit has been defined as a class, then the statement fruit mango; will create
an object mango belonging to the class fruit.

Data Abstraction and Encapsulation


 The wrapping up of data and functions into a single unit (called class) is known
as encapsulation.

 Data encapsulation is the most striking feature of a class.

 The data is not accessible to the outside world, and only those functions which
are wrapped in the class can access it.

 These functions provide the interface between the object's data and the
program.

 This insulation of the data from direct access by the program is called data
hiding or information hiding.

 Abstraction refers to the act of representing essential features without including


the background details or explanations.

9
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.

 Classes use the concept of abstraction and are defined as a list of abstract
attributes such as size, weight and cost, and functions to operate on these
attributes.

 They encapsulate all the essential properties of the objects that are to be
created.

 The attributes are sometimes called data members because they hold
information.

 The functions that operate on these data are sometimes called methods or
member functions.

 Since the classes use the concept of data abstraction, they are known as
Abstract Data Types (ADT).

Inheritance

 Inheritance is the process by which objects of one class acquire the properties
of objects of another class. It supports the concept of hierarchical classification.

 For example, the bird 'robin' is a part of the class 'flying bird' which is again a
part of the class 'bird'.

 The principle behind this sort of division is that each derived class shares
common characteristics with the class from which it is derived as illustrated in
Fig. below.

10
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.

 In OOP, the concept of inheritance provides the idea of reusability.

 This means that we can add additional features to an existing class without
modifying it .

 This is possible by deriving a new class from the existing one.

 The new class will have the combined features of both the classes.

 The real appeal and power of the inheritance mechanism is that it allows the
programmer to reuse a class

Polymorphism
 Polymorphism is another important OOP concept. Polymorphism, a Greek
term, means the 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.

 For example, consider the operation of addition for two numbers, the operation
will generate a sum.

 If the operands are strings, then the operation would produce a third string by
concatenation.

 The process of making an operator to exhibit different behaviors in different


instances is known as operator overloading.

 Figure illustrates that a single function name can be used to handle different
number and different types of arguments.

 This is something similar to a particular word having several different


meanings depending on the context.

 Using a single function name to perform different types of tasks is known as


function overloading.

11
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.

 Polymorphism plays an important role in allowing objects having different


internal structures to share the same external interface.

 Polymorphism is used in implementing inheritance.

Dynamic Binding
 Binding refers to the linking of a procedure call to the code to be executed in
response to the call.

 Dynamic binding (also known as late binding) means that the code associated
with a given procedure call is not known until the time of the call at run-time.

 It is associated with polymorphism and inheritance.

 A function call associated with a polymorphic reference depends on the


dynamic type of that reference.

 Consider the procedure "draw" in Fig.

 By inheritance, every object will have this procedure.

 Its algorithm is, however, unique to each object and so the draw procedure will
be redefined in each class that defines the object.

 At run-time, the code matching the object under current reference will be
called.

Message Passing
 An object-oriented program consists of a set of objects that communicate with
each other.

 The process of programming in an object-oriented language, therefore,


involves the following basic steps:

◦ 1. Creating classes that define objects and their behavior,

◦ 2. Creating objects from class definitions, and

◦ 3. Establishing communication among objects.

Objects communicate with one another by sending and receiving information much
the same way as people pass messages to one another.

 A message for an object is a request for execution of a procedure, and therefore


will invoke a function (procedure) in the receiving object that generates the
desired result.
12
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.

 Message passing involves specifying the name of the object, the name of the
function (message) and the information to be sent.

 Objects have a life cycle. They can be created and destroyed. Communication
with an object is feasible as long as it is alive.

Benefits of OOP
 The principal advantages are:
 Through inheritance, we can eliminate redundant code and extend the use of
existing classes.
 We can build programs from the standard working modules that communicate
with one another, rather than having to start writing the code from scratch. This
leads to saving of development time and higher productivity.
 The principle of data hiding helps the programmer to build secure programs
that cannot be invaded by code in other parts of the program.
 It is possible to have multiple instances of an object to co-exist without any
interference.
 It is possible to map objects in the problem domain to those in the program.
 It is easy to partition the work in a project based on objects.
 The data-centered design approach enables us to capture more details of a
model in implementable form.
 Object-oriented systems can be easily upgraded from small to large systems.
 Message passing techniques for communication between objects makes the
inter-face descriptions with external systems much simpler.
 Software complexity can be easily managed.

13
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.

Object-Oriented Languages
 The languages should support several of the OOP concepts to claim that they
are object-oriented.
 Depending upon the features they support, they can be classified into the
following two categories:
◦ 1. Object-based programming languages,
◦ 2. Object-oriented programming languages.
 Object-based programming is the style of programming that primarily supports
encapsulation and object identity.
 Major features that are required for object-based programming are:
• Data encapsulation
• Data hiding and access mechanisms
•Automatic initialization and clear-up of objects
• Operator overloading
 Languages that support programming with objects are said to be object-based
programming languages.
 They do not support inheritance and dynamic binding. Ada is a typical object-
based programming language.
 Object-oriented programming incorporates all of object-based programming
features along with two additional features, namely, inheritance and dynamic
binding.
 Object-oriented programming can therefore be characterized by the following
statement:
 Object-based features + inheritance + dynamic binding
 Languages that support these features include C++, Smalltalk, Object Pascal
and Java. There are a large number of object-based and object-oriented
programming languages.

14
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.

Application of OOP:
 Real-time systems
 Simulation and modeling
 Object-oriented databases
 Hypertext, hypermedia and expertext
 AI and expert systems
 Neural networks and parallel programming
 Decision support and office automation systems
 CIM/CAM/CAD systems

What is C++:
 C++ is an object-oriented programming language. It was developed by Bjarne
Stroustrup at AT&T Bell Laboratories in Murray Hill, New Jersey.
 C++ is an object-oriented programming language. It was developed by Bjarne
Stroustrup at AT&T Bell Laboratories in Murray Hill, New Jersey, USA, in the
early 1980's.
 Stroustrup, an admirer of Simula67 and a strong supporter of C, wanted to
combine the best of both the languages and create a more powerful language
that could support object-oriented programming features and still retain the
power and elegance of C.
 The result was C++. Therefore, C++ is an extension of C with a major addition
of the class construct feature of Simula67.
 The idea of C++ comes from the C increment operator ++, thereby suggesting
that C++ is an augmented (incremented) version of C.

Tokens
 As we know, the smallest individual unit in a program are known as tokens.
 C++ has the following tokens:
◦ Keywords
◦ Identifiers
◦ Constanta
◦ Strings
◦ Operators
15
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.

Keywords
 The keywords implement specific C++ language features.

 They are explicitly reserved identifiers and cannot be used as names for the
program variables or other user-defined program elements.

 Table gives the complete set of C++ keywords.

 Many of them are common to both C and C++.

 The ANSI C keywords are shown in boldface.

 Additional keywords Have been added to the ANSI C keywords in order to


enhance its features and make it an object-oriented language.

 ANSI C++ standards committee has added some more keywords to make the
language more versatile.

16
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.

Identifiers and Constants


 Identifiers refer to the names of variables, functions, arrays, classes, etc.
created by the programmer.

 They are the fundamental requirement of any language.

 Each language has its own rules for naming these identifiers.

 The following rules are common to both C and C++:

 Only alphabetic characters, digits and underscores are permitted.

 The name cannot start with a digit.

 Uppercase and lowercase letters are distinct.

 A declared keyword cannot be used as a variable name.

Variable declaration

17
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.

Dynamic Initialization of Variables


 In C, a variable must be initialized using a constant expression, and the C
compiler would fix the initialization code at the time of compilation.

 C++, however, permits initialization of the variables at run time. This is


referred to as dynamic initialization.

 In C++, a variable can be initialized at run time using expressions at the place
of declaration.

 For example, the following are valid initialization statements:

◦ int n = strlen(string);

float area = 3.14159 * rad * rad;

 Thus, both the declaration and the initialization of a variable can be done
simultaneously at the place where the variable is used for the first time.

 The following two statements in the example of the previous section

◦ float average; // declare where it is necessary

◦ average = sum/i;

 Can be combined into a single statement:

◦ float average = sum/1; // initialize dynamically of run time

 Dynamic initialization is extensively used in object-oriented programming We


can create exactly the type of object needed, using information that is known
only at the run time.

Reference Variables
 C++ introduces a new kind of variable known as the reference variable.

 A reference variable provides an alias (alternative name) for a previously


defined variable.

 For example, if we make the variable sum a reference to the variable total, then
sum and total can be used interchangeably to represent that variable.

 A reference variable is created as follows:

◦ data-type & reference-name = variable-name

18
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.

 Example:

float total = 100; float & sum = total ;

Operators in C++

 All C operators are valid C++ also.

 In addition, C++ introduces some new operators.

 We have already seen two such operators, namely, the insertion operator «, and
the extraction operator ».

 :: Scope resolution operator

 ::* Pointer- to-member declaration

 ->* Pointer-to-member operator

 .* Pointer-to-member operator

 delete Memory release operator

 endl Line feed operator

 new Memory allocation operator

 setw Field width operator

Scope Resolution Operator


 Like C, C++ is also a block-structured language.

 We know that the same variable name can be used to have different meanings
in different blocks.

 The scope of the variable extends from the point of its declaration till the end
of the block containing the declaration.

 A variable declared inside a block is said to be local to that block.

◦ int x = 10;

◦ {

◦ ……………..

◦ ……………

19
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.

◦ int x = 1;

◦ }

◦ The two declarations of x refer to two different memory locations


containing different values.

 Statements in the second block cannot refer to the variable x declared in the
first block, and vice versa.

 In C, the global version of a variable cannot be accessed from within the inner
block.

 C++ resolves this problem by introducing a new operator :: called the scope
resolution operator.

 This can be used to uncover a hidden variable. It takes the following form:

 :: variable-name

 This operator allows access to the global version of a variable.

 For example, ::count means the global version of the variable count

Manipulators
 Manipulators are operators that are used to format the data display.

 The most commonly used manipulators are endl and setw.

 The endl manipulator, when used in an output statement, causes a linefeed to


be inserted.

 It has the same effect as using the newline character "\n".

 For example, the statement

◦ cout « "m =« m « endl

◦ « “n=“ « n « endl

◦ « "p =“ « p « endl;

20
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.

Input Operator
 The statement

◦ cin » number1;

 is an input statement and causes the program to wait for the user to type in a
number.

 The number keyed in is placed in the variable number!.

 The identifier cin pronounced 'C in') is a predefined object in C++ that
corresponds to the standard input stream.

 The operator » is known as extraction or get from operator. It extracts for


takes) the value from the keyboard and assigns it to the variable on its right.

 Fig shows operator

 This corresponds to the familiar scanf() operation.

 Like << , the operator » can also be overloaded.

Output Operator
 The output statement.

◦ cout « "C++ is better than C.";

 causes the string in quotation marks to be displayed on the screen.

 This statement introduces two new C++ features, cout and «.

21
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.

 The identifier cout pronounced as ‘C out') is a predefined object that represents


the standard output stream in C++.

 It is also possible to redirect the output to other output devices.

 The operator « is called the insertion or put to operator.

 It inserts (or sends) the contents of the variable on its right to the object on its
left.

Memory Management Operators


 C uses malloc() and calloc() functions to allocate memory dynamically at run
time.

 Similarly, it uses the function free() to free dynamically allocated memory.

 We use dynamic allocation techniques when it is not known in advance how


much of memory space is needed.

 Although C++ supports these functions, it also defines two unary operators
new and delete that perform the task of allocating and freeing the memory in a
better and easier way.

 Since these operators manipulate memory on the free store, they are also
known as free store operators.

22
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.

 An object can be created by using new, and destroyed by using delete, as and
when required.

 A data object created inside a block with new, will remain in existence until it
is explicitly destroyed by using delete.

 The new operator can be used to create objects of any type. It takes the
following general form:

 The new operator offers the following advantages over the function malloc().

 1. It automatically computes the size of the data object. We need not use the
operator sizeof.

 2. It automatically returns the correct pointer type, so that there is no need to


use a type cast. 3.

 It is possible to initialize the object while creating the mercury space.

 4. Like any other operator, new and delete can be overloaded.


23
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.

Basic Data types

Size & range of Data types:

24
Subject: Object oriented programming (17432)
Name of staff:- Prof. Bhandare P.S.

Structure of C++ program


 A typical C++ program would contain four sections as shown in Fig.

 These sections may be placed in separate code files and then compiled
independently or jointly.

 This approach enables the programmer to separate the abstract specification of


the interface (class definition) from the implementation details (member
functions definition).

 Finally, the main program that uses the class is placed in a third file which
"includes" the previous two files as well as any other files required.

 This approach is based on the concept of client-server model as shown in fig.

 The class definition including the member functions constitute the server that
provides services to the main program known as client.

 The client uses the server through the public interface of the class.

25
Subject: Object oriented programming (17432)

You might also like