Transcript Slide 1
Object Oriented Programming: Classes and Objects
What is OOP?
A programming technique first introduced in the 1960s but did not enter mainstream programming until 30 years later Why was it developed?
As computers became mainstream tools in business and science, more and more real-world applications were developed These were more complex than earlier applications and thus the programming was more complex as well (weather prediction, space exploration, war game simulation to name a few) Procedural programming did not easily lend itself to designing complex systems Object-oriented programming was deployed in part as an
2
Procedural Programming
Bank has hundreds of thousands of customers Each customer has lots of information describing itself Name Address Id number Phone number Cell phone number E-mail address History off activities (at least a year’s worth) Deposits Withdrawals Transfers Bill payments etc, etc How have we stored information of this type until now?
How do we define what actions we can do for a customer?
3
Procedural Programming
The information about each individual customer is not in one place An array holds one piece of information in a particular location for a customer and all other arrays hold their information in the same index For example, subscript 123 is the location in each array for a particular customer id[123] 87634WD9 name[123] Fran Best address[123] 657 8 th city[123] New Haven Ave state[123] CT zip[123] 06071 phone[123] 203-765-0128 cell[123] 203-879-0932
Classes and Objects 4
Limitations of Procedural Programming
If the data structures change, many functions must also be changed Data and functions are separate entities As programs become larger and more complex, the separation of a program’s data and the code that operates on the data can lead to problems difficult to understand and maintain difficult to modify and extend easy to break
Limitations of Procedural Programming
Procedural programming works, but it doesn’t fit what happens in the real world Think of a customer’s information as being stored in a folder in a file cabinet. To find out anything about the customer, you should be able to open the customer’s folder and examine all the paperwork. Object oriented programming provides a more “natural” way to simulate the real-world via
classes
and
objects
Classes and Objects 6
Object Oriented Programming
What is a class?
A class is a blueprint of something real. A class describes what type of characteristics or properties something real can have but it itself is not real
Classes and Objects 7
Object oriented Programming
The blueprint of a house tells you the dimensions of the house and locations of the rooms When building the house, the specific materials to use, paint colors, wall paper, floors, etc. are subject to individual taste. This makes each home different from one another.
Each home is an actual implementation or
instance
of the blueprint – each specific house is an
object
of the house
class
Classes and Objects 8
Object Oriented Programming
OOP programming is centered on creating objects from classes An object is a software entity that contains both data and procedures It is a self-contained unit consisting of attributes (data) and procedures (methods or functions) The data is known as the object’s
attributes
The procedures that an object performs are called
member methods
or
functions
Class and Object Example
Bank Account Frank’s account Janet’s account What do these three accounts have in common?
Where do they differ?
Richard’s account
Introduction to Classes
Objects are created from a
class
A class has no memory allocated to it (since it’s not real) while an object does The first letter of the class is capitalized to show it is not a variable name Format:
class ClassName { declaration; declaration; };
Introduction to Classes
By declaring a class, you are actually creating a new data type that you can use as you would int, double, bool, etc.
The class name is used to declare objects of the class The class name can also be used to describe a parameter being passed to a function
Declaring a Class
class BankAccount { public: int id; string name; double balance; };
}
Declaring Objects of the Class
int main() { BankAccount Frank, Janet, Richard; Frank.id = 1234; Frank.name= "Frank Smith"; Frank.balance = 789.23; Janet.id = 6098; Janet.name= "Janet Brooks"; Janet.balance = 3028.56; Richard.id = 4387; Richard.name = "Richard Forest"; Richard.balance = 100.00; cout<
Exercise
Write a function
printBankAccount
that accepts an object of the class
BankAccount
as a parameter and prints the id, name and balance of the account. No value is returned.
Using Objects
We can make an arrays of objects of a class and use them to store information about accounts This gives us the ability to use a loop control to go one by one through the accounts and process them as needed
Exercise
Write a function
printAllBankAccounts
that accepts an array of objects of the class
BankAccount
as a parameter and prints the id, name and balance of all the account. No value is returned.
Starting Code
#include