Sie sind auf Seite 1von 47

Chapter 1:

INTRODUCTION TO C++
AND
OBJECT ORIENTED
PROGRAMMING
DCS5088 :: Chapter 1 2
Objectives
At the end of this lecture, students should
be able to :
Understand what is C++.
Know the history of C++.
Understand the concepts of object-oriented
programming (OOP).
Know the advantages of using OOP.
Make comparison between C and C++.
DCS5088 :: Chapter 1 3
1. 1 What is C++?
Procedural
Programming

C/C++
CC++
Object-oriented
programming
DCS5088 :: Chapter 1 4
1.2 Procedural and Object
Oriented Programming
2 ways to think about software development
and program design
C++ is a procedural and also OOP language.
Procedural programming constructs
procedures or functions
collection of programming statements that perform
a specific task.
each procedures contain their own variables and
commonly share variables with other procedures.
DCS5088 :: Chapter 1 5
Procedural Programming is centered on procedure
or function.
PROCEDURE B
Variables
Programming
END OF PROCEDURE B
PROCEDURE A
Variables
Programming
END OF PROCEDURE A
Program
DCS5088 :: Chapter 1 6
Object Oriented Programming is centered on the
object. An object is a programming element that
contains data and the procedures operate on the data.
PROCEDURE B
Variables
Programming
END OF PROCEDURE B
PROCEDURE A
Variables
Programming
END OF PROCEDURE A
Object A
Variables
PROCEDURE B
Variables
Programming
END OF PROCEDURE B
PROCEDURE A
Variables
Programming
END OF PROCEDURE A
Object B
Variables
Program
DCS5088 :: Chapter 1 7
1.3 History of C and C++
History of C
Evolved from two other programming languages
BCPL and B
Typeless languages
Dennis Ritchie (Bell Laboratories)
Added data typing, other features
Development language of UNIX
Hardware independent
Portable programs
1989: ANSI standard
1990: ANSI and ISO standard published
ANSI/ISO 9899: 1990
DCS5088 :: Chapter 1 8
1.3 History of C and C++ (cont)
History of C++
Extension of C
Early 1980s: Bjarne Stroustrup (Bell Laboratories)
Provides capabilities for object-oriented programming
Objects: reusable software components
Model items in real world
Object-oriented programs
Easy to understand, correct and modify
DCS5088 :: Chapter 1 9
1.4 Basics of a Typical C++
Environment
C++ systems
Program-development environment
Language
C++ Standard Library

Phases of C++ Programs:
1. Edit
2. Preprocess
3. Compile
4. Link
5. Load
6. Execute

Loader

Primary
Memory

Program is created in
the editor and stored
on disk.

Preprocessor program
processes the code.

Loader puts program
in memory.

CPU takes each
instruction and
executes it, possibly
storing new data
values as the program
executes.

Compiler

Compiler creates
object code and stores
it on disk.

Linker links the object
code with the libraries,
creates a.out and
stores it on disk

Editor

Preprocessor

Linker



CPU

Primary
Memory

.
.
.

.
.
.

.
.
.

.
.
.

Disk

Disk

Disk

Disk

Disk

DCS5088 :: Chapter 1 11
Example: First C++ Program
This program will display the word Hello!.
//First C++ program
#include <iostream.h>
int main()
{
cout<<"Hello!"<<endl;
return 0;
}
DCS5088 :: Chapter 1 12
Comments
//First C++ program
#include <iostream.h>
int main()
{
cout<<Hello!<<endl;
return 0;
}
DCS5088 :: Chapter 1 13
Pre-processor Directive
//First C++ program
#include <iostream.h>
int main()
{
cout<<Hello!<<endl;
return 0;
}
DCS5088 :: Chapter 1 14
Function header
//First C++ program
#include <iostream.h>
int main()
{
cout<<Hello!<<endl;
return 0;
}
main() body - denoted by braces
DCS5088 :: Chapter 1 15
Display
instruction
//First C++ program
#include <iostream.h>
int main()
{
cout<<Hello!<<endl;
return 0;
}
DCS5088 :: Chapter 1 16
//First C++ program
#include <iostream.h>
int main()
{
cout<<Hello!<<endl;
return 0;
}
Return value of 0.
However, it is not necessary to have this in
every program
DCS5088 :: Chapter 1 17
1.5 Object Oriented Programming
Software design methodology.
Programs defined in terms of objects.
Object consists of data and code
An object can have a set of values
packaged together as a single unit.

DCS5088 :: Chapter 1 18
Example: Employee A
Name &
Salary

Data object
Code
DCS5088 :: Chapter 1 19
1.5 OOP (continued)
Objects can be defined as software
bundles of data and related
methods.
Data = State or Attributes
Methods = Behaviours
DCS5088 :: Chapter 1 20
Example: Employee A
Name : Ali
Salary : 5000

Data Values
describes
the objects
states
DCS5088 :: Chapter 1 21
Example: Employee A
Name : Ali
Salary : 5000

Code to manipulate the
values:
- Display the values
- Modify the values
DCS5088 :: Chapter 1 22
1.6 Class and Structure
Object is implemented in C++ using
class.
Similar to struct type used in C
programming
DCS5088 :: Chapter 1 23
Example : Students record
Suppose there is a need to write a
program to keep the record of students in
a college.
object = Student1.
DCS5088 :: Chapter 1 24
Example: Students record
Set of data values
Student_name
Student_address
Student_cgpa
Set of operations
get_name()
get_address()
get_cgpa()

DCS5088 :: Chapter 1 25
Example: Students record
Using struct in c
struct Student
{ char student_name[30];
char student_address[100];
double student_cgpa;
};
DCS5088 :: Chapter 1 26
Example: Students record
Using class in c++
class Student
{ char student_name[30];
char student_address[100];
double student_cgpa;
void get_name();
void get_address();
void get_cgpa();
};
DCS5088 :: Chapter 1 27
1.7 Basic Concepts of OOP
Classes and Objects
Polymorphism
Inheritance
Data Encapsulation
Data Abstraction
DCS5088 :: Chapter 1 28
1.7.1 Classes
Class is the blueprint of objects.
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.
Able to create variables of class type , also known as
instances or objects of the class type.
DCS5088 :: Chapter 1 29
General format to declare class
class class_name
{
private :
data member variables;
method prototypes();
public :
constructor();
~destructor();
data member variables;
method prototypes();
};
DCS5088 :: Chapter 1 30
Example of a class
class Student
{ string name, status;
int icno, sid;
double marks;

string DetermineStatus()
{
if marks >= 40
status = Pass;
else
status = Fail;
return status;
}
};
Data member
DCS5088 :: Chapter 1 31
Example of a class
class Student
{ string name, status;
int icno, sid;
double marks;

string DetermineStatus()
{
if (marks >= 40)
status = Pass;
else
status = Fail;
return status;
}
};
Method
DCS5088 :: Chapter 1 32
1.7.2 Objects
Object is the term used to explain many
things.
Example: car, bicycle, student, chair and
circle.
An object consists of data and methods
(are shared among objects).
DCS5088 :: Chapter 1 33
Data and method of object: StudentA
Object : StudentA
Data : name, IC number, id, marks,
status
Method : DetermineStatus()
DCS5088 :: Chapter 1 34
1.7.3 Classes and Objects
When you create an object of a class (create
an instance of a class) it gets its own copy of
state initialized with certain values.
through the methods of classes or
constructor
DCS5088 :: Chapter 1 35
class Student

Variables:
string name, status;
int icno, sid;
double marks;


Methods:
M1()
M2()
M3()
Name = Jeff
Status = Pass
icno =
870405016134
Sid = 191131111
marks = 88

M1()
M2()
M3()
Student studentA
DCS5088 :: Chapter 1 36
1.7.4 Data Abstraction
A process to delete all unnecessary
attributes and remain the necessary
attributes to describe an object.
Classes use the concept of abstraction
and are defined as a list of abstract
attributes and functions to operate on
these attributes.
DCS5088 :: Chapter 1 37
1.7.4.1 Abstract Data Types
Since the classes use the concept of
data abstraction, they are known as
Abstract Data Types (ADT).
Object in a program is an abstraction
from a real object (in real world).
DCS5088 :: Chapter 1 38
width

length



depth

Object
Box

Class Box

Characteristics



Behaviors

Length, width, depth

Calculate_Volume()
Calculate_Area()


Data
abstraction

DCS5088 :: Chapter 1 39
1.7.5 Data Encapsulation
Hiding the details of the
implementation of the interface.
It is to reveal as little as possible about
the inner workings of the interface.
DCS5088 :: Chapter 1 40
Example: Car
Dont need to know
the details of the
car in order to drive
one

Keyword :
Interface
(implementation
code)
DCS5088 :: Chapter 1 41
1.7.6 Data Encapsulation
Interface = implementation code
Details of operations are performed
Hidden code
This way, internal coding can be changed
without affecting the interface.
DCS5088 :: Chapter 1 42
1.7.8 Inheritance
Ability to create a new object from an
existing one.
This supports extensibility and
reusability within programs.
Define an object based on another and
create a group of objects that related to
each other in a hierarchical form.
DCS5088 :: Chapter 1 43
Inheritance Hierarchy
BOX


Cake box

Shoes
box


Base
class
Derived
class
Derived
class
DCS5088 :: Chapter 1 44
Concept of Inheritance for class Box.
Box

Atributes:
Length
Width
Depth

Behaviours:
Calculate Volume
Calculate Area


DCS5088 :: Chapter 1 45
Shoes Box and Cake Box
SHOES BOX

Atributes:
Length
Width
Depth
Brand

Behaviours:
Calculate Volume
Calculate Area
Display ShoeBrand

CAKE BOX

Atributes:
Length
Width
Depth
Type of cake

Behaviours:
Calculate Volume
Calculate Area
Display ArtDisplay

DCS5088 :: Chapter 1 46
1.7.9 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.
DCS5088 :: Chapter 1 47
In short, many methods, one interface.
Bicycle





Mountain bike Racing bike Tandems
print (n1, n2) print (n1, n2, n3)
print (n1)
print( )