You are on page 1of 17

Object oriented Programming

Lecture 2: OOP concepts


Natalia Chaudhry

Reference: Starting out with C++ from Control Structures through Objects by TOny Gaddis 1
Recommended Book
• Object Oriented Programming in C++ by Robert Lafore, 4th Edition, SAMS
Publishing, 2001.

Reference: Starting out with C++ from Control Structures through Objects by TOny Gaddis 2
Model
• Modeling is the process of representing a real-world object
• A model is an abstraction of something
• Metro Bus Service Design
• Abstraction purpose is to understand the product before developing it.
• Examples:
• Highway maps
• Architectural models
• Mechanical models

Reference: Starting out with C++ from Control Structures through Objects by TOny Gaddis 3
Object oriented modeling
• Motivation
• OO is (claimed to be) more ‘natural’. As a system evolves, the functions it
performs need to be changed more often than the objects on which they
operate…
• A model based on objects (rather than functions) will be more stable over time.
Hence the claim that object-oriented designs are more maintainable
• OO model consists of several interacting objects happens in real life.
• OOP (classes and objects)
• Limitations in procedural languages:
• global access makes code complex
• Real world modeling needed

Reference: Starting out with C++ from Control Structures through Objects by TOny Gaddis 4
Objects
• Anything can be an object
• External Entities that interact with the system • Occurrences or Events that occur in the context
being modeled of the system
• E.g. people, devices, other systems • E.g. transfer of resources, a control action, etc.
• Things that are part of the domain being modeled • Roles played by people who interact with the
system
• E.g. reports, displays, signals, etc
• Organizational Units that are relevant to the
application
• Some things cannot be objects: • E.g. division, group, team, etc
• procedures (e.g. print, invert, etc)
• attributes (e.g. blue, 50Mb, etc)

Reference: Starting out with C++ from Control Structures through Objects by TOny Gaddis 5
What are objects?
• Complex real-world objects have both attributes and behavior
• Examples of attributes (sometimes called characteristics)
• Behavior is something a real-world object does (If you apply the brakes in a car, it will
generally stop)
• fundamental idea behind object-oriented languages is to combine into a single unit both
data and the functions that operate on that data. Such a unit is called an object.
• Objects
• Ali lives-in
• House Ali House
• Car
• Tree drives
• Interactions
• Ali lives in the house
• Ali drives the car Car Tree
Reference: Starting out with C++ from Control Structures through Objects by TOny Gaddis 6
Reference: Starting out with C++ from Control Structures through Objects by TOny Gaddis 7
Abstraction
• used to hide the complexity
• One more type of abstraction in C++ can be header files. For example, consider
the pow() method present in math.h header file.
• Simplifies the model by hiding irrelevant details
• Abstraction provides the freedom to defer implementation decisions
• “Capture only those details about an object that are relevant to current
perspective”

Reference: Starting out with C++ from Control Structures through Objects by TOny Gaddis 8
• Ali is a PhD student and teaches BS students
Attributes:
Name Employee ID
Student Roll No Designation
Year of Study Salary
CGPA Age

Engineer’s View

Driver’s View

Reference: Starting out with C++ from Control Structures through Objects by TOny Gaddis 9
Information hiding
Protecting the members of a class from an illegal or unauthorized access
• Suppose you declared a CheckAccount class and you have a data
member balance inside that class
• Ali’s name is stored within his brain
• We can’t access his name directly
• Rather we can ask him to tell his name
================================================
A phone stores several phone numbers
• We can’t read the numbers directly from the SIM card
• Rather phone-set reads this information for us

Reference: Starting out with C++ from Control Structures through Objects by TOny Gaddis 10
Access modifiers
• Used to implement data hiding
• Public (available to everyone, to other classes)
• Private (can be accessed only by the functions inside the class)
• Protected ( inaccessible outside the class but they can be accessed by any
subclass(derived class) of that class)

Reference: Starting out with C++ from Control Structures through Objects by TOny Gaddis 11
Encapsulation
• An idea of bundling data and methods that work on that data within one unit
• Difference from data Hiding:
• data hiding focus more on data security and encapsulation focuses more on
hiding the complexity of the system.
• Difference from data abstraction:
• Abstraction hides details at the design level, while Encapsulation hides details
at the implementation level.
• If a driver of a car wants to change the gear of car, what he needs is to just
change the position of the liver operating gears of the car and it thus changes the
gear of a car

Reference: Starting out with C++ from Control Structures through Objects by TOny Gaddis 12
Classes
• An object is said to be an instance of a class
• variable <--> data type
• A class is thus a description of a number of similar objects
• A class describes a group of objects with
• similar properties (attributes),
• common behaviour (operations),
• common relationships to other objects,
• and common meaning (“semantics”)

Reference: Starting out with C++ from Control Structures through Objects by TOny Gaddis 13
UML
• UML, Unified Modeling Language
• It is a standard notation for the modeling of real-world objects as a first step in
developing an object-oriented program.
• It describes one consistent language for specifying, visualizing, constructing and
documenting the artifacts of software systems.
• Why model?
• Developing a model for a software system before you actually start
programming the software, can be seen as having a blueprint for a large
building you want to build. It is essential to have one.

Reference: Starting out with C++ from Control Structures through Objects by TOny Gaddis 14
UML diagrams are graphs containing nodes connected by paths

Drawing a class

Attributes and behavior


Each object has various attributes. An attribute is a
name/value pair. For example, my age is 22. The attribute
name is "age", the value is "22". Objects also have behavior.
I can stand, walk, sleep etc.
Relationships

Reference: Starting out with C++ from Control Structures through Objects by TOny Gaddis 15
Difference of structs from class
• Class can create a subclass that will inherit parent's properties and methods,
whereas Structure does not support the inheritance.
• A class has all members private by default. A struct is a class where members are
public by default.
• Classes allow to perform cleanup (garbage collector) before object is deallocated
because garbage collector works on heap memory. Objects are usually
deallocated when instance is no longer referenced by other code. Structures can
not be garbage collector so no efficient memory management.
• Classes are still fit for larger or complex objects and Structs are good for small,
isolated model objects.

Reference: Starting out with C++ from Control Structures through Objects by TOny Gaddis 16
That’s it

Reference: Starting out with C++ from Control Structures through Objects by TOny Gaddis 17

You might also like