Chapter 4 Introduction To OOP
Chapter 4 Introduction To OOP
Introduction to OOP
Object Oriented Model
◼ Object-oriented modeling and design is a way of thinking
about problems using models organized around real-
world concepts.
◼ The fundamental construct is the object, which
combines both data structure and behavior in a single
entity - James Rumbaugh
◼ Data Structure (attribute)
◼ <variable <instance field <data <data field
◼ <data member < instance variable <state
◼ Behavior
◼ <method <function <member function <operation <service
◼ <sending a message is equivalent to calling a function
2
What Is an Object?
• Informally, an object represents an entity,
either physical, conceptual, or software.
– Physical entity
Truck
– Conceptual entity
Chemical
Process
– Software entity
Linked List
A More Formal Definition
Attributes
John
An Object Has Behavior
• Behavior determines how an object acts and
reacts.
• The visible behavior of an object is modeled by the
set of messages it can respond to (operations the
object can perform).
John’s behavior
• Submit Final Grades
• Accept Course Offering
• Take Sabbatical
• Maximum Course Load: 3 classes
TakeSabbatical()
John
An Object Has Identity
• Each object has a unique identity, even if the
state is identical to that of another object.
Objects Professor
Attributes - name
- employeeID : UniqueId
- hireDate
- status
Professor Smith - discipline
- maxLoad
+ submitFinalGrade()
Professor Mellon + acceptCourseOffering()
+ setMaxLoad()
Operations + takeSabbatical()
Professor Jones
A Sample Class
Class: Automobile
Data Items: Methods:
– manufacturer’s name – Define data items
– model name (specify
manufacturer’s name,
– year made
model, year, etc.)
– color
– Change a data item
– number of doors (color, engine, etc.)
– size of engine – Display data items
– etc. – Calculate cost
– etc.
The Relationship Between Classes and Objects
• A class is an abstract definition of an object.
– It defines the structure and behavior of each object in the class.
– It serves as a template for creating objects
• Objects are grouped into classes.
• An object is an instance of a class.
Class: Professor
From Real World
Professor
Professor Jones Professor Smith abstracting - name
Objects - employeeID : UniqueId
- hireDate
- status
- discipline
Professor Mellon - maxLoad
instancing + submitFinalGrade()
J Clark : Objects + acceptCourseOffering()
+ setMaxLoad()
Professor + takeSabbatical()
To computer World
What Is an Attribute?
• An attribute is a named property of a class that
describes a range of values instances of the property
may hold.
– A class may have any number of attributes or no attributes at
all.
Student
- name
- address
Attributes
- studentID
- dateOfBirth
Attributes in Classes and Objects
Student Objects
- name
- address
- studentID
- dateOfBirth
name: D. Hatcher
address: 456 Oak
studentID: 2
dateofBirth: 12/11/1969
What Is an Operation?
• An operation is the implementation of a service
that can be requested from any object of the
class to affect behavior.
• A class may have any number of operations or
none at all.
Student
+ get tuition()
+ add schedule()
Operations
+ get schedule()
+ delete schedule()
+ has pre-requisites()
Abstraction
• Abstraction can be defined as:
• Any model that includes the most important,
essential, or distinguishing aspects of something
(emphasizes relevant characteristics)
• while suppressing or ignoring less important,
immaterial, or diversionary details (suppresses
other characteristics)
• The result of removing distinctions so as to emphasize
commonalties.
BriefCase
- Capacity
- Weight
+ open()
+ close()
Designing Classes
• Think in terms of “responsibility”
– What class is responsible for maintaining this data?
– What class is responsible for doing this calculation
or for providing this service (i.e., which class
should define this function)?
• Implement data hiding
– Generally, make data private
– Generally, make functions public
• Access data only through public functions (these are the
class’s public interface)
– Make “helper” functions private 16
Object Oriented Program
• An object-oriented program
is a collection of cooperating
objects
• General, library-style classes
at the bottom should provide
a wide range of functions
• Specific classes at the top
should provide only those
functions needed by the
program 17
Implementing a simple Class
class Room { int main() {
public:
double length; // create object of Room class
double breadth; Room room1;
double height;
// assign values to data members
double calculateArea(){ room1.length = 42.5;
return length * breadth; room1.breadth = 30.8;
} room1.height = 19.2;
18
Implementing a simple Class
• The above program has one Class and the main function
• The class is called Room
– It has three data members
• length
• breadth
• height
– It has two functions (member functions)
• calculateArea
• calculateVolume
19
Inheritance
• Classes are extensible
• You can create new classes that extend or are
descendents of existing classes
• The descendent classes can inherit all the
attributes of the parent class, or they can
override inappropriate attributes
– In geometry, a Cube is a descendent of a Square
– A Cube has all of a Square’s attributes, plus one
additional characteristic: depth
Polymorphism
• Programming modules are sometimes needed to
change the way they operate depending on the
context
• Object-oriented programs use polymorphism to
carry out the same operation in a manner
customized to the object
• Example:
– Without polymorphism, you would have to create
separate module names for a method that cleans a Dish
object, one that cleans a Car object, and one that cleans
a Baby object
– With polymorphism, you create a single “clean” method