object oriented programming: classes and objects
TRANSCRIPT
Object Oriented Programming: Classes and Objects
Classes and Objects 2
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 attempt to address this problem
Classes and Objects 3
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?
Classes and Objects 4
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 8th Ave city[123]New Haven state[123]CT zip[123]06071 phone[123] 203-765-0128 cell[123]203-879-0932
C++: Classes & Objects - 15
Limitations of Procedural Programming
If the data structures change, many functions must also be changedData 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 problemsdifficult to understand and maintaindifficult to modify and extendeasy to break
Classes and Objects 6
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 7
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 8
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
C++: Classes & Objects - 19
Object Oriented ProgrammingOOP 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
C++: Classes & Objects - 110
Class and Object Example
Bank Account
Frank’s account
Janet’s account
Richard’s account
What do these three accounts
have in common?
Where do they differ?
C++: Classes & Objects - 111
Introduction to ClassesObjects 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;
};
C++: Classes & Objects - 112
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 Classint 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<<Janet.id<<" "<<Janet.name<< <<" "<< Janet.balance<<endl;
return 0;}
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 <iostream>using namespace std;class BankAccount{ public: int id; string name; double balance; };
void printBankAccount(BankAccount);void printAllBankAccounts(BankAccount[]);
int main(){ BankAccount ba[3]; ba[0].id = 1234; ba[0].name= "Frank Smith"; ba[0].balance = 789.23; ba[1].id = 6098; ba[1].name= "Janet Brooks"; ba[1].balance = 3028.56; ba[2].id = 4387; ba[2].name = "Richard Forest"; ba[2].balance = 100.00;
printBankAccount(ba[0]); cout<<endl<<endl; return 0;}
void printBankAccount(BankAccount ba){//FILL IN CODE HEREreturn;}