You are on page 1of 8

Introduction to object-oriented

programming

The object-oriented paradigm


• What is object-oriented programming?

• What are its benefits?

• How is it different from ”traditional” programming?

• What is its impact on our software development skills?

• How does one become object-oriented?

Two distinct approaches:


• Procedure-oriented programming

• Object-oriented programming

Procedure-oriented programming
• Design some data structure(s) to hold the data

• Write some function(s) to use/change the data

• The main focus is on the functions (procedures)

• Functional decomposition: Thinking about the problem in terms of


the functions; the problem is split into a number of chunks (mod-
ules) each of which can be solved by a function

• Top-down design

1
We use the following terms interchangeably:

• function

• procedure

• subprogram

• routine

Software maintenance: Ripple effect of changes


in functional decomposition
• Main task

– Sub-task 1
∗ Part 1 of Sub-task 1
∗ Part 2 of Sub-task 1
– Sub-task 2
∗ Part 1 of Sub-task 2
∗ Part 2 of Sub-task 2
∗ Part 3 of Sub-task 2
– Sub-task 3
∗ Part 1 of Sub-task 3
∗ Part 2 of Sub-task 3

A change in main ripples through the entire hierarchy of functions.

An example: Bank account


Procedure-oriented approach
Data structure:
(Do not pay much attention to the syntax now.)

2
struct account
{
int account_id;
char* name;
double balance;
/* other details */
};
Functions:
deposit(...);
withdraw(...);
show_balance(...);
/* other functions */

Object-oriented approach
Data and functions put together:
(Do not pay much attention to the syntax now; ”class” will be discussed
in detail later.)
class account
{
int account_id;
char* name;
double balance;
/* other details */

public:
deposit(...);
withdraw(...);
show_balance(...);
/* other functions */
};
Consider two objects: myaccount and youraccount of the account class
account myaccount, youraccount; /* ignore syntax */
Note: account is a class, and we can create any number of account in-
stances, each one of which is an object of the class account.
Operations:
myaccount.deposit(500);
youraccount.withdraw(100);

3
Classes and objects
• The key concept is that of a class and object. A class captures the
common properties of a group of objects. An object is an instance
of a class.

__________________

class name account


__________________

deposit()
methods withdraw()
show_balance()
__________________

data members account_id


name
balance
__________________

The following terms are used interchangeably:

– member function
– method
– message
– operation

And sometimes the following terms are used interchangeably:

– data member
– variable
– field

• In object-oriented programming, the focus is on the data that is


being operated upon and not on the functions that implement these
operations.

4
• In O-O programming, the data and the operations are not treated
as separate entities. The data comes with a set of operations.

• Object-oriented programming is a method of programming in which


programs consist of cooperating objects (objects communicating by
message passing) and the classes are probably related via inheri-
tance. (Inheritance will be discussed later.)

• Functions that want to open a bank account and use it are the
clients. The objects (e.g., myaccount) and classes (e.g., account)
are the servers. This is the so-called client-server model in OOP.

• The public operations (i.e, the interface) are important to a client.


Clients need not know how the operations are implemented. The
private data members can only be accessed from within the member
functions. Clients cannot use the private data members directly.

• We refer to the private data as encapsulated data. This is called


data encapsulation. This results in information hiding. (Hid-
ing from whom?)

INTERFACE | IMPLEMENTATION
| (Client cannot access this part)
|
|
deposit() | account_id
CLIENT ===> withdraw() | name
show_balance() | balance
|
|

The object model


• In procedural programming we talk of calling procedures, e.g., call
procedure f1, or simply, call f1. In OOP, we usually say ”invoke f1
on some object”, e.g., invoke deposit on the object youraccount.

5
• In procedure-oriented programming, by looking at a data structure
it is in general hard to see what it is supposed to do and how it
is supposed to be used. This is because all information required
to use it correctly is buried deep in some collection of functions
elsewhere in the program. In OOP, what a client can do with a
class is clearly stated in the class itself; the client does not have to
look elsewhere.

How is an object created?


• In the simplest case, an object is instantiated as follows:

account my account;
accout my account, his account;

What is contained in an object?


• Each and every instantiated object gets its own copy of the data
members. Data members (except static data members, to be dis-
cussed later) are not shared among objects of a class.

• The code for the member functions is not copied. There is only one
copy of the implementation code of the member functions in one
process (running program or task).

An object is characterized by:


• Identity: This is a unique name.

• State: This is the sum of the properties and values of its data
members.

• Behavior: This is how an object responds (reacts) to messages.


Thus the state is the cumulative result of its behavior. In other
words, the state is a memory of its past activities and influences
future actions.

6
Who alters the state of an object?
• Member functions (note that some member functions may cause a
change of state of the object, some may not)

• Friend functions (to be discussed later)

Introduction to C++ Programming

Our first C++ program


#include <iostream>
using namespace std;

int main()
{

cout << "Welcome to the world of C++\n";

return 0;

Comments in C++
// Illustrating the use of comments

#include <iostream>
using namespace std;

int main()
{

// This is a comment line

// cout << "C++ is fun\n";


cout << "Welcome to the world of C++\n";

7
return 0;
}

Manipulators in C++
// Illustrating the use of endl manipulator
#include <iostream>
using namespace std;

int main()
{

cout << "First line" << endl;

cout << "Second line" << endl << "Third line" << endl;

return 0;

lvalue and rvalue in C++


Try using

int i;
++(++i) and (i++)++ in both C and C++

Defining variables in C++


In C++, variable definitions and executable statements can be mixed.

Scope of variables defined in for in C++


for (int i = 0; i<10; i++)
{
// scope of i is the body of the loop
}

You might also like