Chapter 1
Chapter 1
LECTURE 1
INTRODUCTION TO C++
AND
OBJECT ORIENTED PROGRAMMING
Objectives
MULTIMEDIA UNIVERSITY 1
DCS 5088 OBJECT ORIENTED PROGRAMMING LECTURE 1
Object-oriented programming
MULTIMEDIA UNIVERSITY 2
DCS 5088 OBJECT ORIENTED PROGRAMMING LECTURE 1
1. Comment
Comment is used to make a program easier be read.
2. Pre-processor directives
A component in C++ compiler will direct the pre-processor directives to insert
the content of iostream.h file into the program
3. Function header
Consists of value, which will be returned (in the example, the value is of type
int).
It is a function, which will be first executed by the compiler.
5. Syntax to direct a program to display the sentence Hello! is located between the
symbol “ “. Keyword endl is used to end the current line and start a new line.
6. Value 0 is returned back to the system as a sign to tell that the program has been
successfully executed. It is not necessary that we should include it in every
program. However, the compiler will display a warning message if there is no
returning value.
MULTIMEDIA UNIVERSITY 3
DCS 5088 OBJECT ORIENTED PROGRAMMING LECTURE 1
Example:
Objects: Customer C1 and Account A1
Customer C1 object may send a message to the account A1 object requesting for the
bank balance.
Message Passing
Object 1 Object 2
MULTIMEDIA UNIVERSITY 4
DCS 5088 OBJECT ORIENTED PROGRAMMING LECTURE 1
Example:
A type of programming where the programmer can define data types and types of
operations, which will be performed on the data structure.
Example:
Data Types of data
Types of operation:
Employee No. : Sort employee list based on Employee No.
Salary : Calculate mark.
By using this method, data structure will become a class such as Employee object,
which consists of data and functions. The programmer can create relationship
between one class with another class such as a class inherits characteristics from
another class. For example, salesperson and Hr manager can inherit the
characteristic from a general class called Employee, which has EmployeeNo and
Salary.
EmpNo.
Salary
Employees
Inherit
Salesperson
HR manager
MULTIMEDIA UNIVERSITY 5
DCS 5088 OBJECT ORIENTED PROGRAMMING LECTURE 1
An object oriented approach identifies the key words or key ideas in the problem and the
relationship between them.
These key words will be objects in the implementation, describing well defined entities
which will communicate with each other to perform different tasks.
Example to calculate the salary for a Salesperson, we need to create a sales person
object, S1 that has the attributes such as salary, sales, name, employee no with a
certain values. We can use the functions/ operations such as calc_salary() that performs
some operations on the attributes.
Example :
Suppose there is a need to write a program to keep the record of students in a college. Then
the object for this program will be Student1.
Example:
MULTIMEDIA UNIVERSITY 6
DCS 5088 OBJECT ORIENTED PROGRAMMING LECTURE 1
(a) Classes
Class is the blueprint of objects.
A set of objects that have similar attributes and methods.
Class is a user defined data type similar to struct type in C programming.
The variables declared within a class are called data members and the functions
defined within a class are called member functions also known as methods.
Declaring variables of a specific class type is called creating instances of that
class type, which also can be referred to as objects.
General format to declare classes:
Class class_name
{
private :
data member variables;
method prototypes();
public :
constructor();
~destructor();
data member variables;
method prototypes();
}
Example :
Example 1 shows the definition of a class Student. Student has attributes such as
name, address, status and others. Same too for the method where it can identify the
status of students whether they have passed or failed.
Example 1
class Student
{
char name[], address[], status[];
int icno, sid; data
double marks;
char DetermineStatus()
{ method
if marks >= 40
status = “Pass”;
else
status = “Fail”;
return status;
}
};
MULTIMEDIA UNIVERSITY 7
DCS 5088 OBJECT ORIENTED PROGRAMMING LECTURE 1
Example 2
Example 2 shows the definition of a class Box. Every box can have the same
attributes that are width, height and depth. Method needed is to calculate the volume
and area of the box.
class Box
{ double width, height, depth; data
double ComputeVolume()
{ return( width * height * depth );}
method
double ComputeArea()
{ return( width * height ); }
};
(b) Objects
Object is the term used to explain many things.
Example: car, bicycle, student, chair and circle.
An object consists of data and method.
Properties of an object are called data. In the real world, characteristics of an
object can be divided into two types:
o Data that can be seen such as a human with two hands.
o Data that cannot be seen such as a human with a name.
Method is a set of function that manipulates data, such as method
DetermineStatus() can determine exam result for object student in Example 1
shown below.
Object : StudentA
Data : name, address, IC number, id, mark, status
Method : DetermineStatus()
Object : Box123
Data : height, depth, width
Method : CalculateVolume()
MULTIMEDIA UNIVERSITY 8
DCS 5088 OBJECT ORIENTED PROGRAMMING LECTURE 1
Variables
Height Height = 178
Weight Weight = 70
Sex Sex = male
name Name = Tom
Methods
M1(…) M1(…)
M2(…) M2(…)
M3(…) M3(…)
Person Andrea
Height = 165
Weight = 51
Sex = female
Name = Andrea
M1(…)
M2(…)
M3(…)
MULTIMEDIA UNIVERSITY 9
DCS 5088 OBJECT ORIENTED PROGRAMMING LECTURE 1
The figure shown below shows the data abstraction concept in OOP for object
Box.
Class Box
Characteristics
Data abstraction
length Object Length, width, depth
Box
Behaviors
depth
width Calculate_Volume()
Calculate_Area()
The Implementation is the code that does all the operations, makes the interface
look as if it is doing something. (cook, fry, boil, roast.)
No part of a complex system should depend on the internal details of another
part.
Encapsulation is a technique for minimizing interdependencies among modules
by defining a strict external interface.
This way, internal coding can be changed without affecting the interface, so long
as the new implementation supports the same (or upwards compatible) external
interface.
MULTIMEDIA UNIVERSITY 10
DCS 5088 OBJECT ORIENTED PROGRAMMING LECTURE 1
(e) Inheritance
Inheritance is the ability to create a new object from an existing one.
This supports extensibility and reusability within programs.
In simple terms, it is the ability to define an object based on another and
create a group of objects that related to each other in a hierarchical form.
When a new object is derived from an existing one, it inherits all properties of the
original object.
But the new object has the freedom to define it’s own members, thus tailoring its
behaviour to specific needs.
The base class (super class) represents the generalized model of objects you are
dealing with.
The derived classes (sub classes) represent specialized objects constructed by
customizing the base class for a specific purpose.
Each derived class shares common characteristics with the class from which it is
derived.
o Example, cats and dogs are subclasses of a superclass called animals
The figure below shows the relationship between one and other classes. Derived
class inherits characteristics of the base class
MULTIMEDIA UNIVERSITY 11
DCS 5088 OBJECT ORIENTED PROGRAMMING LECTURE 1
The figure below shows the concept of Inheritance for class Box.
BOX
Box
Atributes:
Shoes box Cake box
Length
Width
Depth
SHOES BOX CAKE BOX
Behaviours: Atributes: Atributes:
Calculate Volume Length Length
Calculate Area Width Width
Depth Depth
Class Box is a base Brand Type of cake
class
Behaviours: Behaviours:
Calculate Volume Calculate Volume
The bold words are new attributes Calculate Area Calculate Area
and behaviours Display ShoeBrand Display ArtDisplay
Class SHOES BOX and CAKE BOX (derived class) inherit class BOX (base class) where
it has the characteristics and methods from class BOX and add their own characteristics
and methods.
(f) Polymorphism
Polymorphism is the ability of different objects to respond, each in its unique
way, to identical messages and requests.
It has the ability to request the same operations to be performed by a wide range
of different things.
Basically, it means that many different objects can perform the same action.
MULTIMEDIA UNIVERSITY 12
DCS 5088 OBJECT ORIENTED PROGRAMMING LECTURE 1
Example, how students respond to school bell. When the bell rings, it has
different meanings to different students. Some go home, some go for another
class, some go to the library, etc.
Polymorphism simplifies the programming process so that programs can be
written in generic fashion to handle variety of existing and yet to be declared
objects.
In short, “many methods, one interface”.
Bicycle
Consider a method to print all the properties of a bicycle. There will likely be a
different print method for each of the types of bicycles above. By using
polymorphism different methods can be created with the same name but with
different parameters.
The object-oriented program development life cycle consists of three main phases:
1. Object-oriented analysis:
This phase is to determine the system requirements and identify objects and their
relationship with other objects. We can also find out the important attributes of
these objects and determine their behaviour and interaction.
2. Object-oriented design:
This phase is a phase to design classes identified during the analysis phase and
user interface.
Object-oriented analysis and object-oriented design are distinct disciplines, but they
can be intertwined. (See the figure below). For example, you can start with object-
MULTIMEDIA UNIVERSITY 13
DCS 5088 OBJECT ORIENTED PROGRAMMING LECTURE 1
oriented analysis, model it and create an object-oriented design. If there are any
changes or improvements to be made in a phase you can repeat the first phase
(object oriented analysis), follow by the next phase. This process can be repeated
between phases.
Object-oriented analysis
Object-oriented design
Object-oriented programming
(Implementation)
MULTIMEDIA UNIVERSITY 14
DCS 5088 OBJECT ORIENTED PROGRAMMING LECTURE 1
#include<iostream.h>
#include<string.h>
class Staff
{
protected:
char name[30], ic[20];
int age;
public:
void set_data(char *a ,char *b, int c) Data abstraction
{ concept
strcpy(name,a);
strcpy(ic,b);
age=c;
}
void show_data() Polymorphism concept
{
cout<<"This is a base class";
}
};
public:
void set_position(char * a)
{
strcpy(position,a);
}
void main()
{
char name[30],ic[20], position[20];
int age;
Executive Ex;
Ex.set_data(name,ic,age);
Ex.set_position(position);
Ex.show_data();
}
MULTIMEDIA UNIVERSITY 15
DCS 5088 OBJECT ORIENTED PROGRAMMING LECTURE 1
Object-oriented programming offers many advantages. Among the advantages are it can
solve program problems and also increase productivity. Below are advantages of object-
oriented programming:
Using object-oriented concept, it will assist the programmer in creating a module because
data used represents real situations.
Thus, difficulties in developing software can be reduced.
MULTIMEDIA UNIVERSITY 16
DCS 5088 OBJECT ORIENTED PROGRAMMING LECTURE 1
MULTIMEDIA UNIVERSITY 17
DCS 5088 OBJECT ORIENTED PROGRAMMING LECTURE 1
Data (e.g: name, age) will be passed to Encapsulation is used to package data and
other functions (e.g: print, display, add method together. It identifies the functions,
and delete) to be manipulated. The which will act on each object. The class will
function in which will manipulate the control any operations on data and function
data is not defined yet. inside it.
Main() function can access all data and There are access controls for data in a
functions in a program. class. For example: keyword private and
protected. To access private data it must be
done through method or encapsulation
mechanism.
References:
1. Gary Bronson, Object-Oriented Program Development Using C++: A Class-Centered
Approach, Thomson Course Technology, 2005
2. Richard Johnsonbaugh, Martin Kalin, Object-oriented programming in C++, 2/e,
Prentice Hall, 2000
MULTIMEDIA UNIVERSITY 18