You are on page 1of 47

Chapter 1:

INTRODUCTION TO C++ AND OBJECT ORIENTED PROGRAMMING

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

1. 1 What is C++?

CC++ C

Procedural Programming

Object-oriented programming

DCS5088 :: Chapter 1

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 4

Procedural Programming is centered on procedure or function. Program


PROCEDURE A Variables Programming END OF PROCEDURE A PROCEDURE B Variables Programming END OF PROCEDURE B

DCS5088 :: Chapter 1

Object Oriented Programming is centered on the object. An object is a programming element that contains data and the procedures operate on the data.
Program
Object A
Variables PROCEDURE A Variables Programming END OF PROCEDURE A PROCEDURE B Variables Programming END OF PROCEDURE B

Object B
Variables PROCEDURE A Variables Programming END OF PROCEDURE A PROCEDURE B Variables Programming END OF PROCEDURE B

DCS5088 :: Chapter 1

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 7

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

1.4 Basics of a Typical C++ Environment


C++ systems
Program-development environment Language C++ Standard Library

DCS5088 :: Chapter 1

Phases of C++ Programs: 1. Edit 2. Preprocess 3. Compile

Editor

Disk

Program is created in the editor and stored on disk. Preprocessor program processes the code. 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

Preprocessor

Disk

Compiler

Disk

Linker

Disk
Primary Memory

4. Link
5. Load

Loader

Disk

Loader puts program in memory.


. . . . . .

6. Execute

Primary Memory

CPU

. . . . . .

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

Example: First C++ Program


This program will display the sentence Hello. //First C++ program #include <iostream.h> int main() { cout<<"Hello! "<<endl; return 0; }

DCS5088 :: Chapter 1

11

Comments
//First C++ program #include <iostream.h> int main() { cout<<Hello!<<endl; return 0; }

DCS5088 :: Chapter 1

12

Pre-processor Directive
//First C++ program #include <iostream.h> int main() { cout<<Hello!<<endl; return 0; }

DCS5088 :: Chapter 1

13

//First C++ program #include <iostream.h> int main() { cout<<Hello!<<endl; return 0; }

Function header

main() body - denoted by braces


DCS5088 :: Chapter 1 14

//First C++ program #include <iostream.h> int main() { cout<<Hello!<<endl; return 0; }

Display instruction

DCS5088 :: Chapter 1

15

//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

16

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

17

Example: Employee A
Name & Salary

Data object

Code

DCS5088 :: Chapter 1

18

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

19

Example: Employee A
Name : Ali Salary : 5000 Data Values describes the objects states

DCS5088 :: Chapter 1

20

Example: Employee A
Name : Ali Salary : 5000

Code to manipulate the values: - Display the values - Modify the values

DCS5088 :: Chapter 1

21

1.6 Class and Structure


Object is implemented in C++ using class. Similar to struct type used in C programming

DCS5088 :: Chapter 1

22

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

23

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

24

Example: Students record


Using struct in c
struct Student { char student_name[30]; char student_address[100]; double student_cgpa; };

DCS5088 :: Chapter 1

25

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 26

1.7 Basic Concepts of OOP


Classes and Objects
Data Abstraction Data Encapsulation Inheritance Polymorphism
DCS5088 :: Chapter 1 27

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

28

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 29

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

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; }

Method

};

DCS5088 :: Chapter 1

31

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

32

Data and method of object: StudentA

Object Data

: StudentA : name, IC number, id, marks, status Method : DetermineStatus()

DCS5088 :: Chapter 1

33

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

34

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

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

Methods: M1() M2() M3()

DCS5088 :: Chapter 1

35

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

36

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

37

Data abstraction
length Object Box width depth

Class Box Characteristics Length, width, depth Behaviors

Calculate_Volume() Calculate_Area()

DCS5088 :: Chapter 1

38

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

39

Example: Car
Dont need to know the details of the car in order to drive one

Keyword : Interface (implementation code)


DCS5088 :: Chapter 1 40

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

41

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 42

Inheritance Hierarchy
BOX Base class

Shoes box

Cake box

Derived class
DCS5088 :: Chapter 1

Derived class
43

Concept of Inheritance for class Box.


Box
Atributes: Length Width Depth Behaviours: Calculate Volume Calculate Area

DCS5088 :: Chapter 1

44

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

45

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 46

In short, many methods, one interface.


print( )
Bicycle

Mountain bike print (n1, n2)

Racing bike print (n1, n2, n3)

Tandems print (n1)

DCS5088 :: Chapter 1

47

You might also like