PAN African E-Network Project: Pgdit
PAN African E-Network Project: Pgdit
PAN African E-Network Project: Pgdit
PGDIT
Object Oriented Programming
Semester 2nd
Session - 1
Welcome
to
Object Oriented
Programming
Session Outline
Genesis of C++
Structured vs. Object Oriented Programming
Basics Concepts of Object Oriented Programming
Structure of C++ program
C++ Comments
A simple C++ program with explanation
Using streams for input and output (cin , cout)
Placement of variable declarations
The const qualifier
Executing a C++ program
A Class in C++
Synonyms
Practice Questions & answers
GENESIS OF C++
Factors that contributed to the evolution
Increasing complexity of the problem domain.
Difficulty in capturing the complexity of the problem domain
through procedural languages(like C, Fortran, Pascal)
Difficulty in modeling real-world objects through procedural
languages.
GENESIS OF C++
Growing demands on software in terms of
Correctness
Maintainability
Reusability
Scalability
Openness and interoperability
Integrity
GENESIS OF C++
Commercial viability
A huge investment in C lead to the incorporation of object
oriented features with C leading to
C++ is a superset to C and C++ = C +OOPs.
C++ provides object-oriented programming (OOP).
OOPs was invented to overcome the drawbacks of the
pop(Procedure-Oriented Programming).
Structured
Vs
Object-oriented
Development
Structured Programming
In the structured 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 the next slide.
Function - 1
Function - 2
Function - 4
Function - 3
Function - 5
Function - 6
Global Data
Function - 1
Function - 1
Function - 1
Local Data
Local Data
Local Data
Data
Functions
Functions
Object A
Object B
Functions
Data
Object C
Library
Management
System
Comparison
b/w OOAD &
SSAD
Object-Oriented A/D
Structured A/D
Decompose by
objects or concepts
Decompose by
functions or processes
Book
Catalog
System
Library
Librarian
Record
Issues
Add To
Stock
Report
Fines
OOP
Emphasis is on procedure.
1.
2.
3.
4.
5.
6.
7.
Objects
Classes
Encapsulation
Abstraction
Inheritance
Polymorphism
DynamicBinding&
MessagePassing
1
2
7
5
Objects
Objects are often used to model real world entities
that we normally find in everyday life.
An object can be a person , place , thing ,concept or
anything we see in the real world . Some common
examples of an object include car ,book, computer ,
apple etc.
The principal building blocks of object-oriented
programming.
Each object is a programming unit consisting of data
(variables) and functionality (methods).
OBJECT:STUDENT
Data
A. Name
B. Date Of Birth
C. Marks
....................
A
2
B
Functions
1. Total
2. Average
3. Display
................
Objectscontaindata(likeName,DOB,Marks)andfunctions
(likeTotal,averageanddisplay)tomanipulatethatdata.
Objects
Each object possess the following characteristics.
Each object has an identity and are distinguishable.
Data and functions are binded together to form an
object.
Objects in the system communicate with each other
using the concept of message passing.
Objects store data which is manipulated by its
functions.
Since all software applications serve people living in the
real world, then that software should mirror the real world
as closely as possible, both in its workings and in its
appearance :: thats the reason to use objects
Class
A class is a general specification or description for a
set of objects that defines their common structure
(data), behaviour (methods), and relationships
(inheritance).
A class is a prototype that defines the variables and
methods common to all objects of a certain kind.
Once a class has been defined, we can create any
number of objects belonging to that class.
If fruit has been defined as a class, then the
statement fruit mango; will create an object mango
belonging to the class fruit.
Class
Encapsulation
Encapsulation
Encapsulation
Book Id
Book author
Book title
Issue()
Book_status
Book_description
Library Class
Roll no
Name
Class
Input()
Show()
Student Class
ADVANTAGES OF ENCAPSULATION
Abstraction
Abstraction Simplicity is good; complexity is bad.
Abstraction is a process that involves identifying the essential
features without including the internal details.
A good abstraction is the one that emphasizes details that are significant
and suppresses details that are not.
Let us take a real world example ::
Consider an example of a Television which consists of features such as
changing the channel ,ON/OFF switch etc. User interacts with television
using interfaces like channel changer, volume controller, and power button
without going in to internal details of the working of each of the interface.
The user is not concerned about how it receives signals over the air.
,translate them and display on the screen. So,it shows how the internal
complexity of working of the television is hidden from the user.
Abstraction
Abstraction
In object oriented programming , classes uses the concept of Abstraction.
A class encapsulates the relevant data and functions that operate on data
by hiding the complex implementation details from the user . In other
words ,user needs to focus on what a class does rather than how it will be
implemented.
The following diagram shows that the basic concept of Abstraction.
Inheritance
Inheritance is the process by which objects of one class
acquire properties of objects of another class.
Types of class in inheritance.
1. Base class.
2. Derived class.
Grand Father
Father
// Base class
// Derived class
Inheritance
Inheritance is the process by which one
object acquires the properties of one or more
other object. It supports the concept of
hierarchical classification (top-down).
An object need to define only those qualities
that make it unique within its class.
It implements "is-a" relationships between
objects.
Inheritance creates a hierarchy of related
classes (types) which share code and
interface.
INHERITANCE EXAMPLES
Base Class
Derived Class
Employee
Manager
Researcher
Worker
Account
Checking Account
Saving Account
Generalization/Specialization
Generalization/Specialization represents the is
a relationship set, an essential element of the
object oriented paradigm. The main idea in
Generalization/ Specialization is that one
object class (the specialization) is a subset of
another (the generalization).
A generalization-specialization (gen-spec)
relationship states that one object is a kind-of
(or is-a) the other, and that the other is a
generalization of the object
Specialization
Specialization is either:
the process of defining a new object based on a
(typically) more narrow definition of an existing
object, or
an object that is directly related to, and more
narrowly defined than, another object.
A specialization relation requires that every
instance of the subtype be an instance of the
supertype.
It is common to say that everything that is true for a
generalization is also true for its corresponding
specialization.
POLYMORPHISM
The word Polymorphism is derived from two Greek
words Poly means many and morphs means forms.
So , polymorphism means the ability to take many
forms.
Polymorphism means one name, multiple forms. It
allows us to have more than one function with the
same name in a program .
It also allows overloading of operators so that an
operation can exhibit different behaviors in different
instances.
Basics of OOP
Polymorphism Example
Ex:-addition(+)
For two numbers ,the operation will generate a sum.
For two strings , then the operation would produce a
third string by concatenation.
Basics of OOP
Dynamic Binding
MESSAGE PASSING
In Object oriented programming (OOP) different objects
communicate with each other using the concept of Message
Passing. From the programmer point of view ,message passing
provides an easy way of modeling real world problems on the
computer. Objects communicate with each other in the same way as
human beings exchange messages among themselves.
A message comprises of the name of the object , name of the
function and any information (arguments) to be sent to the object.
For example :If a user wants to check the balance of his account ,
he simply sends the message getbalance to the object account by
passing the information account_no. as shown below
account. getbalance (account_no)
account:: Object
Getbalance:: Message
account_no :: Argument or Onformation
Main Program Section:: from here the execution of program actually starts.
Eg:: int main()
{ // Executable Part
}
SubProgram Section :: includes user defined functions.
C++ Comments
C++ supports the C comment format where /* begins a
comment and */ ends it.
The // also starts a comment in C++ (unless it is inside a
string). Everything to the end of the current line is a
comment.
e.g.,
/* This is a comment */
//
//
This is invalid
in the next line
/*
This is valid in
the next line */
C++ Comments
Good practice: C++ style of comment is recommended as can
be seen in the following example.
C++ style
if(a>b)
{ //int temp=a;
//swap a, b
//a=b;
//b=temp;
}
C style
if(a>b)
{ /*
int temp=a; /*swap a,b*/
a=b;
b=temp; */
}
Streams
47
use cin
eg cin >> num_in;
48
49
main()
{
cout << Enter a number ;
int num;
// declaration of variable within block
cin >> num;
cout << Number = << num;
}
Bytes
Range
Char
-128 to 127
Unsigned char
0 to 255
Signed char
-128 to 127
Int
-32768 to 32767
Unsigned int
0 to 65532
signed int
-31768 to 32767
Short int
-31768 to 32767
0 to 65535
-32768 to 32767
Long int
-2147483648 to
2147483647
-2147483648 to
2147483647
0 to 4294967295
Float
3.4E-38 to 3.4E+38
Double
1.7E-308 to
1.7E+308
Long double
10
3.4E-4932 to
1.1E+4932
BOOL
It takes only two values: true or false
true=1;
false=0;
eg: #include<iostream.h>
#include<conio.h>
void main()
{
bool b=32;
bool i=false;
cout<<b<<i;
int j=b+b;
bool k=b+b;
cout<<j<<k;
getch();
}
output
10
2 true
output
A Class in C++
Class is a user define data type.
Class is a collection of objects of similar type.
Class Declarations.
class name
{
Data types;
Member functions
};
What is a class ?
A class is
An abstract datatype similar to a C structure.
A representation of objects and the sets of operations that can
be applied to such objects.
Definition of a class
e.g.,
Class A
{
int i ;
char *strng;
Void f (int , char *);
int g() ;
};
Data
Functions
Public area
Entry
allowed
Data
Functions
Synonyms
Operations - "method selectors," "method
interfaces," "messages," or "methods",
services,member function, Instance Method
Object - instance, class instance, surrogate,
entity.
Class - type
Base Class - Super Class, Parent Class.
Derived Class - subtype, subclass, Child
class.
Practice Questions 1
Q1 Identify the drawback of using
procedure-oriented programming, if any:
a) Data is hidden from external functions
b) New functions can be added whenever
necessary
c) Does not reflect real world problems
d) All of the above
Practice Questions 2
Q2 Which one of the following OOP
concepts enables reusability of
components?
a) Inheritance
b) Encapsulation
c) Polymorphism
d) All of the above
Practice Questions 3
Q3 The concept of hierarchical
classification is related to:
a) Abstraction
b) Inheritance
c) Function overloading
d) None
Practice Questions 4
Q4 Comments in C++ starts with
_______ symbol.
a) //
b) \\
c) **
d) None of the above
Practice Questions 5
Q5 return is an example of a
a) Keyword
b) Function
c) Statement
d) Comment
Practice Questions 6
Q6 The declaration of global variables
must be made
a)inside the function
b) outside the function
c) in a function header line
d) None of the above
Practice Questions 7
Q7 Consider the following code:
cout<< Welcome;
This is an example of
a) Return type
b) Function
c) Both a and c
d) C++ statement
Answers
Q1 c
Q2 a
Q3 b
Q4 a
Q5 a
Q6 b
Q7 d
Thank You
Please forward your query
To :[email protected]
Cc: [email protected]