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

6/27/2018

Object Oriented Programming


Dr. Muzammil Khan

Assistant Professor
Department of Computer Science

Office E – 12

Evaluation
 Evaluation Criteria
 Total Marks 100% (100)
 Final Term Exam 50%
 Mid Term Exam 30%
 Assignments + Presentations + Quizzes 20%
 Recommended Readings
 Object Oriented Programming in C++
 By Robert Lafore
 C++ By Aikman Series
 C++ How to Program
 By Deitel & Deitel
 The C++ Programming Language By Bjarne Stroustrup
 Miscellaneous i.e. Internet etc…
Object Oriented Programming
2

1
6/27/2018

Evaluation (Cont…)
 Policies
 Late assignments may be accepted with marks reduction.
There will be a 10% reduction for assignments submitted up
to 24 hours late
 Students who have copied assignments or whose
assignments have been copied will both be given a zero
 Plagiarism is not acceptable. Anyone found to be guilty of
plagiarism in an assignment will be given a zero in that
assignment
 Quizzes may be unannounced or announced (depends on
your response!)
 At least one “Major Assignment”
 Project
Object Oriented Programming
3

Chapter 1
Introduction
Object Oriented Programming
(OOP)

Object Oriented Programming


4

2
6/27/2018

You Know !
 Basics of C and C++ Programs
 Basic Terminologies of C and C++
 Different operators
 Conditional statements
 Loops statements
 Arrays
 Structures
 Functions
 Library functions
 User defined functions
 Pointers
 Etc . . .
Object Oriented Programming
5

Course Objective

 Objective of this course is


 To make students familiar with the concepts of object
oriented programming

 Concepts will be reinforced


 By their implementation in C++

Object Oriented Programming


6

3
6/27/2018

Course Contents
 Object Orientation
 Object Orientation Model
 Objects and Classes
 Overloading
 Inheritance
 Polymorphism
 Brief about Filing
 Brief about (Depends on time)
 Generic Programming
 Exception Handling
 Introduction to Design Patterns

Object Oriented Programming


7

Procedural vs. Object Oriented Programming


 The unit in procedural programming is function, and
 The unit in object-oriented programming is class

 Procedural programming concentrates on creating


functions, while
 Object-oriented programming starts from isolating the
classes, and then look for the methods inside them

 Procedural programming separates the data of the program


from the operations that manipulate the data, while
 Object-oriented programming focus on both of them

Object Oriented Programming


8

4
6/27/2018

Object Orientation
 What is Object-Orientation?

 A technique for system modeling

 OO model consists of several interacting objects

 What is a Model?

 A model is an abstraction of something

 Purpose is to understand the product before developing it

Object Oriented Programming


9

Examples – Model
 Includes
 Highway maps
 Architectural models
 Mechanical models
 Etc...

 Example – Real world objects

Object Oriented Programming


10

5
6/27/2018

Example – OO Model
Lives-in
 Objects
 Ali
Drives In the
 House
 Car
 Tree

 Object Interactions
 Ali lives in the house
 Ali drives the car
 Tree in the house

Object Oriented Programming


11

Advantages of Object Orientation


 Naturally, people think in terms of objects
 OO models map to reality
 Mimic real world scenarios
 Therefore, OO models are
 Easy to develop
 Easy to understand
 Code Reuse and Recycling
 Objects created can easily be reused in other programs
 Encapsulation
 Objects have the ability to hide certain parts
 Design benefits
 Better maintenance, less flaws etc...
Object Oriented Programming
12

6
6/27/2018

Object
 An Object is
 Something tangible / real
 For example
 Ali,
 Car etc…
 Something that can be apprehended intellectually
 For example
 Time, Date
 An Object has
 State (attributes)
 Well-defined behaviour (operations)
 Unique identity
Object Oriented Programming
13

Example – Ali is a Tangible Object


 Ali is a Tangible Object
 It has
 State (attributes)
 Name
 Age
 Behaviour (operations)
 Walks
 Eats
 Identity
 His name

Object Oriented Programming


14

7
6/27/2018

Example – Car is a Tangible Object


 Car is a Tangible Object
 It has
 State (attributes)
 Color
 Model
 Behaviour (operations)
 Accelerate
 Start Car
 Change Gear
 Identity
 Its registration number

Object Oriented Programming


15

Example – Time is an Object Apprehended


Intellectually
 Time is an Object Apprehended Intellectually, It has
 State (attributes)
 Hours
 Seconds
 Minutes
 Behaviour (operations)
 Set Hours
 Set Seconds
 Set Minutes
 Identity
 Would have a unique ID in the model

 Similarly, Time is an Object Apprehended Intellectually


Object Oriented Programming
16

8
6/27/2018

Features of OOP Languages


 An Object Oriented Programming languages has three
features

Or

 OOP languages has the Concepts of


1. Classes
2. Inheritance
3. Polymorphism

Object Oriented Programming


17

Abstraction
 Abstraction is a way to cope with complexity
 Principle of abstraction
 “Capture only those details about an object that are relevant
to current perspective”

 Example – Scenario
 Ali is student of PhD and also Teaches BS students
 related Attributes
 Name - Employee ID
 Student Roll No - Designation
 Year of Study - Salary
 CGPA - Age
Object Oriented Programming
18

9
6/27/2018

Example - Abstraction
 related Behaviour
 Study - DevelopExam
 GiveExam - TakeExam
 PlaySports - Eat
 DeliverLecture - Walk

 Remind (Principle of abstraction)


 “Capture only those details about an object that are relevant
to current perspective”

Object Oriented Programming


19

Example - Abstraction
 Student’s Perspective
 Attributes
 Name - Employee ID
 Student Roll No - Designation
 Year of Study - Salary
 CGPA - Age

 Behaviour
 Study - DevelopExam
 GiveExam - TakeExam
 PlaySports - Eat
 DeliverLecture - Walk
Object Oriented Programming
20

10
6/27/2018

Example - Abstraction
 Teacher’s Perspective
 Attributes
 Name - Employee ID
 Student Roll No - Designation
 Year of Study - Salary
 CGPA - Age

 Behaviour
 Study - DevelopExam
 GiveExam - TakeExam
 PlaySports - Eat
 DeliverLecture - Walk
Object Oriented Programming
21

Example - Abstraction
 A cat can be viewed with different perspectives

 Engineer’s Perspective

Driver’s Perspective

Object Oriented Programming


22

11
6/27/2018

Abstraction – Advantages

 Simplifies the model by hiding irrelevant details

 Abstraction provides the freedom to different


implementation decisions by avoiding commitment to
details

Object Oriented Programming


23

Class
 In an OO model, some of the objects exhibit identical
characteristics
 Such as
 Information structure and
 Behaviours
 We say that they belong to the same class
 For Example
 Ali studies mathematics
 Anam studies physics
 Sohail studies chemistry

 Each one is a Student and hence belong to same class

Object Oriented Programming


24

12
6/27/2018

Class (Cont...)
 Graphical Representation of Classes

(Class Name)
(Class Name)
(attributes)

Suppressed
(operations)
Form

Normal Form
Object Oriented Programming
25

Class (Cont...)
 Example

Person
name Person
age
gender Suppressed Form
eat
walk

Normal Form

Object Oriented Programming


26

13
6/27/2018

Class (Cont...)
 In C++ or in OOP Class is
 Collection of Data and function
 About an object
 Data and functions are define within the class
 The functions is written to work upon the data
 Classes are define to create user-define data types
 Similar to built-in data types provided by C++ (concepts)
 Defining class does not create any space in computer
memory
 But define the data items and functions
 When its object is declared memory space is reserved

Object Oriented Programming


27

Class (Cont...)
 Classes and structure are
 Similar in concept
 Similar in syntax
 Structures are used to hold data items only while
 Classes are used to hold data items as well as functions

Object Oriented Programming


28

14
6/27/2018

Defining a Class
 Class is define in similar way as structure
 The general syntax is

class <class_name>{
body of the class
};

 class
 Is keyword to define a class
 class_name
 Represent name of the class
 The object is created using this name
Object Oriented Programming
29

Defining a Class (Cont…)


 Rules to give name to the class is same as variables

 Body of the class


 Consists of
 Data items and
 Functions
 Called members of the class
 Define within the braces

 Semicolon
 Body of the class ends with “ ; ”

Object Oriented Programming


30

15
6/27/2018

Members of a Class
 Class consists
 Data items &
 Functions
 These are called members of the class

 Data Items
 Data items of the class called data members of the class
 Functions
 Functions of the class called member functions of the class
 Define to work on the data items
 Member functions
 Can be define inside or outside of the class

Object Oriented Programming


31

Example – Class

Class xyz {
private:
int a, b, c;
public:
void get(void){
cout<<“Enter values for a, b & c”;
cin>>a>>b>>c;
}
void put(void){
cout<<“a = ”<<a<<endl;
cout<<“b = ”<<b<<endl;
cout<<“c = ”<<c<<endl;
}
};

Object Oriented Programming


32

16
6/27/2018

Example – Class
 Data items
Class xyz {
private:
int a, b, c;
public:
void get(void){
cout<<“Enter values for a, b & c”;
cin>>a>>b>>c;
}
void put(void){
cout<<“a = ”<<a<<endl;
cout<<“b = ”<<b<<endl;
cout<<“c = ”<<c<<endl;
}
};
Object Oriented Programming
33

Example – Class
 Functions
Class xyz {
private:
int a, b, c;
public:
void get(void){
cout<<“Enter values for a, b & c”;
cin>>a>>b>>c;
}
void put(void){
cout<<“a = ”<<a<<endl;
cout<<“b = ”<<b<<endl;
cout<<“c = ”<<c<<endl;
}
};
Object Oriented Programming
34

17
6/27/2018

Member Access Specifiers


 The commands that determine whether
 A member of the class can be access outside the class or not
???
 Normally we have three types of access specifiers
1. Private
2. Public
3. Protected
 Syntax
 private: , public: , protected:

 Protected will discussed in next chapter

Object Oriented Programming


35

Private Specifier
 Private members
 Members that can be accessed only from within the class
 And cannot access outside the class
 Normally
 Data members are declared as private
 Member functions can also declared as private
 Class member that come after private specifier
 Upto the next specifier are declared as private
 The default specifier is private specifier
 If no specifier is mention then it means
 The members are private
 Declaring data as private means “hiding it from out side”
Object Oriented Programming
36

18
6/27/2018

Public Specifiers
 Public members
 Members that can be accessed from within the class
 And can be access outside the class

 Normally
 Member functions are declared as public
 Data members can also declared as public (not so)

Object Oriented Programming


37

Object
 Data type is used to declare a variable
 Variable is also called
 Instance
 Case

 Each variable has unique name but


 Follow the rule of its data type
 When variable is declared
 Space is reserved for it memory
 Class is also called
 Data type

Object Oriented Programming


38

19
6/27/2018

Object (Cont...)
 Class can be used as to
 Declared variable, i.e. instance or case
 Class variable is called OBJECT
 Can declare more then one object for a class
 Each object has unique name

 Object contain both


 Data items &
 Member functions
 To combine both data and function in one unit is called
 Data Encapsulation
 Hiding data from out side

Object Oriented Programming


39

Object (Cont...)
 Values of data members in objects are
 Different or
 Same
 The values in the object is called
 State of the object
 The member functions are also called
 Methods
 The data hiding or data encapsulation is main feature of
OOP
 Provided by Class concept

Object Oriented Programming


40

20
6/27/2018

Declaring Objects
 Object of a class is declare in the same way as
 Variable of any data type Or
 Variable of a structure
 When class is define
 Reserve no space
 Just show how the object will look like
 When object is declared memory space is reserved
 Syntax
 class_name object_name;
 class_name object_names; (separated by commas)

Object Oriented Programming


41

Calling Member Functions


 Member function is called similarly as
 Calling data items of a structure

 Member function is called through


 Object &
 Dot operator

 Syntax
 object_name.member_function;
 Dot operator is also called class member access operator

 Those member functions can be access through object


those declared as public
Object Oriented Programming
42

21
6/27/2018

#include<iostream.h>
#include<conio.h> void main (){
class edate{ clrscr();
private: edate obj;
int year, month, day; obj.getDate();
public: obj.printDate();
void getDate(void){ getch();
cout<<“Enter Year”; }
cin>>year;
cout<<“Enter Month”;
cin>>month; Output
cout<<“Enter Day”; Enter Year 2013
cin>>day; Enter Month 3
} Enter Day 28
void printDate(void){ Date Today is: 28/3/2013
cout<<“Date Today is: ”;
cout<<day<<“/”<<month<<“/”<<year;
}
};

43
Example – Program

Defining Member Function Outside the Class


 Member function can be define
 Outside the class
 Only the prototype is define inside the class
 Scope resolution operator (::) is use to define member
function outside
 Syntax
data_type class_name :: function_name (arguments) {
Body of the function;
}
 Useful when the function body is large
 Otherwise
 Feasible to define inside the class
Object Oriented Programming
44

22
6/27/2018

Storage of Objects in Memory


 When object is created
 Space is reserved in computer memory for
 Data members &
 Member functions

 Member functions of a class


 Stored at only one place
 Shared among all objects

 While data members store


 At different location for each object

Object Oriented Programming


45

Constructors
 Is a member function

 When an object is created


 Constructors Called and execute automatically

 Name of the constructor function


 Is same as class name

 Constructor may have


 Arguments but
 It cannot Return any values
 Mostly use for initialization

Object Oriented Programming


46

23
6/27/2018

Constructor Types
 Three types of constructors
1. Default Constructor
2. Copy Constructor
3. Parametric Constructor

 The default constructor does not take any arguments

 A copy constructor takes an instance

 A parametric constructor takes an instance of the same


class as a constant reference argument

Object Oriented Programming


47

Constructors Overloading
 Defining more then one constructors with
 Same name, but
 Different return data type and parameters

 In simple word
 Defining more then one constructors with
 Different signatures
 Signatures means return data type and parameters

 Construction overloading is used to initialize different


values to a class

Object Oriented Programming


48

24
6/27/2018

Destructors
 It is a member function of a class

 When an object is destroyed


 Destructors Called and execute automatically

 Name of the destructors function


 Is same as class name preceded by tilde sign (~)

 Destructors
 Do not have or need any arguments &
 It cannot Return any values

Object Oriented Programming


49

End of Chapter
 Homework
 Write programs to practice the concepts of this chapter

 Quiz
 You may have quiz next week

Object Oriented Programming


50

25

You might also like