Professional Documents
Culture Documents
Profile
BS (Computer & Information Sciences)
PIEAS
MS (Software Engineering)
NUST College of E&ME
Shoaib M. Zafar
Instructor Profile
Courses Taught so far
Computer Graphics (DCS, IIUI) (Semester VI)
In Spring 2015
Instructor Profile
Current Semester Assignment (Spring 2016)
Computer Programming
BEME A (Fall 2015)
Shoaib M. Zafar
Shoaib M. Zafar
Assignments
Quizzes
Midterm
Lab
Semester Project
Final Exam
Shoaib M. Zafar
(05%)
(05%)
(20%)
(20%)
(10%)
(40%)
How many???
Names AND Roll No.: Top Right Corner
Assignment/Quiz No.: Top Center
Late Submissions & Copying = Zero
Shoaib M. Zafar
Class Discipline
Shoaib M. Zafar
Course Outline
Introduction: Procedural versus Object Oriented
Programming (OOP), characteristics of OOP,
advantages of OOP, Abstract Data Types (ADT),
information hiding, encapsulation.
Classes and Objects: Classes, objects, access
specifiers, data members, member functions,
properties, getters and setters, object aggregation.
Constructors and Destructors: Default constructors,
overloaded constructors, copy constructor, conversion
constructor, shallow vs. deep copy.
Shoaib M. Zafar
Course Outline
Static Members: Static data members and
static member functions.
Generic Programming and Overloading:
Function overloading, operator overloading,
templates, C++ standard template library
(STL).
Dynamic memory management for objects:
Pointers to objects, reference variables
Shoaib M. Zafar
10
Course Outline
Inheritance and Polymorphism: Inheritance,
types of inheritance, derived classes, function
overriding, dynamic binding, polymorphism,
virtual functions.
Streams and Files: Stream classes, File
objects, File operations with streams.
Object-Oriented Design: Introduction
Unified Modeling Language (UML).
Shoaib M. Zafar
to
11
Procedural vs Object-Oriented
Languages
Procedural Language
Views a program as a series of steps to be
carried out
E.g. C, FORTRAN, Pascal
12
Causes of Problems
Unrestricted Access to Data
Global data is allowed
Access to data by multiple functions means many
connections between functions
Programs become difficult to understand, modify
and maintain
Shoaib M. Zafar
13
Shoaib M. Zafar
14
Example
Procedural program
1. Gather ingredients
Flour, butter, egg, sugar etc.
Shoaib M. Zafar
15
Example
Object Oriented programing
Model for an Object
Properties
List of ingridients, or set of data
Methods
List of actions or instructions
Shoaib M. Zafar
16
Example
Object Oriented programing
Baker
{
Properties (ingredients)
Flour
Butter
Eggs
Milk
Cake pan
Oven
Methods (actions)
Bake cookies
Bake cake
Bake pie
Shoaib M. Zafar
1. Gather ingredients
Flour, butter, egg, sugar etc
2. preheat oven to 350 degree
3. beat eggs and butter
4. add sugar
5. mix
6. bake 10 mins
Object Oriented Programming
17
Function
Shoaib M. Zafar
Global
Data
Function
Global
Data
Function
Function
18
Goal of OOP
Clearer, more reliable, more easily
maintained programs
More effective way of coping with program
complexity
Shoaib M. Zafar
19
Java
Lacks certain features, e.g. multiple inheritance,
pointers, templates
Less powerful than C++ (but more safe, of course)
C#
Emerging
Shoaib M. Zafar
20
The OO Approach
The fundamental idea is to combine into
a single unit both data and functions
that operate on the data.
Such a unit is called an Object.
An objects functions are called
member functions in C++
And its data is called data members.
Shoaib M. Zafar
21
The OO Approach
An objects data is typically accessed
through its member functions, i.e. it is
hidden from accidental alteration
Data and its function are said to be
encapsulated into a single entity
Data encapsulation and data hiding are
key elements of object-oriented languages
Shoaib M. Zafar
22
The OO Approach
If you want to modify data in an object, you
know exactly what functions interact with it
(i.e. the member functions of the object).
This simplifies writing, debugging, and
maintaining the programs
An OO program consists of a number of
objects which communicate with each
others member functions
Shoaib M. Zafar
23
Data
Member
Function
object
Member
Function
Data
Data
Member
Function
Member
Function
Member
Function
Member
Function
Shoaib M. Zafar
object
24
Characteristics of OO Languages
Objects
Classes
Encapsulation
Inheritance
Polymorphism and overloading
Shoaib M. Zafar
25
Classes
Objects belong to classes
A class and an object of that class has the
same relationship as a data type and a
variable
All objects with the same characteristics (data
and functions) constitute one class.
A class serves only as a plan, or a template,
or sketch- of a number of similar things
It merely specifies what data and what
functions will be included in objects of that
class.
Shoaib M. Zafar
26
Classes
Declaring a class doesnt create any
objects, just as mere existence of data
type int doesnt create any variables.
A class is thus a description of a no. of
similar objects.
For instance, HUMAN is a class, and
JOHN is its instance (object)
Shoaib M. Zafar
27
Encapsulation
Information hiding
Encapsulation is the mechanism that binds
together code and the data it manipulates,
and keep both safe from outside
inteference and missuse
object
object
Data
Data
Member
Function
Member
Function
Member
Function
Member
Function
Shoaib M. Zafar
28
Inheritance
Derive other (sub-)classes from an existing
class
The original class is called the BASE CLASS;
the others are DERIVED CLASSES
Each class shares common characteristics
with the class from which it was derived, and
can also add its own modifications, additions.
For instance, VEHICLE is a class from which
CAR, TRUCK, BUS, MOTORCYCLE classes
can be derived.
Shoaib M. Zafar
29
Inheritance
Base class
F A
Feature
F B
Feature
F A
Feature
F A
Feature
F A
Feature
F B
Feature
F B
Feature
F C
Feature
F B
Feature
F
Feature
D
F F
Feature
F
Feature
E
Derived classes
Shoaib M. Zafar
30
Shoaib M. Zafar
31
C and C++
C++
Shoaib M. Zafar
32
Shoaib M. Zafar
33
Program Explanation
<iostream>
This header supports C++ I/O operations.
iostream is to C++ what stdio.h is to C
int main()
Shoaib M. Zafar
34
Program Explanation
cout
An object
Predefined in C++ to correspond with the standard output
stream
Stream
An abstraction that refers to a flow of data
The standard output stream normally flows to the screen
Shoaib M. Zafar
35
Shoaib M. Zafar
36