Object Oriented Programming in C++
Programming Techniques
* a) Unstructured Programming
* (Assembly language programming)
* b) Procedural Programming
* (Assembly language, C programming)
* c) Object Oriented Programming
* (C++, Java, Smalltalk, C#, Objective C)
Lecture Slides By Adil AslamObject Oriented Programming in C++
Programming Techniques
* Unstructured Programming
* This consists of just writing the sequence of
commands or statements in the main program,
which modifies the state maintained in Global Data.
Example: Assembly Language programs.
* Limitations of Unstructured Programming
* a) The data is global and code operates on it
* b) As the size of code increases, maintenance is a
problem
* c) Does not have independent data for processing
* d) The concept of local variables did not exist
* e) Reusability of code was not supported
Lecture Slides By Adil AslamObject Oriented Programming in C++
Programming Techniques
* Machine language is the language which a Central
Processing Unit (CPU) of a computer understands
and consists only of numbers.
* Written in the form of O& 1
* Any computer can directly understand only its own
machine language, defined by its hardware design.
* 1011000000000101
* 1011000000000001 aie’ a. ane
* 1101110000000111 oe agayk
. ath + oF
* Machine Dependent hae athe
ath RS
om ets *
ABE” ag
5 8 aon TAY
Lecture Slides By Adil AslamObject Oriented Programming in C++
Programming Techniques
* Assembly Language: Similar to machine language,
but provides names for numeric instructions
present in the machine language, making it easy for
the programmer.
* It is a low level language
* Use Naturally understandable symbols called
“Minemonics”
* Example: Load 5, Load 1, Add
* Assemblers are used as interpreter
* It work directly with microprocessor
Lecture Slides By Adil AslamObject Oriented Programming in C++
Programming Techniques
Assembly vs. Machine Language
Assembly Language Machine code
SUB AX , BX 01010101011
MOV CX, AX 01010101010
MOV DX, 0 11010101010
* Assembly language is intermediate between high
level language and machine code.
Lecture Slides By Adil AslamObject Oriented Programming in C++
Procedure Oriented Programming
* It means “a set of procedures” which is a “set of
subroutines” or a “set of functions“.
* functions are called repeatedly in a program to
execute tasks performed by them. For example, a
program may involve collecting data from user
(reading), performing some kind of calculations on
the collected data (calculation), and finally
displaying the result to the user when requested
(printing). All the 3 tasks of reading, calculating and
printing can be written in a program with the help
of 3 different functions which performs these 3
different tasks.
Lecture Slides By Adil AslamObject Oriented Programming in C++
Procedure Oriented Programming
Fs
_ Main Program
l Function 1
Function 4
Function 2
Function 5
i
Function 3 |
Lecture Slides By Adil AslamObject Oriented Programming in C++
A Real-World Example
* Let's say that you are working for a vehicle parts
manufacturer that needs to update it's online
inventory system. Your boss tells you to program
two similar but separate forms for a website, one
form that processes information about cars and
one that does the same for trucks.
* For cars, we will need to record the following
information:
* Color, Engine Size, Transmission Type, Number of doors
*¢ For trucks, the information will be similar, but
slightly different. We need:
* Color, Engine Size, Transmission Type, Cab Size, Towing
Capacity
Lecture Slides By Adil AslamObject Oriented Programming in C++
Scenario 1
* Suppose that we suddenly need to add a bus form,
that records the following information:
Color, Engine Size, Transmission Type,
Number of passengers
* Procedural: We need to recreate the entire form,
repeating the code for Color, Engine Size, and
Transmission Type.
* OOP: We simply extend the vehicle class with a bus
class and add the method/function,
numberOfPassengers.
Lecture Slides By Adil AslamObject Oriented Programming in C++
Scenario 2
* Instead of storing color in a database like we
previously did, for some strange reason our client
wants the color emailed to him.
* Procedural: We change three different forms: cars,
trucks, and buses to email the color to the client
rather than storing it in the database.
* OOP: We change the color method in the vehicle
class and because the car, truck, and bus classes all
extend (or inherit from, to put it another way) the
vehicle class, they are automatically updated.
Lecture Slides By Adil AslamObject Oriented Programming in C++
Scenario 3
* We want to move from a generic car to specific
makes, for example: Nissan and Mazda.
* Procedural: We create a new form for each make,
repeating all of the code for generic car information
and adding the code specific to each make.
* OOP: We extend the car class with a Nissan class
and a Mazda class and add methods for each set of
unique information for that car make.
Lecture Slides By Adil AslamObject Oriented Programming in C++
Scenario 4
¢ We found a bug in the transmission type area of
our form and need to fix it.
* Procedural: We open and update each form.
* OOP: We fix the transmission Type method in the
vehicle class and the change perpetuates in every
class that inherits from it.
Lecture Slides By Adil AslamObject Oriented Programming i
Procedural vs. Object-Oriented Programming
PoP OOP
In POP, program is divided into In OOP, program is divided into
small parts called functions. parts called objects.
POP does not have any proper
way for hiding data so it is less POH Provides Data dig so
provides more security.
secure.
Example of POP are : C, VB, Example of OOP are : C++, JAVA,
FORTRAN, Pascal. [Link], C#.NET.
Lecture Slides By Adil AslamObject Oriented Programming in C++
Procedural vs. Object-Oriented Programming
- a
Procedural: ( Object-Oriented:
© Object focused design
© Code reuse
© Complex design
© Protected data
© Top down design
© Limited code reuse
|
© Complex code
© Global data focused
X
Lecture Slides By Adil AslamObject Oriented Programming in C++
oop
* Object Oriented Methodology is a certain process
through which software can be developed.
* The goals of this methodology are to achieve
Software Systems that are reliable, reusable,
extensible; hence, more useful in the long run.
* The methodology achieves its goals by the help of a
collection of objects that communicate by
exchanging messages.
Lecture Slides By Adil AslamObject Oriented Programming in C++
What is Object-Orientation?
* A technique for system modeling
* OO model consists of several interacting objects
Lecture Slides By Adil AslamObject Oriented Programming in C++
What is a Model?
«A model is an abstraction of something
* Purpose is to understand the product before
developing it
Lecture Slides By Adil AslamObject Oriented Programming in C++
Examples — Model
* Highway maps
* Architectural models
* Mechanical models
Lecture Slides By Adil AslamObject Oriented Programming in C++
Example — OO Model
Lecture Slides By Adil AslamObject Oriented Programming in C++
..Example — OO Model
* Objects
* Ali
* House
* Car
° Tree
¢ Interactions
* Ali lives in the house
* Ali drives the car
Lecture Slides By Adil AslamObject Oriented Programming in C++
Object-Orientation - Advantages
* People think in terms of objects
*00 models map to reality
* Therefore, OO models are
* easy to develop
* easy to understand
Lecture Slides By Adil AslamObject Oriented Programming in C++
oop
* Advantage of Object Oriented Programming
* a) Objects are modeled on real world entities.
* b) This enables modeling complex systems of real
world into manageable software solutions.
* Advantage of OOPS
* It provides data hiding
* It provides data encapsulation
* It provides data abstraction
* It provides reusability of code.
* It provides easy code maintenance
Lecture Slides By Adil AslamIntroduction to Java Programming Language
Object-Oriented Programming
(OOP)
Notes By Adil AslamObject Oriented Programming in C++
oop
* Java is an Object-Oriented Language. As a language
that has the Object-Oriented feature, Java supports
the following fundamental concepts —
* Classes
* Objects
* Message Parsing
* Polymorphism
* Inheritance
* Encapsulation
¢ Abstraction
Lecture Slides By Adil AslamObject Oriented Programming in C++
What is an Object?
An object is
= Something tangible (Ali, Car)
= Something that can be apprehended intellectually
(Time, Date)
Lecture Slides By Adil AslamObject Oriented Programming in C++
... What is an Object?
An object has
= State (attributes)
= Well-defined behaviour (operations)
= Unique identity
Lecture Slides By Adil AslamObject Oriented Programming in C++
Example — Ali is a Tangible Object
* State (attributes)
= Name
=Age
¢ Behaviour (operations)
= Walks
= Eats
* Identity
= His name
Lecture Slides By Adil AslamObject Oriented Programming in C++
Example — Car is a Tangible Object
* State (attributes)
= Color
= Model
* Behaviour (operations)
= Accelerate
= Start Car
= Change Gear
* Identity
= Its registration number
Lecture Slides By Adil AslamObject Oriented Programming in C++
Example — Time is an Object Apprehended
Intellectually
* State (attributes)
= Hours
= Seconds
= Minutes
* Behaviour (operations)
= Set Hours
= Set Seconds
= Set Minutes
* Identity
= Would have a unique ID in the model
Lecture Slides By Adil AslamObject Oriented Programming in C++
Example — Date is an Object Apprehended
Intellectually
* State (attributes)
= Year
= Day
= Month
* behaviour (operations)
=Set Year
=Set Day
=Set Month
* Identity
= Would have a unique ID in the model
Lecture Slides By Adil AslamObject Oriented Programming in C++
OOP in C++
* The prime purpose of C++ programming was to add
object orientation to the C programming language,
which is in itself one of the most powerful
programming languages.
* The core of the pure object-oriented programming
is to create an object, in code, that has certain
properties and methods. While designing C++
modules, we try to see whole world in the form of
objects. For example a car is an object which has
certain properties such as color, number of doors,
and the like. It also has certain methods such as
accelerate, brake, and so on.
Lecture Slides By Adil AslamObject Oriented Programming in C++
Advantages of using OOP
* OOP provides a clear modular structure for
programs which makes it good for
defining abstract data types where implementation
details are hidden and the unit has a clearly defined
interface.
* OOP makes it easy to maintain and modify existing
code as new objects can be created with small
differences to existing ones.
* OOP provides a good framework for code libraries
where supplied software components can be easily
adapted and modified by the programmer.
Lecture Slides By Adil AslamObject Oriented Programming in C++
Object-Oriented Programming in C++
* Objects
* Classes
* Data Abstraction
* Data Encapsulation
* Inheritance
* Polymorphism
* Overloading
* Reusability
Lecture Slides By Adil AslamObject Oriented Programming in C++
Class
* Here we can take Human Being as a class. A class is
a blueprint for any functional entity which defines
its properties and its functions. Like Human Being,
having body parts, and performing various actions.
* When you define a class, you define a blueprint for
an object. This doesn't actually define any data, but
it does define what the class name means, that is,
what an object of the class will consist of and what
operations can be performed on such an object.
Lecture Slides By Adil AslamObject Oriented Programming in C++
Class
* Acollection of objects of a similar type.
* Once a class is defined, any number of objects can
be created which belong to that class.
* Aclass is a blueprint, or prototype, that defines the
variables and the methods common to all objects
of a certain kind.
* Class is user defined data type which is use to bind
data member and member function in single unit.
Lecture Slides By Adil AslamObject Oriented Programming in C++
Objects
* Object is the basic unit of object-oriented
programming.
* Objects are identified by its unique name.
* An object represents a particular instance of a
class. There can be more than one instance of a
class. Each instance of a class can hold its own
relevant data.
Lecture Slides By Adil AslamObject Oriented Programming in C++
Objects
* An instance of a class.
* Aclass must be instantiated into an object before it
can be used in the software.
* Asoftware bundle of related state and behavior.
* More than one instance of the same class can be in
existence at any one time.
Lecture Slides By Adil AslamObject Oriented Programming in C++
Abstraction
* Data Abstraction increases the power of
programming language by creating user defined
data types. Data Abstraction also represents the
needed information in the program without
presenting the details.
* For example, a database system hides certain
details of how data is stored and created and
maintained. Similar way, C++ classes provides
different methods to the outside world without
giving internal detail about those methods and
data.
Lecture Slides By Adil AslamObject Oriented Programming in C++
Encapsulation
* Encapsulation is placing the data and the functions
that work on that data in the same place.
* While working with procedural languages, it is not
always clear which functions work on which
variables but object-oriented programming
provides you framework to place the data and the
relevant functions together in the same object.
Lecture Slides By Adil AslamObject Oriented Programming in C++
Encapsulation
* Storing data and functions in a single unit (class).
* Mechanism that binds together code and data in
manipulates.
* Keeps both safe from outside interference and
misuse.
* Data cannot be accessible to the outside world and
only those functions which are stored in the class
can access it.
Lecture Slides By Adil AslamObject Oriented Programming in C++
Inheritance
* One of the most useful aspects of object-oriented
programming is code reusability.
* As the name suggests Inheritance is the process of
forming a new class from an existing class that is
from the existing class called as base class, new
class is formed called as derived class.
* This is a very important concept of object-oriented
programming since this feature helps to reduce the
code size.
Lecture Slides By Adil AslamObject Oriented Programming in C++
Polymorphism
* The ability to use an operator or function in
different ways in other words giving different
meaning or functions to the operators or functions
is called polymorphism.
* Poly refers to many. That is a single function or an
operator functioning in many ways different upon
the usage is called polymorphism.
Lecture Slides By Adil AslamObject Oriented Programming in C++
Polymorphism
* The ability to take more than one form.
* An operation may exhibit different behaviors in
different instances.
* The behavior depends on the data types used in
the operation.
* Reduce complexity by allowing the same interface
to be used to specify a general class of action.
Lecture Slides By Adil AslamObject Oriented Programming in C++
User Defined Data Types
° Class
« Member of the class:
v Variables
v Functions
* Structure
» Member of Structure:
v Variables
Lecture Slides By Adil AslamObject Oriented Programming in C++
Classes in C++
Lecture Slides By Adil AslamObject Oriented Programming in C++
Object Oriented Programming
* Programmer thinks about and defines the
attributes and behavior of objects.
* Often the objects are modeled after real-world
entities.
* Very different approach than function-based
programming (like C).
Lecture Slides By Adil AslamObject Oriented Programming in C++
Object Oriented Programming
* Object-oriented programming (OOP)
* Encapsulates data (attributes) and functions (behavior)
into packages called classes.
* So, Classes are user-defined (programmer-defined)
types.
* Data (data members)
* Functions (member functions or methods)
* In other words, they are structures + functions
Lecture Slides By Adil AslamObject Oriented Programming in C++
Classes in C++
* Class is an encapsulation of data and coding.
* Classes are an expanded version of structures.
* Structure can only contain multiple variables.
* Classes can contain multiple variables, even more,
classes can also contain functions as class member.
* Variables declared in class are called Data
Members(Attributes).
* Functions declared or defined in class are called
Member Functions.
Lecture Slides By Adil AslamObject Oriented Programming in C++
What is a Class?
* Aclass is a user defined data type.
* The instance of the class are called objects.
* Class is a place where we can define the properties
and functionalities of the objects.
Lecture Slides By Adil AslamObject Oriented Programming in C++
Classes in C++
* Class: Class is a blue print which is containing only
list of variables and method and no memory is
allocated for them. A class is a group of objects that
has common properties.
* Aclass in C++ contains, following properties:
* Data Member
* Method
* Constructor
* Block
* Class and Interface
Lecture Slides By Adil AslamObject Oriented Programming in C++
Class Declaration
* Declaration of class must start with the keyword
class followed by the class name and class
members are declared within braces.
* Syntax of declaring class
class class_name
{
// some data
// some functions
hb
Lecture Slides By Adil AslamObject Oriented Programming in C++
Example of Declaring class
class Employee
{
private:
char Name[25];
int Age;
long Salary;
public:
void GetData();
void PutData();
i
Lecture Slides By Adil AslamObject Oriented Programming in C++
Example of Declaring class
class Employee
{
private:
char Name[25];
int Age;
long Salary;
public:
void GetData();
void PutData();
#
Lecture Slides By Adil AslamObject Oriented Programming in C++
Example of Declaring class
class Employee
ia ae Classis a
private: Keyword
char Name[25];
int Age;
long Salary;
public:
void GetData();
void PutData();
Lecture Slides By Adil AslamObject Oriented Programming in C++
Example of Declaring class
class Employee
{
private:
char Name[25];
int Age;
long Salary;
public:
void GetData();
void PutData();
i
Lecture Slides By Adil AslamObject Oriented Programming in C++
Example of Declaring class
class Employee
{ Starting
private: Class Scope
char Name[25];
int Age;
long Salary;
public:
void GetData();
void PutData();
i
Lecture Slides By Adil AslamObject Oriented Programming in C++
Example of Declaring class
class Employee
{ Access
private: Specifier
char Name[25];
int Age;
long Salary;
public:
void GetData();
void PutData();
i
Lecture Slides By Adil AslamObject Oriented Programming in C++
Example of Declaring class
class Employee
{ a ae]
private: Colon
char Name[25];
int Age;
long Salary;
public:
void GetData();
void PutData();
i
Lecture Slides By Adil AslamObject Oriented Programming in C++
Example of Declaring class
class Employee
{
private:
char Name[25]; a1 PL]
int Age; Members of
long Salary;
(aE
public:
void GetData();
void PutData();
i
Lecture Slides By Adil AslamObject Oriented Programming in C++
Example of Declaring class
class Employee
{
private:
char Name[25];
int Age;
long Salary;
Access
public: Seletelicig
void GetData();
void PutData();
i
Lecture Slides By Adil AslamObject Oriented Programming in C++
Example of Declaring class
class Employee
{
private:
char Name[25];
int Age;
long Salary;
public:
void GetData();
void PutData();
Public Member
functions of
Class
i
Lecture Slides By Adil AslamObject Oriented Programming in C++
Example of Declaring class
class Employee
{
private:
char Name[25];
int Age;
long Salary;
public:
void GetData();
void PutData();
Semicolon
and end of
class Scope
Lecture Slides By Adil AslamObject Oriented Programming in C++
Compiler View of Previous Program
i #include
2 using namespace std;
3 class Employee
4cit
5 private:
6 char Name[25];
z int Age;
8 long Salary;
9
10 public:
a1 void GetData();
12 void PutData();
13 ~ 35
Lecture Slides By Adil AslamObject Oriented Programming in C++
Another Example of Declaring class
classtemp {
private:
int data1;
float data2;
public:
void funcl() {
data1=2; }
float func2(){
data2=3.5;
return data; }
iY
As mentioned, definition of class
starts with keyword class
followed by name of class(temp)
in this case. The body of that
class is inside the curly brackets
and terminated by semicolon at
the end. There are two
keywords: private and public
mentioned inside the body of
class. This class also contain two
private data members and two
public member functions.
Lecture Slides By Adil AslamObject Oriented Programming in C++
Simple Example of Class
class Employee {
public:
int salary; // data member
void sal() //Member function
{
cout<<"Enter Salary: ";
cin>>salary;
cout<<"Salary: "<>regno;
cout<<"Enter the Name:";
cin>>name;
}
Lecture Slides By Adil AslamObject Oriented Programming in C++
Program Example-2
void ShowData()
{
cout<<"\nRegister number:"<>Id;
cout<<"\nEnter Employee Name : ";
cin>>Name;
cout<<"\nEnter Employee Age :";
cin>>Age;
cout<<"\nEnter Employee Salary :";
cin>>Salary;
}
cture Slides By Adil AslObject Oriented Programming a
Example of Defining Member Function Outside class-1
class Employee {
int Id; char Name[25]; int Age; long Salary;
public:
void GetData();
void PutData();
Behe) teieda et
Rare reel)
Operator
}
void Employee :: GetData() { //Statement 1: Defining GetData()
cout<<"\nEnter Employee Id: ";
cin>>Id;
cout<<"\nEnter Employee Name : ";
cin>>Name;
cout<<"\nEnter Employee Age :";
cin>>Age;
cout<<"\nEnter Employee Salary :";
cin>>Salary;
}
cture Slides By Adil AslObject Oriented Programming a
Example of Defining Member Function Outside class-1
class Employee {
int Id; char Name[25]; int Age; long Salary;
public:
void GetData();
void PutData();
//Statement 1 : Defining GetData()
cout<<"\nEnter Employee Id: ";
cin>>Id;
cout<<"\nEnter Employee Name : "; eye eee
cin>>Name; “GetData” is Define
cout<<"\nEnter Employee Age :"; ole au keh
cin>>Age;
cout<<"\nEnter Employee Salary : ";
cin>>Salary;
cture Slides By Adil AslObject Oriented Programming in C++
Example of Defining Member Function Outside class-2
//Statement 2 : Defining PutData()
void Employee :: PutData()
{
cout<<"\n Employee Id : "<>Id;
cout<<"\nEnter Employee Name: ";
cin>>Name;
cout<<"\nEnter Employee Age :";
cin>>Age;
cout<<"\nEnter Employee Salary : ";
cin>>Salary;
}
Lecture Slides By Adil AslamObject Oriented Programming a
Example of Defining Member Function inside class-1
class Employee {
int Id; char Name[25]; int Age; long Salary;
public:
void GetData()
{
cout<<"\nEnter Employee Id: ";
cin>>Id;
cout<<"\nEnter Employee Name: ";
cin>>Name;
cout<<"\nEnter Employee Age :";
cin>>Age;
cout<<"\nEnter Employee Salary : ";
cin>>Salary;
}
Lecture Slides By Adil Aslam
//Statement 1 : Defining GetData()
Merle
Larrea col aM etre (=
the Class BodyObject Oriented Programming a
Example of Defining Member Function inside class-2
cout<<"\n Employee Id : "<