0% found this document useful (0 votes)
24 views

Introduction of Oops and C++

This document provides an introduction to object-oriented programming (OOP) and C++. It discusses the need for OOP by explaining the limitations of procedural programming, such as inconsistent global data. OOP treats data as critical and binds it closely to the functions that operate on it. The document then covers key OOP concepts like classes, objects, encapsulation, inheritance, and polymorphism. It explains how OOP models real-world problems better than procedural programming by grouping related data and functions into objects. Advantages of OOP include reusability, extensibility, and ease of maintenance.

Uploaded by

amolia2004
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Introduction of Oops and C++

This document provides an introduction to object-oriented programming (OOP) and C++. It discusses the need for OOP by explaining the limitations of procedural programming, such as inconsistent global data. OOP treats data as critical and binds it closely to the functions that operate on it. The document then covers key OOP concepts like classes, objects, encapsulation, inheritance, and polymorphism. It explains how OOP models real-world problems better than procedural programming by grouping related data and functions into objects. Advantages of OOP include reusability, extensibility, and ease of maintenance.

Uploaded by

amolia2004
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Unit

INTRODUCTION TO OOP AND C++

Lesson Structure

1.0 Objective
1.1 Introduction
1.2 Need of OOP
1.3 Features of OOP
1.4 Object Oriented Design
1.5 Advantages of OOP
1.6 Summary
1.7 Questions
1.8 Suggested Readings

1.0 Objective

After going through this unit you will understand:

 The need of Object Oriented Programming


 OOP concepts like Class, Object, Encapsulation, Inheritance etc.
 The Object Oriented Analysis and Design approach
 Advantages of Object Oriented Programming

1.1 Introduction

The most inspiring aspect in the development of object-oriented approach is to eliminate some of the errors
met in the procedural oriented approach. Object Oriented Programming (OOP) treats data as a critical element
in the program development and does not permit it to freely move around the system. It binds data more
closely to the functions that function on it, and guards it from unintentional alteration from external functions.
OOP allows problem decomposition into several entities known as objects and then creates data and functions
around these objects. The functions associated with the object data can only access them. However, functions
of one object can access the functions of other objects. In program design OOP follows the bottom-up
approach.
The rest of the unit is organized as follows. Section 1.2 explains the need of OOP. Section 1.3 discusses
features of OOP. Section 1.4 describes the Object Oriented Analysis and Design. Section 1.5 discusses the
advantages of OOP. Section 1.6 gives a brief summary on the unit. Section 1.7 contains some questions for
the students to workout. Section 1.8 shows some suggested readings.

1.2 Need of Object Oriented Programming

Immediate question comes into mind that what is need of object oriented programming language, for example,
C++ in spite of existence of one of very popular procedural programming languages that is C. We can
understand need of object oriented programming language with the help of following paragraphs.

You should note that data is the most important in our life. For example, your name, age, marks, father’s
name, mother’s name, bank account, your book chapter etc. These all are data only. That is you are
surrounded with data only. Basic purpose of programming language is to help human being is to give
information after processing the data. And hence we can say that data is most important.
Whereas in the procedure oriented programming (POP) language gives more important to procedure rather
to data. We can say that procedure oriented programming language is a set of procedures. Procedure can be
named as a subroutine or function. We know about functions in C language. C programming language is a
procedure oriented language. In a POP language, primary focus is on functions or subroutines as shown in
Figure 1.1.
Main Program

Function1 Function3 Function2

Function4 Function5 Function6

Figure 1.1: Typical structure of procedure oriented programming

The main problem with POP is the way of handling of data. POP approach gives no importance to data. In
program, which has more than one functions, many important data are placed as global so that they may be
accessed by all functions. Each function may have its own local data. From Figure 1.2, it clear that Global
data can be accessed by more than one functions. In such case, it become very difficult to trace what global
data are being used by which function. These data may be inconsistent during execution. Inconsistency in
data may result in erroneous result. So we need to rewrite functions so that consistency of global data may
be maintained.
Global Data 1 Global Data 2
.

Function1 Function2 Function3


Local Data Local Data Local Data

Figure 1.2: Relationship between functions and data in POP


Another serious drawback with the procedural approach is that it does not model real world problems very
well.
Now, we can list some of the important characteristics of Procedure Oriented Programming are: -
 Uses top down approach to design a program
 Large programs can be divided into smaller programs known as functions.
 Most of the functions share global data.
 Data freely moves from function to function which may result in inconsistency in data
 Functions may change data type of data as many functions may access the same data.
OOP removes some of the problems associated with POP. Object oriented programming gives importance to
data rather function. OOP keeps data and function to operate the data together that means data are not allowed
to move freely around the functions. This property of OOP protects the data from accidental changes. It is
more real than POP. Object oriented programming allows a decomposition of a problem into objects using
class and then builds data and functions around these objects. The data of an object can be accessed only by
the functions associated with that object. OOPS also allows function of one object to access the functions of
other objects in well-mannered so that no accidental changes in data occur. For example, you bank account
should be operated by account holder only. This is possible in OOP. Your bank account is data and credit/debit
is like function. Both will be kept together as shown in Figure 1.3.
Object A Object B

Data Data

Function Function
Object C

Data

Function

Figure 1.3: Data and function in OOP

Based on above, characteristics of OOP are listed here:


 OOP follows bottom-up approach
 OOP emphasizes on data rather than procedures or algorithms.
 Programs are divided into objects.
 Data is hidden and cannot be accessed by external functions without permission.
 Objects may communicate with each other through functions.
 New data and functions can be easily added whenever necessary.
1.3 Features of Object Oriented Programming

It is necessary to understand some of the concepts used in object – oriented programming that means there
are some important features of programming language in order to categorize as an object oriented
programming:
Classes:
A class in C++ can be defined as a user defined data type. So, typical definition of Data type is applicable
in case of class also. Data type not only define kind of data but also what kind of operation can be performed
over data. Similarly, Class contains data and functions (also called methods) as its members. There are
three types of access specifiers namely private, protected and public which controls the access of data by
function either from inside the class or outside of class. Private is by default access specifier. Class is a
static entity.
So, we can think class as a data type and object as a variable. Class represents a blueprint of similar type of
object. For example, mango, grape, guava are objects of the class fruit. Student can be a class whereas Ram
will be object of this class. For instance, the following statement,
Student Ram;
will create an object Ram belonging to the class Student. This is similar to the statement used in C/C++:
int a;
In nut shell, object oriented programming allows us to create new data types through class.
Objects:
Instance of the class is known as Object. In terms of variable declaration, object is a variable. Object is
being created during run time only that means object is a run time entity. All the living and non-living
things of this universe can be represented as objects. For example: Delhi, Patna, the bank account number,
mango, milk, pen, Pencil etc. After execution, objects consume memory space similar to variable. Total
memory space consumed by any object will be equal to sum of memory space consumed by individual data
members.
Methods:
Objects once created, then it must be capable doing some actions. For this methods are used. In object-
oriented programming, a method is a procedure connected with a class. A method outlines the object
behavior that are generated from the class. Alternative way to say define a method is that it is object
performs action that are defined by methods. The association between class and method is named as
binding. Consider the example of an object, where a ‘person class’ is used to create an object type ‘person’.
Methods in this class may possibly contain things like walking, running and eating. Methods are at times
tangled with functions, but they are different.
Functions:
A function is an amalgamation of commands that are combined to attain some outcome. A function usually
needs some input (called arguments) and returns some outcomes. For example, consider the example of Car
driving. For mileage calculation, one requires to execute a computation by means of the distance driven
and the quantity of petrol used. A function can be written to do this computation. The inputs are going to
the function would be petrol consumption and distance travelled, and the outcome would be mileage.
Anytime to determine the mileage, one can just make a function call to make the calculation.

Encapsulation:
Encapsulation is one of the most important feature of OOP. This allow us to wrap up of data and functions
into a single unit in the form of class. This keeps the data safe form accidental access or modification of
data from outside the class. That is data is only accessible from the member functions which are wrapped
with data.
Abstraction:
Abstraction represents essential features without exposing the background details or explanations to outside
world. Let's take a real world scenario of a TV. We can switch on and off, change the channel, fine-tune the
volume, and add extra device such as USB pen drive, speakers, etc without knowing the internal
specifications. User must be unaware about receiving of signal through cable or air and lastly shows them on
the monitor. But the channel tuning is done through buttons. This button behaves like external interface for
the user.
Likewise, classes use the impression of abstraction of data known as ADT (Abstract Data Types).
Data Hiding:
If a program cannot access the data directly, then it means it is insulated from it and known as information
hiding or data hiding. Data Hiding is the mechanism through which the information of the class are
concealed from the user. The user can execute only a limited set of procedures on the hidden member of
the class. Classes are the user-defined types through which C++ supports the properties of encapsulation
and data hiding.
Inheritance:
Inheritance allows object of one class to inheriting the properties from object of another class. In other
words, we may have mutual characteristics or features that may be required by various classes. So those
characteristics can be put together in a class named as base class and the supplementary classes that have
these features can take the base class and outline only those different features that they have on extra in as
their individual property in their classes. These classes are named as derived class. By this means, it cuts
the size of code as the mutual features are placed disjointedly in base class and it is just discussed in the
derived class. This offer the users the significant practice of the terminology known as reusability.
Let us understand through example.
1. Mammals, amphibians, insects, birds and so on are the classifications of class animal
2. Cars, buses, trucks and motor cycles are the classification of vehicle class. Each sub-class shares
common features with the class from its base class. Cars, buses, trucks and motorcycles all have a
motor and wheels; these are the essential features of vehicles. In accumulation to these common
features, each sub-class also has its own features: buses have spaces for many people while trucks
have space for heavyweight loads.
3. Consider a Player class. A player has certain behavior and properties. Now precisely define various
types of players. So, for different game, different player classes can be created: Cricketer,
Footballer, Rugby Player etc. Here, base class is the Player and derived classes from Player class
are Cricketer, Footballer, Rugby Player etc. The behavior and properties of the Player class are
inherited by these classes and then they enhance their properties and behavior of their own by
adding some other specific behavior and properties. The Cricketer class can be inherited furtherly
by new classes like: Batsman, Bowler, Wicketkeeper etc. Base class and derived class are also
acknowledged as superclass and subclass.

Figure 1.4: Concept of Inheritance

The idea of reusability is provided by the concept of inheritance, meaning extra characteristics can be added
to a prevailing class without changing it. In above example, the class ‘Cricketer’ can be inherited from base
class ‘Player’. Then, all features of Player class are also of class ‘Cricketer’. Thus, we do not need to
indicate common features to class ‘Cricketer’. The only extra characteristics of Cricketer are involved in
class ‘Cricketer’. The mutual characteristics are shared from class ‘Player’. Thus, features of class
‘Cricketer’= extra characteristics of class ‘Cricketer’+ common characteristics of class ‘Player’.
Reusability:
Reusability is defined as reusing the existing structure without modifying it but by incorporating additional
characteristics or features to it. This can be attained by means of OOPs property of inheritance. It benefits
by decreasing the size of code. Since classes can be just derivative of existing classes. Hence user requires
only to add the new features to derived class and it helps users to save their time.
Polymorphism and Overloading:
Polymorphism is another essential concept of OOPs. In OOPs, polymorphism (from the Greek sense
means "having multiple forms") is the property of being capable to allocate a different meaning or use to
something in different perspectives. Precisely it allows an entity such as a function, a variable, or
an object to have more than one outline. There are many different types of polymorphism that makes an
operator or function to behave in diverse forms dependent on the situation where they are existing is known
as polymorphism. Overloading is a type of polymorphism. In other words, consider the operators *, / that
function on numeral data type and is used to accomplish arithmetic multiplication and division. But in
operator overloading the operators are given new definition and new operations to operate on different data
types. In different words overloading is new functionality is assigned to existing one. This is a very
significant characteristic of OOP methodology that extends the management of data-type and operations.
A procedure may show different actions in different occasions. The behavior dependency is on types of
data used in the operation. For instance, consider the addition operation. Sum is the outcome of the operation
when it is executed on two numbers. If strings are the input to the operator, then the operation yields a third
string by concatenating the input strings. The method of creating an operator to have different behaviors in
different occurrences is known as operator overloading.
Figure 1.5 demonstrates that a name of a function can be used to manage different number and different
kinds of parameters. This is somewhat analogous to a certain word exhibiting various meanings subject on
the dependency on context. By means of one name of a function it performs different types of activities and
is named as function overloading.
Polymorphism shows a vital part in permitting objects having different structures internal to share the
similar interface externally. This aids that operations of a general class may be executed in the same manner
however specific actions related with each operation may vary. Polymorphism is widely used in employing
inheritance.

Figure 1.5: Polymorphism

Interfaces:
An interface is a portrayal of the activities that an object can perform. For illustration, when we flip a switch
of bulb, the bulb switches on, how it works is not important, just that it does. In OOPs, an Interface is an
explanation of all functions that an object must have in order to be a "P". For instance, a bulb should have
methods as turniton() and turnitoff(). The interfaces purpose is to permit the system to impose these
properties and to distinguish that a TYPE P (whatever the interface is) object must have functions called Q,
R, S, etc.

1.4 Object Oriented Design

Object-oriented development has a part called object-oriented design where an object-oriented strategy is
implemented all over the development process.

 Object-oriented analysis. Object-oriented model of application domain are developed with this.
Entities in the model represents the object and operations related with the problem to be resolved.
 Object-oriented design. The identified requirements of a software system are implemented by
developing an object-oriented model with help of this.
 Object-oriented programming. It is concerned with executing a software design using an object-
oriented programming language, such as C++.

The transition amid these phases of development should, preferably, be smooth, with compatible
representations used at each phase. Transition to to the next phase includes refining the preceding phase by
adding supplement information to existing object classes and developing new classes to offer additional
functionality.
Some of the Object Oriented Design methodologies have been discussed below:
1) The Booch Method
In software engineering the Booch method, which is published in 1991 by Grady Booch, is a widely used
method in object-oriented analysis and design. UML supersedes the Booch method, which shows elements
from the Booch method with OMT and OOSE.
The Booch method assists in designing of the systems using object paradigm. It includes the object-oriented
analysis and object-oriented design phases of an object-oriented system. The method describes different
models to define a system and it supports the incremental and iterative progress of systems.
The Booch method contains six types of diagrams such as object diagrams, class diagrams, module
diagrams, state transition diagrams, interaction diagrams and process diagrams.

Figure 1.6: A class diagram notation

Figure 1.7: A object diagram notation


Figure 1.8: An interaction diagram

There Booch diagrams: state transition and interaction diagrams are very much analogous to UML
diagrams. The State transition diagram resembles to state chart diagram of UML’s and the interaction
diagram matches to sequence diagram of UML's.

2) The Shlaer-Mellor Method

The Shlaer-Mellor Method offers complete coverage for the analysis, design, and implementation phases
of the software development lifecycle.

In order to define the Shlaer-Mellor process in detail, the procedure is fragmented down into the phases
enumerated below. These stages discriminate between the works according to different types of domains,
namely, the application domain, the service domains, and the translation domain.
1. Divide the system into domains.
2. Study the application domain.
3. Approve the analysis via static verification and simulation (dynamic verification).
4. Mine the necessities for the service domains.
5. Examine the service domains.
6. Postulate the modules of the architectural domain.
7. Shape the architectural modules.
8. Decode the prototypes of each domain using the architectural modules.

Benefits

 Verification: The capability to simulate the implementation of the Shlaer-Mellor OOA models
noticeably creates the completion norms for analysis and lets the functional behavior of the system
to be verified at primary level i.e. at analysis time.
 Integrated Approach: The Shlaer-Mellor Method offers widespread lifecycle coverage with
smooth switches among the analysis, design, and implementation phases by means of Recursive
Design.
 Iteration: This approach decreases and controls iteration in analysis by restraining it to a single
domain at a time. Iteration in design is similarly controlled. Changes to the design are made
completely in the architectural domain and spread to the entire system through the prototypes.
 Reuse: The method organizes and supports use again of complete domains. Because domains are
set aside entirely isolated from one another till the final construction steps, they can be conveyed
together to other systems. This employs over mainly to the architectural domain: This domain, as
well as the mechanisms and prototypes, is commonly used again for other systems that have mostly
the same loading and performance features. Mappings combines the domains into a system at the
end of the development lifecycle.
 Automation: Since the process is created on basis of transformation of analysis models, it is very
much vulnerable to automation. The most current generation of CASE tools have generated the
competence for 100% code generation.
3) The Rumbaugh Method
 Jim Rambaugh and his co-workers presented OMT that describes a method for the analysis, design
and implementation of a system by means of an object-oriented technique.
 OMT is a fast, spontaneous tactic for classifying and modeling all the objects making up a system.
Particulars such as class, method, attributes, inheritance and association too can be expressed easily.
 OMT consists of four stages, which can be iteratively performed.
o Analysis: The results are objects and dynamic and functional models.
o System Design: The outcomes are an arrangement of simple design of system along with
strategy decisions at high– level.
o Object Design: This stage yields a document of design, comprising of in depth objects
static, dynamic and functional models.
o Implementation: This stage yields extensible, reusable and robust program.
 OMT keeps modeling into three different parts.
o The object model and the data dictionary represents an object model.
o The state diagrams and even flow diagrams represents a dynamic model.
o Data flow and constraints represents a functional model.
 Object Model: It defines the outline of objects in a system: Their identity, associations with other
objects, attributes and operations. Object diagram is used to denote graphically the object model
that contains classes connected by association lines. Each class comprises a set of individual objects
and association lines establish relationships between the classes.
 OMT Dynamic Model: It offers in depth and comprehensive dynamic model, in addition to letting
us illustrate states, transitions, events and actions. A diagram that consists of network of states and
events is the OMT state transition diagram. Each state accepts one or more events, at which instance
it makes the switch to the subsequent state whereas the subsequent state depends on present state
as well as events.
 OMT Functional Model: OMT DFD (data flow diagram) displays the movement of data between
different processes in a business. It also offers a simple and spontaneous method for characterizing
business process without aiming on the particulars of the computer system. DFD has 4 principal
symbols:
o Any function being performed is symbolizes by the process.
o The direction of data element movement is showed by the data flow.
o A location where data are stored is represented by the data store.
o A source or destination of a data element is shown by an external entity
4) The Jacobson Method
 It comprises of Object-Oriented Business Engineering (OOBE), Object-Oriented Software
Engineering (OOSE) that cover the complete lifecycle and stress traceability among different
stages, both forward and backward. This traceability allows analysis & design work reuse, probably
much bigger aspects in the reduction of development time than code reuse.
 Use Cases: At the core of these methodologies is the use–case model, which developed with Object
Factory for S/w Development (objectory). A use case is communication amongst users and a
system. It accounts user goal & duty of system towards its user.
 The use cases are styled as one of the subsequent form:
o Non formal manuscript with no clear flow of events
o Manuscript, easy to read but with a clear flow of events to flow (suggested form)
o Formal style using pseudo code.
 The use case explanation must enclose:
o When and how the use case starts and finishes
o Communication between use case & its actors containing when communication happens?
What is communicated?
o When and how use case will require data stored in system or will store data in system.
o Exceptions to the flow of events.
o When and how concepts of the problem domain are tackled.
 An abstract use case is incomplete & has no actors that start it but is used by other use case. This
inheritance could be used in numerous levels. It has ones that have uses or extends relationships.
 OOSE: It is a method of object, also known as objectory– oriented development with the particular
goal suitable for the development of large and real time systems.
 The process of development is named as use–case driven development. It points out that use cases
are involved in several stages of the development comprising of analysis, design, validation and
testing. The use case situation commences with a user of the system beginning a sequence of
interconnected events.
 Objectory has been developed and applied to numerous application areas and embodied in the
CASE tool systems. Objectory is built around several different models:
o Use–case model: It defines outside (actors) and inside (use case) of the system’s behavior
o Domain object model: The objects of the real world are mapped into the domain object
model
o Analysis object model: It presents how the source code (implementation) should be carried
out
o Implementation model: It represents the implementation of the system
o Test model: It constitutes the test plans, specifications and reports
 OOBE: OOBE means modeling of object at the enterprise level. The following Phases are involved
in the development course.
o Analysis phase: It defines the system that has to be built based upon the requirements
model, problem-domain object model, and analysis model. It decreases the complexity &
supports maintainability over life of the system.
o Design & implementation phases: Implementation environment must be recognized for
design model. It comprises constraints, DBMS, distribution process, due to the
programming language, available component libraries and incorporation of graphical user
interface tools.
o Testing phase: Lastly, Jacobson defines numerous testing levels and methods. The levels
include integration testing, unit testing, and system testing.

1.5 Advantages of Object Oriented Programming

Following are the advantages of Object Oriented Programming:


 OOP implements real life scenario.
 implementation details are hidden from other modules
 inheritance, abstraction, polymorphism, and encapsulation, are some of the features provided by
OOP.
 good for defining abstract data types.
 OOP provides a clear modular structure for programs.
 easy to maintain and transform existing code because new objects can be created with small
differences to current ones.
 objects, instance, methods, message passing, are the chief properties provided by OOP languages
 Programmer does not only define data types but he also deals with operations applied for data
structures.

1.6 Summary

In OOP, data is treated as a critical element in the program development. The data is not allowed to flow
freely around the system. OOP binds data to the functions that operate on it more closely. Moreover, data
is protected from accidental modification from outside functions. Encapsulation is one of the most
important feature of OOP. This allow us to wrap up of data and functions into a single unit in the form of
class. Abstraction represents essential features without exposing the background details or explanations to
outside world. Data hiding or information hiding is insulating data from direct access by the program.
Inheritance allows object of one class to inheriting the properties from object of another class.
Polymorphism means making an operator or a function act in different forms depending on the place they
are present. Reusability means re-usage of structure without altering the current one but adding new
characteristics or features to it. An interface is a description of the actions that an object can do.

1.7 Questions

1. How is OOP different from POP?


2. Write down the characteristics of OOP.
3. Explain the concept of Classes and Objects in OOP.
4. What is Inheritance? Give an example to explain this feature of OOP.
5. Explain about Encapsulation and Abstraction with example.
6. What are the steps involved in Object Oriented Design?
7. Write any two advantages of OOP.
8. What do you mean by Polymorphism?
9. What is the difference between Methods and Functions?
10. Define Interfaces. Give a suitable example to support your statement.

1.8 Suggested Readings

1. Lafore, Robert. Object-oriented programming in C++. Pearson Education, 1997.


2. https://www.geeksforgeeks.org/basic-concepts-of-object-oriented-programming-using-c/
3. https://www.tutorialspoint.com/object_oriented_analysis_design/ooad_object_oriented_principles.htm

You might also like