0% found this document useful (0 votes)
19 views137 pages

Oops 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views137 pages

Oops 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 137

INTRODUCTION TO OBJECT ORIENTED

PROGRAMMING

Dr. Shatrughan Modi


School of Computing
IIIT Una
Software
Evolution:
• Since the invention of the computer, many programming approaches have been used.

• The Primary motivation in all programming styles is the concern to handle the increasing
complexity of programs that are reliable and maintainable.

• These Programming techniques include

• 1. Machine Level Programming

• 2. Assembly Language Programming

• 3. High Level Programming


Machine level Language :

• Machine code or machine language is a set of instructions


executed directly by a computer's central processing unit (CPU).

• Each instruction performs a very specific task, such as a load, a


jump, or an ALU operation on a unit of data in a CPU register or
memory.

• Every program directly executed by a CPU is made up of a series


of such instructions.
Assembly level Language :
• An assembly language (or assembler language) is a low-level
programming language for a computer, or other programmable device,
in which there is a very strong (generally one-to-one) correspondence
between the language and the architecture's machine code instructions.

• Assembly language is converted into executable machine code by a


utility program referred to as an assembler; the conversion process is
referred to as assembly, or assembling the code.
High level Language :
• High-level language is any programming language that enables
development of a program in much simpler programming context and
is generally independent of the computer's hardware architecture.

• High-level language has a higher level of abstraction from the


computer, and focuses more on the programming logic rather than
the underlying hardware components such as memory addressing
and register utilization.
• The first high-level programming languages were designed in the 1950s.
Now there are dozens of different languages, including Ada , Algol,
BASIC, COBOL, C, C++, JAVA, FORTRAN, LISP, Pascal, and Prolog.

• Such languages are considered high-level because they are closer to


human languages and farther from machine languages.

• In contrast, assembly languages are considered low- level because they


are very close to machine languages.
High-Level programming approaches are broadly classified as:

1. Procedure-Oriented Programming (POP) &


2. Object-Oriented Programming (OOP).
Procedure-Oriented Programming (POP)
• Conventional programming, using high level languages such as
COBOL, FORTRAN and C, are 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.
Analysis of Procedure-oriented-Programming

• Concentration is on the development of functions, very little attention is given


to the data that are being used by various functions.

• Data move freely between functions where there is chance for the intruders to
hack the data.

• Critical applications should be designed in such a way that the data should be
protected when they are transferred between functions
Object-Oriented Programming
Paradigm

• The major motivating factor in the invention of object-oriented approach is


to remove some of the flaws encountered in the procedural approach
mainly providing data hiding feature.

• 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.
Some characteristics exhibited by procedure-oriented programming are:

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


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

1. Objects

2. Classes

3. Data abstraction and encapsulation

4. Inheritance

5. Polymorphism

6. Dynamic binding

7. Message passing
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 must handle.

• The fundamental idea behind object oriented approach is to combine


both data and function into a single unit and these units are called
objects.

• The term objects means a combination of data and program that represent
some real word entity.
• CLASS:
• A group of objects that share common properties for data
part and some program part are collectively called as class.

• In C ++ a class is a new data type that contains member


variables and member functions that operate on the
variables.
DATA ABSTRACTION :

• Abstraction refers to theact of representing essential features without


including the back ground details or explanations.

• Classes use the concept of abstraction and are defined as size, width and
cost and functions to operate on the attributes.
DATA ENCAPSALATION :
• The wrapping up of data and function into a single unit (called class)
is known as encapsulation.

• The data is notaccessible to theoutside world and only


those functions which are wrapped in the class can access it.

• These functions provide the interface between the objects data and the
program.
Encapsulation
“Mechanism that associates the code and the data it manipulates into a
single unit and keeps them safe from external interference and misuse.”
INHERITENCE :
• Inheritance is the process by which objects of one class
acquire the properties of another class.

• The concept of inheritance provides the idea of reusablity.


• This mean that we can add additional features to an existing class
with out modifying it.

• This is possible by desining a new class that will have the


combined features of both the classes.
INHERITENCE
INHERITENCE
INHERITENCE
POLYMORPHISIM:
•Polymorphism means the ability to take more than one form.

Meaning of Polymorphism

Poly Multiple

Morphing Actions

• A language feature that allows a function or operator to be given more


than one definition. The types of the arguments with which the function
or operator is called determines which definition will be used.
POLYMORPHISIM Cont.
• Overloading may be operator overloading or function
overloading.

• It is able to express the operation of addition by a single operater


say ‘+’. When this is possible you use the expression x + y to denote
the sum of x and y, for many different types of x and y; integers ,
float and complex no. You can even define the + operation for two
strings to mean the concatenation of the strings.
POLYMORPHISI
M
BINDING A FUNCTION
CALL:
• Binding refers to the linking of a procedure call to the code to be
executed in response to the call.
DYNAMIC BINDING :
• Dynamic binding means the code associated with a given procedure call
is not known until the time of the call at run-time.

• It is associated with a polymorphic reference depends upon the dynamic


type of that reference.
MESSAGE PASSING :
• An object oriented program consists of a set of objects that
communicate with each other.

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

• Message passing involves specifying the name of the object, the name
of the function (message) and information to be sent.
BENEFITS OF OOP:

1. Through inheritance redundant code can be eliminated and extend the


use of existing classes.

2. Code reusability leads to saving of development time and


higher productivity.

3. The principle of data hiding helps the programmer to build


secure programs that can’t be invaded by code in other parts of
the program.
BENEFITS OF OOP
Cont.
4. It is possible to have multiple instances of a class to co-exist with out
any interference.

5. It is easy to partition the work in a project based on objects.

6. Object-oriented systems can be easily upgraded from small to


large systems.
BENEFITS OF OOP
Cont.
7. Message passing techniques for communication between
objects makes the interface description with external systems
much simpler.

8. Software complexity can be easily managed.


APPLICATION OF
OOP:

The most popular application of oops up to now, has been in the area of
user interface design such as windows. There are hundreds of windowing
systems developed using oop techniques.

The promising areas for application of oop includes.


1. Real – Time systems.
2. Simulation and modeling
3. Object oriented databases.
APPLICATION OF OOP Contd.
4. Hypertext, Hypermedia and Expertext.

5. Artificial intelligence and expert systems.

6. Neural networks and parallel programming.

7. Decision support and office automation systems.

8. CIM / CAM / CAD system.


cout

< insertion or put to


<
ci
n

>> extraction or get from


C++ Class
Implementation
Example:
class student
{ private:
char st_name[30];
char st_id[10];
char branch[10];
char
semester[10];
public:
void Enroll( );
void Displayinfo(
MANIPULATERS: - endl &
setw
Manipulators are operator 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 line feed to
be insert.(just like \n). If m=2597, n=14 & p=175

Example:
cout<<”m=”<<m<<endl
;
cout<<”n=”<<n<<endl;
cout<<”p=”<<p<<endl;
Example:
cout<<setw(5)<<”m=”<<m<<endl
;
cout<<setw(5)<<”n=”<<n<<endl;
cout<<setw(5)<<”p=”<<p<<endl;
Size & range of C++ basic data types:
Built-in Data types in C++ using sign & size qualifiers.

TYPE BYTES RANGE


char 1 -128 to127
unsigned char 1 0 to 255
signed char 1 -128 to 127
int 2 -32768 to 32767
unsigned int 2 0 to 65535
signed int 2 -32768 to 32767
short int 2 -32768 to 32767
long int 4 -2147483648 to 2147483647
signed long int 4 -2147483648 to 2147483647
unsigned long int 4 0 to 4294967295
float 4 3.4E-38 to 3.4E+38
double 8 1.7E -308 to 1.7E +308
long double 10 3.4E-4932 to 1.1E+ 4932
A Structure is a user defined data type for handling a group of logically related
heterogeneous data items.

i.e. It can be used to represent a set of attributes, such as : student_name , roll_no

A structure definition creates a format that may be used to declare structure


variables.

Structure Declaration Syntax:

struct tag_name
{
data_type member1;
data_type member2;
……
……… …..
1. The template terminated with a semicolon

2. While the entire declaration considered as a statement, each member is declared


independently for its name and type in a separate statement inside the template.

3. The tag name can be used to declare structure variables

Example:
struct book
{
char title[20];
char author[15];
int pages;
float price;
};
❖ The keyword struct declares a structure to hold the details of four fields,
namely title, author, pages and price.

❖ These fields are called structure elements or members and each


member may belong to different type of data.

❖ book is the name of the structure and also called as ‘STRUCTURE TAG’.

Example: void main( )


struct book {
{ struct book b1;
char title[20]; strcpy(b1.title,“Programming in c++”);
char author[15]; strcpy(b1.author,“Dr. E.
int pages; Balagurusamy”); b1.pages=680;
float price; b1.price=350.0;
}; cout<<b1.title<<b1.author<<b1.pages<<b1.price
;
}
Union is a user defined data type similar to structures and therefore follow the same
syntax as structures.

Major difference is in terms of Storage:

In structures each member has its own storage location.

In unions all members use the same location – common memory locations

Union may contain many members of different type but can handle only one member at
a time.
union union_name
{
data_type
member1;
Example
data_type mrmber2;
union
… … .. …..
book
} var1, var2, .. ;
{
char
title[15];
char *author;
int pages;
DECLARATION OF VARIABLES:
In ANSI C all the variable which are to be used in programs must be declared at the beginning of the
program.

But in C++ we can declare the variables any where in the program where it requires.

It makes the program easier to understand because the variables are declared in the context of their use.

Example:
main( )
{
float x;
float sum=0.0;
for(int i=1;i<5;i++)
{
cin>>x;
sum=sum+x
}
float average;
average=sum/4;
cout<<average;
return 0;
REFERENCE VARIABLES:

A references variable provides an alias name(alternative name) for a previously defined


variable.

Syntax for declaring reference variable :

Datatype & reference_name=variable name;

Example:
float total=1500;
float
&sum=total;

*Here sum is the alternative name for variables total


*Both the variables will refer to the same data object in the memory
*sum and total can be used interchangeably to represent the variable.
*A reference variable must be initialized at the time of declaration .
When the compiler encounters a mixed expression, it will convert one of them
to match with the other, using the rule that the “smal er” type is converted
to the “wider” type.
For eg.

* char to int

* int to float

* float to double
Selection
The if statement: Statements
The if statement is implemented in three forms:
1. simple if statement
2. if..else statement
3. if..else if ladder
1. Simple if statement Syntax:
if (condition)
{
Statement block;
}

Eg.if(a>b) { cout<<a<< “ is greater than “<<b;} #include<iostream.h>


if(b>a) { cout<<b<<“ is greater than “<<a; } void main()
}
{ Outout:
int a,b; 10 34
cin>>a>>b 34 is greater than 10
;
2. if.. else
if
statement
{Statement
(condition) block1 …….;}
else
{Statement block2 …….;}

Eg.
#include<iostream.h
> void main()
{
int a,b;
cin>>a>>b;
if(a>b) { cout<<a<< “ is greater than
“<<b;}
else { cout<<b<<“ is greater than “<<a; }
}
Outout:
3. if..else if ladder
if (condition1)
{Statement
block1…….;} else
if(condition2)
{Statement block2.......;}
else if(condition3)
…………
else
{Statement blockN……;}

Eg. #include<iostream.h>
void main()
{
int a,b,c;
cin>>a>>b>>c;
if((a>b) && (a>c)) { cout<<a<< “ is the largest number”;}
This is a multiple-branching statement where, based on a condition, the
control
is transferred to one of the many possible points;
switch(expr)
{
case 1:
action1;
break
; case
2:
action2
;
break;
..
..
default:
1. The while statement:
Syntax:
while(condition)
{
Statements;
}

2. The do-while statement:


Syntax:
do
{
Statements;
}while(condition);

3. The for loop:


Syntax:
for(initialization expression1;termination expression2;step expression3)
{
while
do..while
for
break;
continue
; return
goto
goto
REFERENCES:

1. E. Balagurusamy, “Object Oriented Programming with C++”, Fourth edition, TMH,


2008.

2.LECTURE NOTES ON Object Oriented Programming Using C++ by Dr. Subasish Mohapatra,
Department of Computer Science and Application College of Engineering and Technology,
Bhubaneswar Biju Patnaik University of Technology, Odisha

3. K.R. Venugopal, Rajkumar, T. Ravishankar, “Mastering C++”, Tata McGraw-Hill


Publishing Company Limited

4. Object Oriented Programming With C++ - PowerPoint Presentation by Alok Kumar

5. OOPs Programming Paradigm – PowerPoint Presentation by an Anonymous Author

You might also like