Class
Object
Object Oriented
Programming
Objects and Classes
Dr. Khalil Ullah
Department of Software Engineering
khalil.ullah@uom.edu.pk
University of Malkand
Bridging the gap between inspiration and
aspiration 1
Welcome
Object-Oriented Programming
Mon. Tue. Wed., 9:00–10:00 a.m.
Instructor: Dr. Khalil Ullah
Email – Khalil.ullah@uom.edu.pk
Office hours – Thu. and Fri. 9:30–11:00 am.
Contact No. 03480916815.
2
Course Introduction
Required Textbook
H. M. Deitel and P. J. Deitel, C++ How to
Program, Fifth Edition. Upper Saddle
River, NJ: Prentice Hall, 2005. ISBN 0-13-
185757-6
Textbook web page:
http://www.deitel.com/books/cppHTP5/index.html
A recommended book (not required):
Ann R. Ford and Toby J. Teorey, Practical
Debugging in C++. Upper Saddle River, NJ:
Prentice Hall, 2002. ISBN 0-13-065394-2
Object Oriented Programming by Robert
Lafore 3
Course Introduction
How to Succeed
Read the chapter before class
Participate
In class and in the discussions
Learn object-oriented programming
by writing a lot of small programs
To learn C++ programming skills
To apply those skills in problem solving
Start the assignments early
Invest time and work hard!
4
Some brief facts
C++ AND
OBJECT-ORIENTED PROGRAMMING
5
Historical Perspective
C++ and Object-Oriented Programming (Deitel; Randell; Wirth)
1968 – The “software crisis”
NATO Software Engineering Conference in
Garmisch, Germany
Late projects, high costs, code was hard to
maintain
Ad hoc programming
Structured programming
Adapted in the late 1960s to produce
programs that were easy to understand,
develop, test, and modify
6
Structured Programming
C++ and Object-Oriented Programming (Deitel; Wirth)
Programs should be composed of
pieces that have only one starting
point and one terminating point
Use only three kinds of “pieces”
1. Sequence
2. Selection control structures
e.g., if, if/else, switch
3. Repetition control structures
e.g., while, do/while, for 7
C++ and Object-Oriented Programming (Deitel; Eckel)
Object-Oriented Programming
Became widely used in the 1990s
Provided the foundation for real progress in creating
software that is easy to understand, develop, test, and
modify
A way of solving a problem by writing a program
that models the elements in the problem space
as objects
For example, a program used to support a video rental
business would have objects such as a store, videos,
and customers
Programs are collections of objects
Each object can provide a set of services
8
Objects interact by sending messages telling each
Object-Oriented Programming
C++ and Object-Oriented Programming (Deitel)
Some advantages
Easy to understand and develop because the objects
directly model real-world elements
Faster development
Easier to maintain
Object-oriented software is easier to reuse
Object-based programming
Class – program element that contains both data and
the functions that manipulate it
Object –instance of a class (like a “variable”)
Object-oriented programming
Inheritance
Polymorphism 9
Brief Facts About C++
C++ and Object-Oriented Programming (Deitel; Josuttis)
Evolved from C
Designed and implemented by Bjarne
Stroustrup at the Bell Labs in the early
1980s
“C with classes”
Standardized by ISO in 1997
Includes the C++ standard library
Standard Template Library (STL)
Part of the C++ standard library
Readymade classes for data structures and algorithms
10
C++ Features in Chapter 1
INTRODUCTION TO
C++ PROGRAMMING
11
A Simple Example Introduction to C++ Programming (Deitel)
Page 42, Fig. 2.4
C++ features
Comments
Include directive
main()
Statements end with semicolons ;
cout and << for output
std namespace
Escape characters, e.g. \n
12
1 // Fig. 2.4: fig02_04.cpp
2 // A first program in C++. Single-line
3 #include <iostream> Function main returns an comments.
4 integer value.
Left brace { beginsPreprocessor directive to
5 // function main begins program
function body. include
execution
Function input/outputStatements
main appears stream end with a
6 int main() header file <iostream>.
exactly once in every C++ semicolon ;.
7 { program..
8 std::cout << "Welcome to C++!\n";
9 Corresponding right
10 bracethat
return 0; // indicate } ends function
program ended successfully
Stream
body. Name cout insertion
belongs to operator.
11
12 } // end function main namespace std.
Keyword return is one of
Welcome to C++!
several means to exit
function; value 0 indicates
program terminated
successfully.
13
Introduction to C++ Programming (Deitel)
Another Example
Page 43, Fig. 2.5
C++ features
Declaring variables
Input using cin and >>
C++ supports many arithmetic operators
+, -, *, /, %
Precedence, p. 33: parentheses first; then *,/, and %;
and last are + and –. All from left to right.
endl to output a newline and flush output
buffer
<< can be concatenated 14
1 // Fig. 2.5: fig02_05.cpp
2 // Addition program.
3 #include <iostream>
4
5 // function main begins program execution
6 int main()
7 { Declare integer variables.
8 int integer1; // first number to be input by user
9 int integer2; // second number to be input by user
10 int sum;
Use stream extraction
// variable in which sum will be stored
11
operator with standard
12 input stream
std::cout << "Enter first integer\n"; to obtain user
// prompt
13 std::cin >> integer1; input.
// read an integer
14
15 std::cout << "Enter second integer\n"; // prompt
16 std::cin >> integer2; Calculations can be
// read an integer performedStream
in output statements:
manipulator
17 alternative for lines 18 and 20:std::endl outputs a
18 sum = integer1 + integer2; // assign result to sum newline, then “flushes
19 output+buffer.”
std::cout << "Sum is " << integer1 integer2 << std::endl;
20 std::cout << "Sum is " << sum << std::endl; // print sum
21
22 return 0; // indicate that program ended successfully
23 Concatenating, chaining or
24 } // end function main cascading stream insertion
operations.
15
Introduction to C++ Programming (Deitel)
Equality Operators and
Relational Operators
Page 53, Fig. 2.13
C++ features
if
equality operator == (don’t confuse with = )
operator !=
operator <
operator >
operator <=
operator >=
16
What is Object Oriented
Programming?
OOP is a programming paradigm or
programming style centered around
objects rather than functions.
This style of programming is not new
but is followed since 1970s.
Object Oriented Programming is not a
programming language or a tool, but it
is a framework, a style, a way of doing
programming
There are many languages which
support OOP framework 17
Languages
C++
C#
JAVA
Ruby
Python
JavaScript
And many more
18
Many of the new frameworks that are
introduced recently in the market and
are used in web development like
Object Oriented PHP and Angular
are designed using OOPs programming
concepts
OOPs concepts are very Important for a
serious programmer who want to be a
developer or a successful software
engineer in the market
19
Four main pillars of Object Oriented programming
20
Encapsulation
Look first into the procedural
programming
Program
f() x,y
f() x,y,z
f() w,x,y,z
21
Encapsulation
This problem of interdependency of
functions in procedural programing
is
f() now solved in OOPs
Property
x
Method
f()
x
22
23
Procedural programming example
float wage(float bS,int ot,float rate)
{
return bS + ot*rate;
}
int main(int argc, char** argv) {
float baseSalary=20000, rate=20;
int overtime = 10;
float netSalary;
netSalary = wage(baseSalary,overtime,rate)
return 0;
} 24
Solve the same problem using OOP
class employee
{
private: Encapsulation
float baseSalary=10 - process of binding data members
int overTime=20; (variables, properties) and member
float rate=30; functions (methods) together.
public: - Providing different levels of scope on
float wage() how classes, methods, and attributes can
{ be accessed and only allowing access on
return (baseSalary + overTime*rate);
a need-to-know basis. Generally, oop
} uses private, public and protected
};
keywords to implement encapsulation on
attributes, methods and classes. For
example, a private method in a class
int main(int argc, char** argv) {
could only be accessed by the class and
employee e1; a public method could be accessed any
cout<<e1.wage(); other class.
return 0;
25
}
Abstraction
Abstraction is the process of showing
only essential/necessary features of an
entity/object to the outside world and
hide the other irrelevant information.
Simpler Interface
Reduce the
impact of change
26
Inheritance
The mechanism which allows you to
eliminate the redundant code
Consider these HTML elements
HTML Element
Size
Color
Click()
Focus()
27
Polymorphism
Poly means Many and Morph means
form
It is a technique which allows you to get
rid of long if else switch case In Procedural
Language the code
statements. for rendering these
HTML Element elements will be:
For example
Size rendring HTMLswitch(…){
Color Case ‘select’:
elementClick() renderSelect();
Case ‘text’:
Focus()
renderTextBox()
…
…
}
28
In OOP
We implement HTML Element
Render method Size
Color
For each object and it Click()
Focus()
Will behave differently
For each object;
Virtual render(){}
In main it can be called like: textbox.render();
select.render();
29
30
We begin our introduction to object
orientation, a natural way of thinking
about the world and writing computer
programs.
Our goal here is to help you develop an
object-oriented way of thinking and to
introduce you to the Unified Modeling
Language™ (UML™)a graphical language
that allows people who design object-
oriented software systems to use an
industry-standard notation to represent
them. 31
analyze a typical requirements
document that describes a software
system (the ATM) to be built
determine the objects required to
implement that system
determine the attributes the objects will
have
determine the behaviors these objects
will exhibit
specify how the objects interact with
one another to meet the system 32
Let's begin with a simple analogy to help you reinforce your
understanding of classes and their contents.
Suppose you want to drive a car and make it go faster by pressing down
on its accelerator pedal.
What must happen before you can do this?
Well, before you can drive a car, someone has to design it and build it.
A car typically begins as engineering drawings, similar to the blueprints
used to design a house.
These drawings include the design for an accelerator pedal that the
driver will use to make the car go faster.
In a sense, the pedal "hides" the complex mechanisms that actually
make the car go faster, just as the brake pedal "hides" the mechanisms
that slow the car, the steering wheel "hides" the mechanisms that turn
the car and so on.
This enables people with little or no knowledge of how cars are
engineered to drive a car easily, simply by using the accelerator pedal,
the brake pedal, the steering wheel, the transmission shifting
mechanism and other such simple and user-friendly "interfaces" to the
car's complex internal mechanisms. 33