[go: up one dir, main page]

0% found this document useful (0 votes)
57 views41 pages

EE-123 OOP Spring 2017 Lect 1-4

This document discusses object-oriented programming and the motivation for using an OOP approach. It notes that procedural programming focuses on functions operating on data, while OOP encapsulates both data and functions within classes. OOP allows for reusable components like hardware, making it easier to assemble applications compared to procedural languages which require reinventing components for each new program. The document motivates OOP by comparing assembling a PC and car from reusable parts to how software should be assembled, and outlines drawbacks of procedural languages in creating reusable software.

Uploaded by

Taha Tariq
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)
57 views41 pages

EE-123 OOP Spring 2017 Lect 1-4

This document discusses object-oriented programming and the motivation for using an OOP approach. It notes that procedural programming focuses on functions operating on data, while OOP encapsulates both data and functions within classes. OOP allows for reusable components like hardware, making it easier to assemble applications compared to procedural languages which require reinventing components for each new program. The document motivates OOP by comparing assembling a PC and car from reusable parts to how software should be assembled, and outlines drawbacks of procedural languages in creating reusable software.

Uploaded by

Taha Tariq
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/ 41

Spring 2017

Object Oriented
Programming (EE-123)
BY ENGR. SYED ASAD ALI
LECTURER (ELECTRICAL ENGINEERING)-DHA SUFFA UNIVERSITY
2
Points To Ponder

 PUNCTUALITY
 If any student is late by 5 minutes, (s)he will be marked absent but Allowed to sit in Class.
 No quiz/assignment/hourly exam will be retaken if a student was absent.
 Late submission leads to a marks deductions
 SINCERE TO LEARN (Learn and Let Others to Learn)
 Unbearable, taunting and distracting behavior will be dealt strictly and may lead to disciplinary action against
him/her
 Student may not be allowed to appear in further classes and exams.
 Use of handheld device is prohibited during Lecture and penalized.
 Keep phone on Silent and Seek permission to use in case of emergency
 BE RESPONSIBLE
 Students are responsible to regularly check the announcements (Notice Board, LMS , Class). Failing to do so
cannot be accepted as an excuse.
3
Course synopsis & Books

 Objectives:
 To familiarize the students with the fundamental concepts of object oriented programming.
 Practice key features of object-oriented programming language using C++.
 To capable students to Use these concepts in solving real life problems.
 Reading Material
 Starting Out With C++ By Tony Gaddis 7th Ed
 Object Oriented Programming in C++ By Robert Lafore 4th Ed
 My Lectures Slides & Our Old Friend.. Mr. oogle !
 Prerequisite
 Introduction to Computing & Programming (EE-121)
4
Program and Teacher

 Program & Section


 BE (Electrical Engineering) :: Section 2A, 2B & 2C
 Credit Hour
 Theory: 2 & Lab:1
 Facilitator:
 Engr. Syed Asad Ali (Theory)
 Email: asad.ali@dsu.edu.pk or syed_asad@ymail.com
 Room # SF 218 (pl see notice board at the office door before consultation)
 Web :: https://sites.google.com/site/easadclass/ or Google easadclass
 Ext 161
Week and Sessions Brief Contents Assignments, Quizzes etc.

 Motivation behind object-oriented programming language


Week 1-2
Lectures 4  Recap of Procedural Programming Concepts like Data Types, Mathematical
Quiz # 1
Expressions, Relational Operators, Loops, Function and Arrays Assignment # 1

 Chapter # 9
Week 3-4
Lectures 4
 Introduction to Pointer Variables
 Pointer Arithmetic, Initializing the Pointers
 Chapter # 11
Week 5-6
Lectures 4
 Introduction to Structured Data
 Accessing Structure Members & Initializing a Structure
 Chapter # 13 & 14 Quiz # 2
Assignment # 2
 Introduction to Classes
Week 7-10
Lectures 8
 Defining an Instance of a Class
 Constructors & Destructors
 Instance(Object) and Static Members
 Chapter # 15
 Inheritance Quiz # 3
Week 11-14
Lectures 8
 Protected Members and Class Access Assignment # 3
 Polymorphism and Virtual Member Functions
 Multiple Inheritance
Week 15
Lectures 2 • Course Revision and Final Exam Overview
Marks & Grading Criteria 6

Final Examination Hourly Tests (Best 2 out of 3)

Assignments and Presentation Class Tests/ Quiz Weighted by Class Attendance and behavior

12%
8%

50%

30%
7
Quiz/Tests weighted by Attendance & Behavior

Attendance in % Weight**
100-90 100

90-85 95

85-80 90

80-75 85

Below 75* 0
* Also Marked as Short Attendance
** Weight can be enhance by class behavior
8
Assignments & Presentations

 Assignments & Presentation will have to be submitted in groups


 Well in time and Not Copied, As I will test it for plagiarism
 Failing to submitted on time or copied from others or Internet leads to ZERO
marks
 Each group can have 5 to 6 members max.
 I will settle your groups based on your grades
 Each group can only have one position holder (A /A- Student)
9
SUGGESTED READING

Lecture # 1-2 OBJECT ORIENTED PROGRAMMING


LANGUAGE
BY ENGR. SYED ASAD ALI
WHAT AND WHY..??
10
Object oriented Vs Procedural
Oriented programming language

 Procedural programming and object-oriented programming are


two ways of software development and program design.
 In Course EE121, you have learned to write procedural programs.
 In a procedural program, you typically have data stored in a
collection of variables and/or structures, coupled with a set of
functions that perform operations on the data.
 Usually the variables and data in a procedural program are passed to the
functions that perform the desired operations.
Cont.. 11
 As you might imagine, the focus of procedural programming is
on creating the functions(Actions) that operate on the
program’s data.
 Object-oriented programming (OOP) is a programming
paradigm based on the concept of "objects & Class“
 Class encapsulates both the data (aka attributes) and functions
(aka behaviors) within a "box", and specifies the public interface
for using these boxes. A realization of these classes is Object
We will learn about Objects and Classes in detail in Ch: 13 & 14 but first we
should know why Opp !
12
Why OOP? – an Motivation towards
OOPs Approach
 Example 1: Assembling a PC
 Suppose that you want to assemble your own PC
 You go to a hardware store and pick up:
 Motherboard, Processor, RAMs, Hard disk, Casing, Power supply, and put them together.
 Voila ! Your PC runs when turn on the power. It is not difficult to set up a machine from
hardware components.
 You need not to worry about whether:
 How's Motherboard works and how it was developed nor you think to re-invent it !
 Whether Hard disk has 4 or 6 plates & 3 inches or 5 inches in diameter
 RAM is made in Japan or Korea, and so on.
 All you have to make sure that you have assemble them correctly with proper interfaces
13
Cont.…

 Example 2: Assembling a Car:


 Similarly, a car is assembled from parts and components
 Chassis, Doors, Engine, Wheels, Brake, and Transmission.
 The components are reusable, e.g., a wheel can be used in many cars (of the same
specifications).
 Hardware, such as computers and cars etc. are assembled from parts, which
are reusable components.
 How about software? Can you "assemble" a software application by picking a
routine from here & there, and expect the program to run?
 The answer is obviously no! Unlike hardware, it is very difficult to "assemble" an
application from software components.
14
Traditional Procedural-Oriented
languages
 Since the advent of computer 60 years ago, we have written tons and tons
of programs.
 However, for each new application, we have to write the program from
scratch.
 Rather Reusing the Component, we re-invents the Component!
 Main problem in traditional procedural-oriented
programming language such as C, Fortran, Cobol, or Pascal
is that they Re-invents the Component
 Writing from the scratch
 Traditional procedural-oriented languages (such as C and
Pascal) suffer some notable drawbacks in creating reusable
software components.
 Why ??
15
Drawbacks of Procedural-Oriented
languages

 The programs are made up of functions. Functions are often not reusable. It is
very difficult to copy a function from one program and reuse in another
program because the function is likely to reference the headers, global
variables and other functions.
 Procedural-Oriented languages are centered on functions which are not well-
encapsulated as a self-contained reusable unit.
 The procedural languages are not suitable for solving real life problems. For
example, C programs uses if-else, for-loop, array, function, pointer, which are
low-level and hard to abstract real problems.
 Programs become complex as they grows up to capture real life problems
16
Origin of OOPs

 In 1970s, the US DoD commissioned a task force to investigate why its IT budget
always went out of control; but without much to show for.
 The findings are:
1. 80% of the budget went to the software
1. More than 80% of the software budget went to maintenance (only the remaining 20% for new
software development).
2. Hardware components could be applied to various products
1. Hardware can share and reuse!
3. Software procedures were often non-sharable and not reusable.
 The task force proposed to make software behave like hardware OBJECT.
 Subsequently, DoD replaces over 450 computer languages, which were then used
to build DoD systems, with an object-oriented language called Ada.
17
Object oriented programming
language

 Object-oriented programming (OOP) languages


are designed to overcome these problems.
 By creating a self-contained unit an Object.
 An object may be treated as a Box that
encapsulates both the attributes(Characteristics)
and behaviors(functions) within a "box", and
specifies the public interface for using these
boxes.
 To Create an object we first create its class.
 Object is the realization of class
 In OOP languages (such as Java, C++, C#) uses
software objects to solve the problem.
18
Use of software objects to Solve Pbs:

Benefits of OOP ??
19
Foundation of object-oriented
programming:

 Classes and Objects


 Abstraction
 Encapsulation
 Inheritance
 Polymorphism
For Detail , Go through Course EE121 Lectures 20
Click Here

Lecture # 3-4
RECAPITULATION OF PROCEDURE
BY ENGR. SYED ASAD ALI
ORIENTED PROGRAMMING
21
Computer Programming

 Programming a computer involves writing instructions that enable a computer to


carry out a single task or a group of tasks
 A computer programming language is both Art[Designing] and science[Syntax]
 The rules of any language make up its syntax
 A translator (called either a compiler or an interpreter) checks your program for
syntax errors and converts into Machine code
 syntax errors & Logical error ??
 compiler or an interpreter??
 Three steps while coming from a High-Level Program to an Executable File??
22
What is a Program Made of?

 Common elements in programming languages:


1. Key Words
 Reserved or dedicated words
2. Programmer-Defined Identifiers
 Variables
3. Operators
 Mathematical & Logical Operators
4. Punctuation
 Semicolon , comma
5. Syntax
 Grammar of language
23
Programmer-Defined Identifiers or a
Variable

 A variable is a name of a storage location in the computer’s memory (RAM) for


holding a piece of information.
 Type of stored data is depends upon ??
 Variable type aka Data type
 To Use a variable in a program you must write a variable definition (also called a
variable declaration) headed by it Data type
 Names of C++ variables can include letters, numbers, and underscores, but must
begin with a letter or underscore and No spaces or other special characters are
allowed within a C++ variable name
Data Types 24
1. Numeric information : whole numbers and fractional
numbers, negative numbers and positive numbers,
numbers so large, and others so small.
2. Textual information : Names and addresses, for instance,
are stored as groups of characters.

When you write a program you must determine what types of


information it will be likely to encounter
Data Nature Data Type in C Sub Type(Family) Example
Integer short, Int , unsigned int etc 10 20 8 etc
Numeric
Float float , double etc 10.12 1.22
Character char ‘A’ ‘B’ ‘C’ ‘D’
Text
String string “Karachi”
Boolean Bool bool True or False 1 or 0
25
C++ Arithmetic Operators

 Used for performing numeric calculations


 C++ has Variety of operators:
1. Unary (1 operand)
 E.g. - 5
2. Binary (2 operands)
 E.g. 13 + 7
3. Ternary (3 operands)
 E.g. exp1 ? exp2 : exp3
 largest = ((a > b) ? a : b);
26
Making Decisions

 Relational Operators Used to compare numbers to determine relative order


 A Relational expression is one that evaluates as true or false
 2>9 has the value 0
 What does unary operator ! Means ?
 Means not, and essentially reverses the true/false value of an expression
 Based on relation, computer programs can make make decisions
 The if statement
 The switch statement
Some Sample Selection
Statements within a C++ Program
28
Switch Statement

 When you want to create different


outcomes depending on specific
values of a variable, you can use a
series of IFs
 As an alternative to the long string of
IFs , you can use the switch
statement
 The switch can contain any number
of cases in any order
The if Statement

 If the execution of more than one statement depends on the selection, then the
statements must be blocked with curly braces as shown in the code segment in Figure
2-8
30
Logical AND and Logical OR

 To Combine Multiple Conditions


31
Loops

 Loop: a control structure that causes a statement or statements to repeat


1. While Loop
2. Do-While Loop
3. FOR loop
 General format of the while loop:
while (expression)
statement;
statement; can also be a block of statements enclosed in { }
32
Cont..

 General Format for Do-While Loop:


do
statement; // or block in { }
while (expression);

 General Format of FOR loop:


for(initialization; test; update)
statement; // or block in { }
Examples
33
Arrays
34

• Array is a variable that can store multiple values of the same type.
• The variables you have worked with so far are designed to hold only one value at a
time.
• The values are stored together in consecutive memory locations.
• Declared using [] operator:
• int tests[5];
• In this definition
• int is the data type of the array elements
• tests is the name of the array
• 5, in [5], is the size declarator.
• It shows the number of elements in the array.
Accessing Array Elements
35

• The individual elements of an array are assigned unique subscripts.


• These subscripts are used to access the elements.
• Subscripts start at 0
• The last element’s subscript is n-1 where n is the number of
elements in the array.
• n is Size Declarators

• Examples Pgm
36
Functions

 Function is a collection of statements to perform a task


 Function Definitions Include ??
(Program Continues)
Program 6-8 (Continued)
The function call in line 18 passes value1,
value2, and value3 as a arguments to the
function.
 Evaluate the following Expression
1. cout << "Value is " << 20 % (4-2) <<endl ; 40
2. cout << "Value is " << static_cast<char>(66) <<endl ;
3. if(0.1) cout << "Condition is False ";
4. if(10) cout << "Condition is False Again ";
5. if(0) cout << "Now Condition is True “;
 Predict the Output
1. int count=0;
cout<<++count<<endl;
cout<<count++<<endl;
cout<<count<<endl;
2. int num1=10, num2=5, temp;
cout << "Num1 is " << num1 << " Num2 is " << num2 << endl ;
temp=num1;
num1=num2;
num2=temp;
cout << "now Num1 is " << num1 << " and Num2 is " << num2 << endl ;
 Predict the Output
1. int x=6; 41
bool found = true;
if (x==6 && !found )
cout << "CONDITION IS FALSE"<<endl;
 Define the Following
1. What is object-oriented programming language?
2. Why we switched toward object-oriented programming language?
3. What is meant by class and objects?
4. What is Functions and why they are often not consider as reusable unit?
5. Differentiate between compiler or an interpreter?
6. Let int tests[5] is defined & initialized then what will be print by cout << "Value is " << test[5] <<endl
7. What every Function Definitions Includes??

You might also like