Introduction To C++ AND Object Oriented Programming
Introduction To C++ AND Object Oriented Programming
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
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
DCS5088 :: Chapter 1
DCS5088 :: Chapter 1
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
6. Execute
Primary Memory
CPU
. . . . . .
CPU takes each instruction and executes it, possibly storing new data values as the program executes.
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
Function header
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
DCS5088 :: Chapter 1
17
Example: Employee A
Name & Salary
Data object
Code
DCS5088 :: Chapter 1
18
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
DCS5088 :: Chapter 1
22
DCS5088 :: Chapter 1
23
Set of operations
get_name() get_address() get_cgpa()
DCS5088 :: Chapter 1
24
DCS5088 :: Chapter 1
25
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
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
Object Data
DCS5088 :: Chapter 1
33
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()
DCS5088 :: Chapter 1
35
DCS5088 :: Chapter 1
36
DCS5088 :: Chapter 1
37
Data abstraction
length Object Box width depth
Calculate_Volume() Calculate_Area()
DCS5088 :: Chapter 1
38
DCS5088 :: Chapter 1
39
Example: Car
Dont need to know the details of the car in order to drive one
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
DCS5088 :: Chapter 1
44
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
DCS5088 :: Chapter 1
47