Computer Programming
[ECEg-1052]
Chapter Eight:
Chapter 8: Introduction to Object
Oriented Programming (OOP)
2018 E.C
Outline
Introduction
Procedure Oriented Vs Object Oriented
Features of OOP-reusability, extensibility
Over view of Classes and Objects
Procedure Oriented Vs Object Oriented
There arc two common programming approach:
1. Procedural programming and
2. Object-oriented programming (or OOP)
3
Cont’d...
Procedural programming is a programming practice centered on the
procedures or actions that take place in a program.
Object-oriented programming is centered around the object.
Objects are created from abstract data types that encapsulate data and
functions together.
In procedural programming the data and the functions are separate
entities.
When programs become larger and more complex, the separation of a
program's data and the code that operates on the data can lead to
problems.
4
Cont’d...
The procedural paradigm.
5
Cont’d...
When the structure of the data changes, the code that operates on the
data must also change to accept the new format.
This results in additional work for programmers and a greater
opportunity for bugs to appear in the code.
This problem has helped influence the shift from procedural
programming to object-oriented programming.
6
Cont’d...
An object is a software entity that contains both data and procedures.
The object is, conceptually, a self-contained unit consisting of attributes
(data) and procedures (functions).
7
Cont’d...
OOP addresses the problems that can result from the separation of code
and data through encapsulation and data hiding.
Encapsulation refers to the combining of data and code into a single
object.
Data hiding refers to an object's ability to hide its data from code that is
outside the object.
8
Basic concepts
Abstraction
Encapsulation or information hiding
Inheritance and
Polymorphism
9
Cont’d...
Abstraction
Abstraction refers to the act of representing essential features without
including the background details or explanations.
Example We can driving a car with out knowing how each system work
We can use object without knowing its internal implementation.
10
Cont’d...
Encapsulation
Encapsulation: wrapping up of data and methods into a single unit
(called class)
Data is not accessible to the outside world and only those methods in
the class can access it.
Insulation of the data from direct access by a program is called data
hiding.
11
Cont’d...
Inheritance
Inheritance is a process by which object of one class acquire the
property of object of other class.
Classes with properties in common can be grouped so that their common
properties are only defined once.
Superclass – inherit its attributes & methods to the subclass(es).
Subclass – can inherit all its superclass attributes & methods besides having
its own unique attributes & methods.
12
Cont’d...
Superclass
Vehicle
Subclasses
Automobile Motorcycle Bus
Sedan Sports Car Luxury Bus School Bus
13
Cont’d...
Polymorphism
The ability to take more than one form
The same word or phrase can mean different things in different contexts
Analogy:
In English, bank can mean side of a river or a place to put money.
14
Program reusability and extensibility
Re-usability – without redundant code
Sharing a certain code within a program
Previously written code can be used again in the new code.
Extensible – possible to alter additional features to the existing class.
Over view of Classes and Objects
A Java class uses variables to define data fields and methods to define
actions.
Additionally, a class provides methods of a special type, known as
constructors, which are invoked to create a new object.
A constructor can perform any action, but constructors are designed to
perform initializing actions, such as
✔ initializing the data fields of objects.
16
Defining Classes and Creating Objects
cin >> [Link];
class DayOfYear {
cout << "Enter the day of the month: ";
public:
cin >> [Link];
void output( );
cout << "Enter your birthday:\n";
int month;
cout << "Enter month as a number: ";
int day;
cin >> [Link];
};
cout << "Enter the day of the month: ";
int main( ){
cin >> [Link];
DayOfYear today, birthday;
cout << "Today's date is ";
cout << "Enter today's date:\n"; [Link]( );
cout << "Enter month as a number: "; cout << "\nYour birthday is ";
cin >> [Link]; [Link]( );}
17
Cont’d...
if ([Link] == [Link] && [Link] == [Link])
cout << "Happy Birthday!\n";else
cout << "Happy Unbirthday!\n";
return 0;}
//Uses iostream:
void DayOfYear::output( ){
switch (month){
case 1:
cout << "January "; break;
case 2:
cout << "February "; break;
18
Cont’d...
case 3: case 9:
cout << "March "; break; cout << "September "; break;
case 4: case 10:
cout << "April "; break; cout << "October "; break;
case 5: case 11:
cout << "May "; break; cout << "November "; break;
case 6: case 12:
cout << "June "; break; cout << "December "; break;
case 7: default:
cout << "July "; break; cout << "Error in DayOfYear::output.";
case 8:
}
cout << "August "; break;
cout << day;}
19
Function
Member Function Definition A member function is defined similar to any
other function except that the Class_Name and the scope resolution
operator, ::, are given in the function heading.
Syntax
Returned_Type Class_Name:: Function_Name(Parameter_List){
Function_Body_Statements
20
Cont’d...
The Dot Operator and the Scope Resolution Operator
Both are used with member names to specify of what thing they are a
member.
For example, suppose you have declared a class called DayOfYear and
you declare an object called today as follows:
DayOfYear today;
[Link]( );
[Link];
void DayOfYear::output( )
21
Thank You !
22