Download - Programming Fundamentals
![Page 2: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/2.jpg)
OBJECT-ORIENTED PROGRAMMING CONCEPTS
![Page 3: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/3.jpg)
Key Concepts Classes Objects Inheritance Polymorphism Encapsulation
![Page 4: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/4.jpg)
Classes and Objects A class describes what an object looks like
AttributesBehavior
An object is an instance of a particular class
A class is like the definition for “mouse” in the dictionary.
Object instances are like a dozen of them sitting on desks in the room.
![Page 5: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/5.jpg)
Attributes An object’s attributes are represented by a class’
instance variables (or member variables, or fields) The content of all of an object’s instance variables
define its state
class Door bool opened bool lockedendClass
Each instance of Door can have one of four states
![Page 6: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/6.jpg)
Behaviors An object’s behaviors are represented by its methods
class Door void closeDoor return
void lockDoor return
void openDoor return
void unlockDoor returnendClass
![Page 7: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/7.jpg)
Similar Yet Different Consider two objects
MovieSong
What attributes do they have in common?
What behaviors do they have in common?
What attributes and behaviors are unique to each one?
![Page 8: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/8.jpg)
Inheritance If we consider a Movie and a Song to
both be types of a MediaItem, we can define a MediaItem and say Movie and Song both inherit certain attributes and behaviors from MediaItem
Heirarchy:MediaItem
○ Song○ Movie
![Page 9: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/9.jpg)
Polymorphism We know a MediaItem has behaviors to play
and pause, but the behavior is different depending on whether we are referring to a Movie or Song
Defining play and pause methods on a Movie or Song class is an example of polymorphism
The method call looks the same for both objects, but the specific behavior is differentA MediaItem can take on many (poly) shapes
(morph)
![Page 10: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/10.jpg)
Encapsulation Every class encapsulates internal
information by hiding it from other objects (information hiding)
Hiding an object’s internal information makes it possible to modify the internal structure of an object without affecting other objects that may use it
![Page 11: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/11.jpg)
Defining Classes Every class has a unique name Most classes contain data (fields) Most classes contain methods
A class that only contains simple data values may not define any methods, but this is rare
Our Program classes have always contained methods, but no data – again, this is rare
![Page 12: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/12.jpg)
An Employee Classclass Employee string name
string getName() return name
void setName(string newName) name = newName returnendClass
The class has a unique name
![Page 13: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/13.jpg)
An Employee Classclass Employee string name
string getName() return name
void setName(string newName) name = newName returnendClass
The class contains a field
![Page 14: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/14.jpg)
An Employee Classclass Employee string name
string getName() return name
void setName(string newName) name = newName returnendClass
The class contains methods
![Page 15: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/15.jpg)
A CardStack Classclass CardStack num cards[52]
num draw() num card = the next card in the stack return card
void shuffle() rearrange the cards in the stack returnendClass
![Page 16: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/16.jpg)
A Card Classclass Card num value // 0 – 51
num getRank() return (value % 13) + 1 // 1 – 13
num getSuit() return value / 13 // 0 – 3
bool isFaceCard return getRank() > 10 // true or falseendClass
![Page 17: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/17.jpg)
Declaring Instances Since a class is like a definition in a
dictionary, we need to create an instance of the class to actually do anything
Creating an instance allocates memory for the class variables
![Page 18: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/18.jpg)
Declaring Instances Declaring variables uses the computer’s
memory Declaring class instances works the
same way
int
int
int
stringint
CardStack
. . .int
int
int
int
int
int
int
int
int
int
int
int
int
int
int
int
![Page 19: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/19.jpg)
Declaring Instances Each instance declaration creates a new object
num counter = 0string name = “John Smith”
Employee assistantEmployee managerEmployee president
Employee Employee Employeeint
string
![Page 20: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/20.jpg)
An Employee with More Fields
class Employee int id string name string hireDate
…Methods for the class…endClass
![Page 21: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/21.jpg)
Memory for Each Instance
Employee Employee Employee
int
string string
int
string string
int
string string
int
string
Each instance of Employee has its own allocation of class variables
![Page 22: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/22.jpg)
Using Methods Methods are accessed using a “dot” notation on an
instance of the class
// Create an instance of the Employee classEmployee manager// We now have an employee object.
// Set and get the name for an instance of// the Employee classmanager.setName(“John Smith”)output manager.getName() // John Smith
![Page 23: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/23.jpg)
Using Multiple InstancesEmployee managermanager.setName(“John Smith”)
Employee assistantassistant.setName(“Fred Jones”)
output manager.getName() // John Smithoutput assistant.getName() // Fred Jones
![Page 24: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/24.jpg)
ReviewWhich of the following is the same as
object?
a) classb) fieldc) instanced) category
![Page 25: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/25.jpg)
ReviewWhich of the following is the same as
object?
a) classb) fieldc) instanced) category
![Page 26: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/26.jpg)
Common Types of Methods “Set” methods are used to set fields in the class
class Employee string name
void setName(string newName) name = newName returnendClass
![Page 27: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/27.jpg)
Common Types of Methods “Get” methods are used to retrieve data
from the class
class Employee string name
string getName() return nameendClass
![Page 28: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/28.jpg)
Common Types of Methods “Get” methods may combine fields
class Employee string firstName string lastName
string getFullName() string name = lastName + ", " + firstName return nameendClass
![Page 29: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/29.jpg)
Common Types of Methods “Work” methods may calculate a return value
class Circle num radius
num calcArea() num area = PI * radius * radius return area
num calcCircumference() num circumference = 2 * PI * radius return circumferenceendClass
![Page 30: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/30.jpg)
Public and Private Access We may want to control access to some
fields and methods Fields and methods can be declared as
public or private Public fields and methods can be
accessed from outside the class Private fields and methods can only be
accessed from within the class
![Page 31: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/31.jpg)
Back to the CardStack Which fields and methods do we want to protect?
class CardStack num cards[52]
num draw() num card = the next card in the stack return card
void shuffle() swap random cards in the stack return
void swap(num cardIndex1, num cardIndex2) switch the card at index 1 with the card at index 2 returnendClass
![Page 32: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/32.jpg)
Back to the CardStack We don’t want free access to any card in the stack
class CardStack private num cards[52]
num draw() num card = the next card in the stack return card
void shuffle() swap random cards in the stack return
void swap(num cardIndex1, num cardIndex2) switch the card at index 1 with the card at index 2 returnendClass
![Page 33: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/33.jpg)
Back to the CardStack We do want access to the card on top of the stack
class CardStack private num cards[52]
public num draw() num card = the next card in the stack return card
void shuffle() swap random cards in the stack return
void swap(num cardIndex1, num cardIndex2) switch the card at index 1 with the card at index 2 returnendClass
![Page 34: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/34.jpg)
Back to the CardStack We do want ability to shuffle the stack
class CardStack private num cards[52]
public num draw() num card = the next card in the stack return card
public void shuffle() swap random cards in the stack return
void swap(num cardIndex1, num cardIndex2) switch the card at index 1 with the card at index 2 returnendClass
![Page 35: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/35.jpg)
Back to the CardStack We don’t want the ability to swap any two cards in the stack
class CardStack private num cards[52]
public num draw() num card = the next card in the stack return card
public void shuffle() swap random cards in the stack return
private void swap(num cardIndex1, num cardIndex2) switch the card at index 1 with the card at index 2 returnendClass
![Page 36: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/36.jpg)
Back to the Card Class Which fields and methods do we want to protect?
class Card num value // 0 – 51
num getRank() return (value % 13) + 1 // 1 – 13
num getSuit() return value / 13 // 0 – 3
bool isFaceCard return getRank() > 10 // true or falseendClass
![Page 37: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/37.jpg)
Back to the Card Class We don’t want the card to be changed
class Card private num value // 0 – 51
num getRank() return (value % 13) + 1 // 1 – 13
num getSuit() return value / 13 // 0 – 3
bool isFaceCard return getRank() > 10 // true or falseendClass
![Page 38: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/38.jpg)
Back to the Card Class We do want to access the card’s rank
class Card private num value // 0 – 51
public num getRank() return (value % 13) + 1 // 1 – 13
num getSuit() return value / 13 // 0 – 3
bool isFaceCard return getRank() > 10 // true or falseendClass
![Page 39: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/39.jpg)
Back to the Card Class We do want to access the card’s suit
class Card private num value // 0 – 51
public num getRank() return (value % 13) + 1 // 1 – 13
public num getSuit() return value / 13 // 0 – 3
bool isFaceCard return getRank() > 10 // true or falseendClass
![Page 40: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/40.jpg)
Back to the Card Class We do want to know if it’s a face card
class Card private num value // 0 – 51
public num getRank() return (value % 13) + 1 // 1 – 13
public num getSuit() return value / 13 // 0 – 3
public bool isFaceCard return getRank() > 10 // true or falseendClass
![Page 41: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/41.jpg)
ReviewAssume that a working program contains the
following statements:
Dog myDogmyDog.setName(“Bowser”)
Which of the following do you know?a) setName() is a public method of the Dog classb) setName() accepts a string parameterc) both of the aboved) none of the above
![Page 42: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/42.jpg)
ReviewAssume that a working program contains the
following statements:
Dog myDogmyDog.setName(“Bowser”)
Which of the following do you know?a) setName() is a public method of the Dog classb) setName() accepts a string parameterc) both of the aboved) none of the above
![Page 43: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/43.jpg)
Instance Methods The methods we’ve seen so far are called
instance methods They operate on a particular instance of a
class
// Get the name from a specific instance// of the Employee classEmployee managermanager.setName(newName)output manager.getName()
![Page 44: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/44.jpg)
But which instance? From outside a method we reference an
instance of a class using an identifier The identifier determines the instance
Employee manageroutput manager.getName()
![Page 45: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/45.jpg)
But which instance?class Employee private string name
public string getName() return nameendClass
output manager.getName()output assistant.getName()
The compiler generates object code that knows which instance of name should be used when calling getName()
manager- name
assistant- name
![Page 46: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/46.jpg)
But which instance? What if we want an instance to
reference itself? There’s no identifier defined within the
class and there may be many instances of the class
![Page 47: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/47.jpg)
Use “this” one… From within a method, you can reference the instance that is
“in scope” using the “this” keyword
class Employee private string name
public string getName() return this.name
void setName(string newName) this.name = newName returnendClass
![Page 48: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/48.jpg)
Why do I care? One example…
class Employee private string name
// What if a parameter identifier overlaps // a field identifier? void setName(string name) name = name returnendClass
![Page 49: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/49.jpg)
“this” Resolves Ambiguity One example…
class Employee private string name
// What if a parameter identifier overlaps // a field identifier? void setName(string name) this.name = name returnendClass
![Page 50: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/50.jpg)
You’re right… Why not just avoid using the same identifier?
class Employee private string mName
// What if a parameter identifier overlaps // a field identifier? void setName(string name) mName = name returnendClass
![Page 51: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/51.jpg)
Or… More likely, change the parameter name…
class Employee private string mName
// What if a parameter identifier overlaps // a field identifier? void setName(string inName) mName = inName returnendClass
![Page 52: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/52.jpg)
But… We will see “this” again where it is more
useful. For now, just know it’s there.
![Page 53: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/53.jpg)
Static Methods Instance methods operate on a specific
instance of a class Static methods do not operate on a
specific instance of the classWhich means they cannot modify fields defined
within the class Static methods are typically useful
methods that are closely related to the class, but not associated with a particular instance
![Page 54: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/54.jpg)
ExamplesFrom our C# examples…
Console.ReadLine();Console.WriteLine();
Console is the class name, but we never create an instance of the class
The Console class is used to group together all the functionality for interacting with a console window
![Page 55: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/55.jpg)
Using Objects (Instances) We can pass class instances as method parameters and
return values
class CardStack void add(Card newCard) add a new card to the stack return
Card draw() Card nextCard = find the next card in the stack return nextCardendClass
![Page 56: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/56.jpg)
Using Objects (Instances) But object are passed by reference
Counter hiccupshiccups.reset() // count = 0
useCounter(hiccups)output hiccups.getCount() // count = 1
void useCounter(Counter c) // The variables “c” and “hiccups” refer // to the same object. Changing one changes // the other. c.increment()return
![Page 57: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/57.jpg)
Using Objects (Instances) Objects can be stored in an array
Employee[] newHires = Employee[10]
![Page 58: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/58.jpg)
ReviewAssume that a working program contains the
following statements:
Cat myCatmyCat.setName(“Socks”)
Which of the following do you know?a) myCat is an object of a class named Catb) setName() is a static methodc) both of the aboved) none of the above
![Page 59: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/59.jpg)
ReviewAssume that a working program contains the
following statements:
Cat myCatmyCat.setName(“Socks”)
Which of the following do you know?a) myCat is an object of a class named Catb) setName() is a static methodc) both of the aboved) none of the above
![Page 60: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/60.jpg)
Bringing It All Together Let’s create a Card class and a
CardStack class A Card represent any one of the standard
52 playing cards A CardStack can be shuffled A Card can be drawn from a CardStack A Card can be added to a CardStack We should be able to determine how
many Cards are in a CardStack
![Page 61: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/61.jpg)
Graphical Objects Let’s take a look at GUI programming in
Visual Studio
![Page 62: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/62.jpg)
Class Diagrams Represent the definition of a class Language independent way of
describing the class p. 272 Programming tools can often consume
or generate class diagrams
![Page 63: Programming Fundamentals](https://reader035.vdocuments.us/reader035/viewer/2022062816/568163d1550346895dd518d3/html5/thumbnails/63.jpg)
Summary Principles of object-oriented
programming Using objects Public and private access Instance methods Static methods GUI object Class diagrams