Sie sind auf Seite 1von 30

Introduction to Object Oriented

Programming
UNIT-1
Principles of Object Oriented
Programming
Software Crisis:-
Reasons -
• Dynamic nature of S/W tech.
• Look for new approaches to S/W design and
development.
• Increasing complexity; highly competitive
nature.
Issues to be addresses:-
• How to represent real-life entities of problems in
system design?
• How to design systems with open interfaces?
(heterogeneous entities)
• How to ensure reusability and extensibility of
modules?
• How to develop modules that are tolerant to any
changes in future?
• How to improve software productivity and decrease
cost?
• How to improve the quality of software?
• How to manage time schedules?
• How to industrialize the software development
process?
Maintenance costs:-
• Changes in data formats.
• Changes in user requirements
• Hardware changes
• Efficiency improvement
• Documentation
• Routine debugging
• Emergency fixes
Quality issues for critical evaluation:-
• Correctness
• Maintainability
• Reusability
• Openness and interoperability
• Portability
• Security
• Integrity / connectedness
• User friendliness
Software Evolution
-> Binary
-> Machine language
->Assembly language
-> Procedure – Oriented
-> Object - Oriented Programming
Procedure-Oriented Programming
• Problem is viewed as sequence of things.
• Primary focus is on functions. Fig. 1.4
• Limitations:-
– Very little attention is given to data; important
items are placed as Global.
– Does not model the real-world problems well.
• Top-Down approach in program design.
Object-Oriented Programming
Paradigm
• Treats data as Critical element.
• Does not allow it to flow freely. Data is hidden.
• Protects from accidental modifications.
• Allows decomposition of a problem into entities
called objects and build the functions for them.
• Various objects communicates through functions.
• Bottom-up approach.
• Fig. 1.6
Difference between OOPS and POP
Basis For comparison POP OOP

Basic Procedure/Structure oriented . Object oriented.

Approach Top-down. Bottom-up.


Main focus is on "how to get the task done" Main focus is on 'data security'. Hence, only
Basis i.e. on the procedure or structure of a objects are permitted to access the entities
program . of a class.
Large program is divided into units called
Division Entire program is divided into objects.
functions.
Access specifier are "public", "private",
Entity accessing mode No access specifier observed.
"protected".
It overloads functions, constructors, and
Overloading/Polymorphism Neither it overload functions nor operators.
operators.
Inheritance achieved in three modes public
Inheritance Their is no provision of inheritance.
private and protected.
Data is hidden in three modes public,
There is no proper way of hiding the data,
Data hiding & security private, and protected. hence data security
so data is insecure
increases.
Global data is shared among the functions Data is shared among the objects through
Data sharing
in the program. the member functions.
Classes or function can become a friend of
Friend functions/classes No concept of friend function. another class with the keyword "friend".
Note: "friend" keyword is used only in c++
Concept of virtual function appear during
Virtual classes/ function No concept of virtual classes .
inheritance.
Example C, VB, FORTRAN, Pascal C++, JAVA, VB.NET, C#.NET.
• Defining Object-Oriented Programming:-

“an approach that provides a way of


modularizing programs by creating partitioned
memory area for both data and functions that
can be used as templates for creating copies
of such modules on demand.”
BASIC CONCEPTS OF OBJECT
ORIENTED PROGRAMMING
• Objects
• Classes
• Data Abstraction & Encapsulation
• Inheritance
• Polymorphism
• Dynamic Binding
• Message Passing
Objects
• Run time entity.
• Ex. Student, account, time, etc.
• Match closely to real world objects.
• Represents user defined data.
• Object contains data and code / functions to operate
on data.
• Take up space in memory and has memory address.
• Objects interact by sending messages to each other.
• Ex. Students & course; customer & account.
• Ways of Representing objects (fig. 1.7)
Classes
• User defined data type to wrap the set of data
and code of an object.
• Objects are variables of Class type; create ‘n’
number of objects.
• Class is collection of objects of similar type.
• Ex. Class ‘Animal’ creates Objects ‘Cat’,
‘Monkey’, ‘Elephant’, etc.
• Syntax: Animal Monkey;
Data Abstraction & Encapsulation
• Encapsulation – wrapping up of data and
functions into a single unit (i.e. class).
• Data is hidden from outside world; Data /
Information hiding.
• Functions inside the class can only access the
data.
• Abstraction – representing essential features
without including the background details.
• Classes uses abstraction; list of attributes (data
members) of an object. They are ADT.
Inheritance
• Process by which object of one class acquire
the properties of another class.
• Concept of Hierarchical classification.
• Idea of reusability
– Deriving new class from existing one.
– Additional features to an existing class.
• Fig. 1.8.
Polymorphism
• A Greek term meaning ability to take more than
one form.
• Different behaviors in different instances.
• Concept of Operator Overloading.
– Ex:- addition of two numbers is sum; addition of two
strings is concatenation.
• Concept of Function Overloading.
– Ex:- single function name handles different number
and different types of arguments.
• Fig. 1.9
Dynamic Binding
• Binding – linking of a procedure call to the
code to be executed.
• Dynamic Binding / Late Binding – code
associated with a procedure call is not known
until the time of call at run-time.
Message Passing
• Objects communicate with each other
through messages.
• Message can be request for execution of a
procedure.
• Invokes a function in the message receiving
object that generates the result.
Feature-based Classification
• Object-based programming languages:-
– Data Encapsulation
– Data hiding
– Automatic initialization and clear-up of objects
– Operator Overloading
• Object-oriented programming languages:-
– All the above features along with
– Inheritance
– Dynamic Binding
Benefits of OOP
Benefits to Both Designer & User:-
• Inheritance – eliminate redundant code; extend
the use of existing classes.
• Standard working modules can be reused; saving
development time and higher productivity.
• Data hiding – build secure programs.
• Multiple instances of an object co-exists.
• Map the objects in the problem domain.
• Partitioned the work based on objects.
• Easy to upgrade from small to large systems.
• Complexity can be easily managed.
Applications of OOP
• Real-Time systems
• Simulation and Modeling
• Object-Oriented databases
• Hypertext, hypermedia and expertext
• AI and Expert systems
• Neural network and Parallel Programming
• Decision support and Office automation system
• CIM / CAM / CAD systems
Beginning with C++
• What is C++?
• Applications
Developing a simple Program in C++
//First C++ program

#include<iostream.h>
#include<conio.h>

int main()
{
int number;
clrscr();
cout<<“Enter number”;
cin>> number;
cout << “the number is” << number;
getch();
return 0;
}
Exercise
• Check whether a number is prime or not
• Check Whether Number is Even or Odd
• Create Pyramid and Pattern
• Find Quotient and Remainder
• Swap Two Numbers
• Check Whether a character is Vowel or
Consonant
• Find Largest Number Among Three Numbers
• Calculate Sum of Natural Numbers
• Check Leap Year
• Find Factorial
Points to Ponder
• Keywords
• Basic data types
• User-Defined data types (struct & union)
Reference Variables
• Provides an alias i.e. alternative name for already
defined variable.
• Both the names can be used interchangeably.
• Syntax:- data-type & reference-name = variable-name
(note:- & represents reference to that data type)
• Ex:- float total = 100;
float & sum = total; //reference variable sum is
initalized to value 100.
• cout<<total; cout<<sum; // same output.
• total = total +10; //changes both to 110.
• sum=0; // changes both to 0.
• It can also be created for user defined data types.
• More Examples:-

1) int n[10];
int &x = n[5]; //alias for n[5] value i.e. 6th value.
char &a=‘\n’; // holds next line char
cout<<x; //prints value of n[5]
cout<<a; //next line

2) int x;
int *p = &x;
int &m = *p; // i.e. int &m=x;
//m refers to x
Application of Reference Variable
• Passing arguments to function.
Call by reference:-

void function1(int &x) // if no ‘&’ (call by value), then x takes value


of m but m doesn’t change
{
x=x+10; // x and m both are incremented.
cout<<x;
}
void main()
{
int m = 100;
function1(m);
cout<<m; // x is alias of m
}
Scope Resolution Operator
• Scope of any variable extends from its declaration
to end of the block containing the variable
declaration; local variable of block.
• In nested blocks:-
– Declaration in inner block hides the declaration of
same variable in outer block.
– Global version of any variable can not be accesses
from within the inner block.
– This problem can be resolved by Scope Resolution
Operator (::).
– Ex:- Program 3.1
Course Outcomes
• On successful completion of the course,
students will be able to:
• Conceptualize object oriented features and their
implementation.
• Implement classes and member concepts.
• Develop programs using constructors, destructors and
inheritance
• Implement operator overloading and streams paradigm

Das könnte Ihnen auch gefallen