0% found this document useful (0 votes)
12 views21 pages

Chapter 4 Introduction To OOP

Chapter 4 introduces Object-Oriented Programming (OOP) concepts, focusing on objects, classes, attributes, and operations. It explains how objects encapsulate state and behavior, and how classes serve as templates for creating objects with shared properties. The chapter also discusses key principles such as inheritance and polymorphism, emphasizing the importance of collaboration among objects to solve problems.

Uploaded by

linzskybes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views21 pages

Chapter 4 Introduction To OOP

Chapter 4 introduces Object-Oriented Programming (OOP) concepts, focusing on objects, classes, attributes, and operations. It explains how objects encapsulate state and behavior, and how classes serve as templates for creating objects with shared properties. The chapter also discusses key principles such as inheritance and polymorphism, emphasizing the importance of collaboration among objects to solve problems.

Uploaded by

linzskybes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Chapter 4

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

• An object is an entity with a


well-defined boundary and
identity
– usually a person, place or thing (a
noun)
• An object encapsulates state
and behavior
– State - represented by attributes
and relationships.
• An attribute is a description of
objects in a class
Object
– Behavior - represented by
operations, methods, and state Operations
machines.
• A method is an action performed by an
object (a verb)
An Object Has State
• The state of an object is one of the possible
conditions in which an object may exist.
• The state of an object normally changes over
time.
Example of State

Name: John Name: John


Employee ID: 567138
Employee ID: 567138 HireDate: 07/25/1991
Status: Tenured
Date Hired: July 25, 1991 Discipline: Finance
MaxLoad: 3
Status: Tenured
Discipline: Finance
Maximum Course Load: 3 classes

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.

Mary teaches Biology John teaches Biology


Objects Need to Collaborate

• Objects are useless unless they can


collaborate together to solve a problem.
– Each object is responsible for its own
behavior and status.
– No one object can carry out every
responsibility on its own.
• How do objects interact with each other?
– They interact through messages.
What Is a Class?
• A class is a description of a set of objects that share
the same properties and behavior.
– An object is an instance of a class.
Class: Professor

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

Class name: M. Modano


address: 123 Main
studentID: 9
dateofBirth: 03/10/1967

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;

double calculateVolume(){ // calculate and display the area and volume


return length * breadth * height; cout << "Area of Room = " << room1.calculateArea() << endl;
} cout << "Volume of Room = " << room1.calculateVolume() << endl;
};
return 0;
}

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

• The main function instantiates one objects - room1


• The object room 1 is assigned the data values which are used in the
calculations of area and volume

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

You might also like