Programming Language
II
Lecture 5
Introduction to Object Oriented
Programming
Basic Structure of a C Program
// Example of a C Program
# include <stdio.h>
# include <conio.h>
void fun1()
int a = 10;
Void main()
{
cout <<“a value inside main()” << a;
fun1();
return 0;
}
Void fun1()
{
cout << “a value inside fun1” << a;
}
Basic Structure of a C Program (2)
// Example of a C Program Documentation Section
# include <stdio.h> Link Section
# include <conio.h>
void fun1()
Definition Section
int a = 10;
Void main()
Global declaration section
{ Main()
cout <<“a value inside main()” << {
a;
Declaration part
fun1();
Executable part
return 0;
} }
Void fun1() Subprogram section
{ User defined functions
cout << “a value inside fun1” << a;
Funtion1
}
Why Do We Need Object-Oriented
Programming?
Division into Functions
When programs become larger, a single list of instructions becomes unwieldy.
Few programmers can comprehend a program of more than a few hundred statement
unless it is broken down into smaller units.
For this reason the function was adopted
as a way to make programs more comprehensible to their human creators.
A procedural program is divided into functions, and (ideally, at least) each function
has a clearly defined purpose
clearly defined interface to the other functions in the program.
Why Do We Need Object-Oriented
Programming?
Procedural Languages
C is a procedural languages.
That is, each statement in the language tells the computer to do something:
Get some input, add these numbers, divide by six, display that output.
A program in a procedural language is a list of instructions.
For very small programs, no other organizing principle (often called a paradigm) is
needed.
The programmer creates the list of instructions, and the computer carries them out.
Why Do We Need Object-Oriented
Programming?
The idea of breaking a program into functions can be further extended by grouping a number
of functions together into a larger entity called a module
Dividing a program into functions and modules is one of the cornerstones of structured
programming
Problems with Structured Programming
The project is too complex, the schedule slips, more programmers are added, complexity increases,
costs skyrocket, the schedule slips further, and disaster ensues.
Analyzing the reasons for these failures reveals that there are weaknesses in the procedural paradigm
itself.
No matter how well the structured programming approach is implemented, large programs
become excessively complex.
There are two related problems.
First, functions have unrestricted access to global data.
Second, unrelated functions and data, the basis of the procedural paradigm, provide a poor model of
the real world.
Unrestricted
In a procedural program, one written in C for
Access
example, there are two kinds of data
Local data is hidden inside a function, and is
used exclusively by the function.
In the inventory program a display function
might use local data to remember which item it
was displaying.
Local data is closely related to its function and
is safe from modification by other functions.
global data is used when two or more functions
must access the same
Global data can be accessed by any function in
the program.
Unrestricted Access
In a large program, there are many functions
and many global data items.
The problem with the procedural paradigm is
that this leads to an even larger number of
potential connections between functions and
data
This large number of connections causes
problems in several SQL
ways.
Server Management Studio
First, it makes a program’s structure difficult to
conceptualize.
Second, it makes the program difficult to
modify.
A change made in a global data item may
necessitate rewriting all the functions that access
that item.
Real-World Modeling
The arrangement of separate data and functions does a poor job of modeling things in the real world.
In the physical world we deal with objects such as people and cars. Such objects aren’t like data and they
aren’t like functions.
Complex real-world objects have both attributes and behavior.
Attributes
Examples of attributes (sometimes called characteristics) are, for people, eye color and job title; and, for cars,
horsepower and number of doors.
Attributes in the real world are equivalent to data in a program: they have a certain specific values, such as blue (for
eye color) or four (for the number of doors).
Behavior
Behavior is something a real-world object does in response to some stimulus.
If you apply the brakes in a car, it will generally stop.
Behavior is like a function
you call a function to do something (display the inventory, for example) and it does it.
So neither data nor functions, by themselves, model real-world objects effectively.
The Object-Oriented Approach
The fundamental idea behind object-oriented
languages is to combine into a single unit both
data and the functions that operate on that data.
Such a unit is called an object
An object’s functions, called member functions in
C++, typically provide the only way to access its
data.
If you want to read a data item in an object, you call
a member function in the object. It will access the
data and return the value to you.
You can’t access the data directly.
The object structured program
The Object-Oriented Approach
The data is hidden, so it is safe from accidental
alteration.
Data and its functions are said to be encapsulated
into a single entity.
Data encapsulation and data hiding are key terms
in the description of object-oriented languages.
This simplifies writing, debugging, and
maintaining the program.
A C++ program typically consists of a number of
objects, which communicate with each other by
calling one another’s member functions.
The object structured program
OOP: An Approach to
Organization
Keep in mind that object-oriented programming is not primarily
concerned with the details of program operation.
Instead, it deals with the overall organization of the program.
Most individual program statements in C++ are similar to statements in
procedural languages, and many are identical to statements in C.
Indeed, an entire member function in a C++ program may be very
similar to a procedural function in C.
It is only when you look at the larger context that you can determine
whether a statement or a function is part of a procedural C program or
an object-oriented C++ program.
Characteristics of Object-Oriented Languages
The major elements of object-oriented languages
C++ in particular.
Objects
When you approach a programming problem in an object-oriented language
you no longer ask how the problem will be divided into functions, but how it will be
divided into objects.
Thinking in terms of objects, rather than functions, has a surprisingly helpful effect
on how easily programs can be designed.
This results from the close match between objects in the programming sense and
objects in the real world.
Characteristics of Object-Oriented Languages
Classes
In OOP we say that objects are members of classes. What does this mean?
Almost all computer languages have built-in data types.
For instance, a data type int, meaning integer, is predefined in C++ You can declare as
many variables of type int as you need
int day;
int count;
int divisor;
int answer;
Similarly, you can define many objects of the same class
It specifies what data and what functions will be included in objects of that class.
Defining the class doesn’t create any objects, just as the existence of data type int
doesn’t create any variables.
A class is thus a description of a number of similar objects.
Characteristics of Object-Oriented Languages
Inheritance
The idea of classes leads to the idea of inheritance.
The same as the concept of classes divided into subclasses.
An OOP class can become a parent of several subclasses.
In C++ the original class is called the base class;
other classes can be defined that share its characteristics, but add their own as well. These are
called derived classes.
Don’t confuse the relation of objects to classes, on the one hand, with the relation of a
base class to derived classes, on the other.
Objects, which exist in the computer’s memory, each embody the exact characteristics of
their class, which serves as a template.
Derived classes inherit some characteristics from their base class, but add new ones of
their own.
Characteristics of Object-Oriented Languages
Reusability
Once a class has been written, created, and debugged, it can be distributed to other programmers
for use in their own programs.
This is called reusability.
It is similar to the way a library of functions in a procedural language can be incorporated into
different programs.
However, in OOP, the concept of inheritance provides an important extension to the idea of
reusability.
A programmer can take an existing class and, without modifying it, add additional features and
capabilities to it. This is done by deriving a new class from the existing one.
The new class will inherit the capabilities of the old one, but is free to add new features of its own.
For example, you might have written (or purchased from someone else) a class that creates a
menu system, such as that used in Windows or other Graphic User Interfaces (GUIs).
This class works fine, and you don’t want to change it, but you want to add the capability to make
some menu entries flash on and off.
To do this, you simply create a new class that inherits all the capabilities of the existing one but
adds flashing menu entries.
The ease with which existing software can be reused is an important benefit of OOP.
Characteristics of Object-Oriented Languages
Creating New Data Types
One of the benefits of objects is that they give the programmer a convenient way to
construct new data types
For example, given two-dimensional positions (such as x and y coordinates, or latitude
and longitude) in your program.
You would like to express operations on these positional values with normal arithmetic
operations,
such as
position1 = position2 + origin
where the variables position1, position2, and origin each represent a pair of independent
numerical quantities.
By creating a class that incorporates these two values, and declaring position1, position2,
and origin to be objects of this class,
Hence, create a new data type.
Many features of C++ are intended to facilitate the creation of new data types in this
manner.
Characteristics of Object-Oriented Languages
Polymorphism and Overloading
Note that the = (equal) and + (plus) operators, used in the position don’t act the same
way they do in operations on built-in types such as int.
The objects position1 and so on are not predefined in C++, but are programmer-defined
objects of class Position.
How do the = and + operators know how to operate on objects?
we can define new behaviors for these operators.
These operations will be member functions of the Position class.
Using operators or functions in different ways, depending on what they are operating on,
is called polymorphism (one thing with several distinct forms).
When an existing operator, such as + or =, is given the capability to operate on a new
data type, it is said to be overloaded.
Overloading is a kind of polymorphism; it is also an important feature of OOP.