Module : Object Oriented Programming in
C++
Module Code: ITU07217
Laban Gasper
College of Business Education (CBE)
Email :
[email protected] May 5, 2025
Lecture 01
Introduction to C++
C++ is an extension to C Programming language.
It was developed at AT&T Bell Laboratories in the early 1980s
by Bjarne Stroustrup
It is a deviation from traditional procedural languages in the
sense that it follows Object Oriented Programming (OOP)
approach which is quite suitable for managing large and complex
programs.
It can be used to develope operating systems, browsers, games
and so on
cbe
Laban Gasper (CBE) Course instructor (OOP in C++) May 5, 2025 2 / 55
Types of programming Languages
Produre-oriented programming
In procedure-oriented programming, the problem is viewed as a
sequence of things to be executed, such as reading, calculating,
and printing.
Procedure oriented programming basically consist of writing a
list of instruction or actions for the computer to follow and
organizing these instruction into groups known as functions.
Example: COBOL, FORTRAN, and C
cbe
Laban Gasper (CBE) Course instructor (OOP in C++) May 5, 2025 3 / 55
cbe
Laban Gasper (CBE) Course instructor (OOP in C++) May 5, 2025 4 / 55
Cont...
Object-oriented programming
Object-oriented programming is a methodology to design a
program with the help of objects and classes.
The major motivating factor in the invention of object-oriented
approach is to remove some of the flaws in the POP approach.
Languages used in Object Oriented Programming: Java, C++,
Python, Ruby
cbe
Laban Gasper (CBE) Course instructor (OOP in C++) May 5, 2025 5 / 55
Key Differences Summary
Feature Procedure-Oriented (POP) Object-Oriented (OOP)
Focus Functions Objects and Classes
Approach Top-down (Sequential) Bottom-up (Modular)
Data Security Less secure (Global data access) More secure (Encapsulation)
Reusability Limited (Functions) High (Inheritance, Polymorphism)
Basic Concepts of OOP
1 Objects 4 Encapsulation
2 Classes 5 Inheritance
3 Data abstraction 6 Polymorphism
cbe
Laban Gasper (CBE) Course instructor (OOP in C++) May 5, 2025 6 / 55
Object and Class
Object
A real-world entity that has data and action
Here, data represents properties/ attributes and action
represents functions/methods
Example, a person, chair, pen, table, etc
Class
A class is basically user-defined data types that act as a
template for creating objects of the identical type
It represents the common properties and actions (functions) of
an object.
cbe
Laban Gasper (CBE) Course instructor (OOP in C++) May 5, 2025 7 / 55
Class
cbe
Laban Gasper (CBE) Course instructor (OOP in C++) May 5, 2025 8 / 55
Objects
cbe
Laban Gasper (CBE) Course instructor (OOP in C++) May 5, 2025 9 / 55
OOP Principles
Abstraction
It is a fundamental concept in programming that focuses on
hiding complex implementation details while exposing only
the essential features of an object or system
It is when the user only interacts with specific object’s methods
and/or attributes
By hiding complex details from the user, abstraction
consequently reduces complexity
Shows "what" an object does, not "how" it does it
Example: driving a car (you use the steering wheel/brakes
without knowing engine mechanics)
In C++, abstraction is implemented using abstract classes
with pure virtual functions to define interfaces and access
specifiers (public/private/protected) to hide implementation
details, exposing only essential features
cbe
Laban Gasper (CBE) Course instructor (OOP in C++) May 5, 2025 10 / 55
Cont...
cbe
Laban Gasper (CBE) Course instructor (OOP in C++) May 5, 2025 11 / 55
Polymorphism
cbe
Laban Gasper (CBE) Course instructor (OOP in C++) May 5, 2025 12 / 55
Cont...
cbe
Laban Gasper (CBE) Course instructor (OOP in C++) May 5, 2025 13 / 55
cbe
Laban Gasper (CBE) Course instructor (OOP in C++) May 5, 2025 14 / 55
cbe
Laban Gasper (CBE) Course instructor (OOP in C++) May 5, 2025 15 / 55
cbe
Laban Gasper (CBE) Course instructor (OOP in C++) May 5, 2025 16 / 55
cbe
Laban Gasper (CBE) Course instructor (OOP in C++) May 5, 2025 17 / 55
cbe
Laban Gasper (CBE) Course instructor (OOP in C++) May 5, 2025 18 / 55
cbe
Laban Gasper (CBE) Course instructor (OOP in C++) May 5, 2025 19 / 55
cbe
Laban Gasper (CBE) Course instructor (OOP in C++) May 5, 2025 20 / 55
Inheritance
1 What is inheritance?
2 Why do we use inheritance in C++
3 Modes of inheritance
4 Types of inheritance
5 Single inheritance
6 Multiple inheritance
7 Multi-level inheritance
8 Heirarchical inheritance
9 Hybrid inheritance
cbe
Laban Gasper (CBE) Course instructor (OOP in C++) May 5, 2025 21 / 55
cbe
Laban Gasper (CBE) Course instructor (OOP in C++) May 5, 2025 22 / 55
cbe
Laban Gasper (CBE) Course instructor (OOP in C++) May 5, 2025 23 / 55
cbe
Laban Gasper (CBE) Course instructor (OOP in C++) May 5, 2025 24 / 55
cbe
Laban Gasper (CBE) Course instructor (OOP in C++) May 5, 2025 25 / 55
cbe
Laban Gasper (CBE) Course instructor (OOP in C++) May 5, 2025 26 / 55
cbe
Laban Gasper (CBE) Course instructor (OOP in C++) May 5, 2025 27 / 55
cbe
Laban Gasper (CBE) Course instructor (OOP in C++) May 5, 2025 28 / 55
cbe
Laban Gasper (CBE) Course instructor (OOP in C++) May 5, 2025 29 / 55
cbe
Laban Gasper (CBE) Course instructor (OOP in C++) May 5, 2025 30 / 55
Encapsulation
1 What is encapsulation in C++?
2 Why do we need encapsulation?
3 What are access modifiers?
4 Benefits of encapsulation
5 Difference between abstraction and encapsulation
cbe
Laban Gasper (CBE) Course instructor (OOP in C++) May 5, 2025 31 / 55
cbe
Laban Gasper (CBE) Course instructor (OOP in C++) May 5, 2025 32 / 55
cbe
Laban Gasper (CBE) Course instructor (OOP in C++) May 5, 2025 33 / 55
cbe
Laban Gasper (CBE) Course instructor (OOP in C++) May 5, 2025 34 / 55
cbe
Laban Gasper (CBE) Course instructor (OOP in C++) May 5, 2025 35 / 55
cbe
Laban Gasper (CBE) Course instructor (OOP in C++) May 5, 2025 36 / 55
Structure of C++ Program
C++ program structure is divided into various sections namely,
headers, class definition, member functions definitions and main
function
1 C++ Headers
2 Class Definition
3 Member Functions Definition
4 Main Function
cbe
Laban Gasper (CBE) Course instructor (OOP in C++) May 5, 2025 37 / 55
A Sample C++ Program (Without Class)
cbe
Laban Gasper (CBE) Course instructor (OOP in C++) May 5, 2025 38 / 55
Comments
C++ introduces a new comment symbol → // (double slash)
Example: // This is example of C++ comment
Comments start with a double slash symbol and terminate at
the end of the line
The C comment symbols /*, */ are still valid and more suitable
for multi-line comments
/* This is an example of multi-line
C++ comment */
cbe
Laban Gasper (CBE) Course instructor (OOP in C++) May 5, 2025 39 / 55
Tokens in C++
cbe
Laban Gasper (CBE) Course instructor (OOP in C++) May 5, 2025 40 / 55
C++ Keywords
auto double long switch
break else new this
case enum public throw
char extern private try
class float protected catch
const flor return void
continue friend short while
default goto sizeof final
delete if static union
do int struct vitrual
cbe
Laban Gasper (CBE) Course instructor (OOP in C++) May 5, 2025 41 / 55
Identifiers
Identifiers refer to the names of variables, functions, arrays,
classes, etc., created by the programmer.
The following rules are common to both C and C++:
Only alphabetic characters, digits, and underscores are permitted
The name cannot start with a digit
C++ is case sensitive. Uppercase and lowercase letters are
distinct
It should not be a reserved word (keywords)
Constants
Whenever we declare any variable as a constant and assigning a
particular value to it, so we can’t change that value further in a
program
Example: const int size = 10;
cbe
Laban Gasper (CBE) Course instructor (OOP in C++) May 5, 2025 42 / 55
Basic data types
Every program specifies a set of operations to be done on some
data in a particular sequence
The built in or basic data types supported by C++ are integer,
floating point and character type
A brief discussion on these types is given below:
cbe
Laban Gasper (CBE) Course instructor (OOP in C++) May 5, 2025 43 / 55
Operators
Operators are special symbols used for specific purposes. C++
includes many operators
1 Arithmetical operators
2 Relational operators
3 Logical operators
4 Assignment operators
5 Comma operator
cbe
Laban Gasper (CBE) Course instructor (OOP in C++) May 5, 2025 44 / 55
Arithmetic operators
An operator that performs an arithmetic (numeric) operation +,
-, *, / , or %.
The following table shows the arithmetic operators.
cbe
Laban Gasper (CBE) Course instructor (OOP in C++) May 5, 2025 45 / 55
Relational operators
The relational operators are used to test the relation between
two values
A relational expression returns zero when the relation is false and
a non-zero when it is true. The following table shows the
relational operators.
cbe
Laban Gasper (CBE) Course instructor (OOP in C++) May 5, 2025 46 / 55
cbe
Laban Gasper (CBE) Course instructor (OOP in C++) May 5, 2025 47 / 55
Logical operators
The logical operators are used to combine one or more relational
expression
cbe
Laban Gasper (CBE) Course instructor (OOP in C++) May 5, 2025 48 / 55
cbe
Laban Gasper (CBE) Course instructor (OOP in C++) May 5, 2025 49 / 55
Assignment operators
Compond assignment operators
cbe
Laban Gasper (CBE) Course instructor (OOP in C++) May 5, 2025 50 / 55
cbe
Laban Gasper (CBE) Course instructor (OOP in C++) May 5, 2025 51 / 55
Comma operator
cbe
Laban Gasper (CBE) Course instructor (OOP in C++) May 5, 2025 52 / 55
Input & Output operators
Input operator
cin » number1;
The operator » is known as extraction or get from operator
It extracts / takes the value from the keyboard and assigns it to
the variable on its right
Output operator
cout « "C++ is better than C";
The operator » is known as extraction or get from operator
he operator « is called the insertion or put operator
The multiple use of « (insertion operator) in one statement is
called cascading
cout « "C++ is better than C";
cbe
Laban Gasper (CBE) Course instructor (OOP in C++) May 5, 2025 53 / 55
Try
cbe
Laban Gasper (CBE) Course instructor (OOP in C++) May 5, 2025 54 / 55