Intro to Classes Chapter 18 AND 19. Agenda Classes – getting the Real World onto the Virtual World...
-
Upload
isabel-fleming -
Category
Documents
-
view
213 -
download
0
Transcript of Intro to Classes Chapter 18 AND 19. Agenda Classes – getting the Real World onto the Virtual World...
![Page 1: Intro to Classes Chapter 18 AND 19. Agenda Classes – getting the Real World onto the Virtual World Defining a Class – Data and functions Our first C++](https://reader035.fdocuments.us/reader035/viewer/2022062717/56649e235503460f94b1188c/html5/thumbnails/1.jpg)
Intro to ClassesChapter 18 AND 19
![Page 2: Intro to Classes Chapter 18 AND 19. Agenda Classes – getting the Real World onto the Virtual World Defining a Class – Data and functions Our first C++](https://reader035.fdocuments.us/reader035/viewer/2022062717/56649e235503460f94b1188c/html5/thumbnails/2.jpg)
Agenda
Classes – getting the Real World onto the Virtual World Defining a Class – Data and functions
Our first C++ Class
Why do we need Classes and Objects?
Summary
![Page 3: Intro to Classes Chapter 18 AND 19. Agenda Classes – getting the Real World onto the Virtual World Defining a Class – Data and functions Our first C++](https://reader035.fdocuments.us/reader035/viewer/2022062717/56649e235503460f94b1188c/html5/thumbnails/3.jpg)
The Real World
How do you look at things in the real world ?
Objects
Look at a carWheelsChassisSteeringDoorsColorModel
![Page 4: Intro to Classes Chapter 18 AND 19. Agenda Classes – getting the Real World onto the Virtual World Defining a Class – Data and functions Our first C++](https://reader035.fdocuments.us/reader035/viewer/2022062717/56649e235503460f94b1188c/html5/thumbnails/4.jpg)
The Car as an object
Define it4 Wheels
Metal Chassis
Can move left, right, forward and back
2 Doors
Bright Red Color
BMW Z3
![Page 5: Intro to Classes Chapter 18 AND 19. Agenda Classes – getting the Real World onto the Virtual World Defining a Class – Data and functions Our first C++](https://reader035.fdocuments.us/reader035/viewer/2022062717/56649e235503460f94b1188c/html5/thumbnails/5.jpg)
The Virtual World
Why make any difference in the Virtual World ?
With C++ Classes and Objects this can be a reality
Solve problems as you visualize them
![Page 6: Intro to Classes Chapter 18 AND 19. Agenda Classes – getting the Real World onto the Virtual World Defining a Class – Data and functions Our first C++](https://reader035.fdocuments.us/reader035/viewer/2022062717/56649e235503460f94b1188c/html5/thumbnails/6.jpg)
Even Simpler:How about representing a balloon?
Define it:
radius we’ll just use this
shape
color
can inflate it
can pop it
radius
![Page 7: Intro to Classes Chapter 18 AND 19. Agenda Classes – getting the Real World onto the Virtual World Defining a Class – Data and functions Our first C++](https://reader035.fdocuments.us/reader035/viewer/2022062717/56649e235503460f94b1188c/html5/thumbnails/7.jpg)
Agenda
Classes – getting the Real World onto the Virtual World
Defining a Class – Data and Functions Our first C++ Class
Why do we need Classes and Objects?
Summary
![Page 8: Intro to Classes Chapter 18 AND 19. Agenda Classes – getting the Real World onto the Virtual World Defining a Class – Data and functions Our first C++](https://reader035.fdocuments.us/reader035/viewer/2022062717/56649e235503460f94b1188c/html5/thumbnails/8.jpg)
How would you define it?Balloon
initialize
inflate
pop
display
radius
![Page 9: Intro to Classes Chapter 18 AND 19. Agenda Classes – getting the Real World onto the Virtual World Defining a Class – Data and functions Our first C++](https://reader035.fdocuments.us/reader035/viewer/2022062717/56649e235503460f94b1188c/html5/thumbnails/9.jpg)
Data and Functions AssociatedClass NameBalloon
initialize
inflate
pop
display
radius
![Page 10: Intro to Classes Chapter 18 AND 19. Agenda Classes – getting the Real World onto the Virtual World Defining a Class – Data and functions Our first C++](https://reader035.fdocuments.us/reader035/viewer/2022062717/56649e235503460f94b1188c/html5/thumbnails/10.jpg)
Data and Functions Associated
Functions
Balloon
initialize
inflate
pop
display
radius
![Page 11: Intro to Classes Chapter 18 AND 19. Agenda Classes – getting the Real World onto the Virtual World Defining a Class – Data and functions Our first C++](https://reader035.fdocuments.us/reader035/viewer/2022062717/56649e235503460f94b1188c/html5/thumbnails/11.jpg)
Data and Functions Associated
Attributes
(the data)
Balloon
initialize
inflate
pop
display
radius
A class is a schematic
for a hypothetical balloon
![Page 12: Intro to Classes Chapter 18 AND 19. Agenda Classes – getting the Real World onto the Virtual World Defining a Class – Data and functions Our first C++](https://reader035.fdocuments.us/reader035/viewer/2022062717/56649e235503460f94b1188c/html5/thumbnails/12.jpg)
An Actual Balloon has attributes
Attribute
(the size of the balloon)
Balloon
initialize
inflate
pop
display
radius = 5
We can use the radius to indicate
whether balloon has been popped
(by setting it to -1)
![Page 13: Intro to Classes Chapter 18 AND 19. Agenda Classes – getting the Real World onto the Virtual World Defining a Class – Data and functions Our first C++](https://reader035.fdocuments.us/reader035/viewer/2022062717/56649e235503460f94b1188c/html5/thumbnails/13.jpg)
Agenda
Classes – getting the Real World onto the Virtual World
Defining a Class – Data and functions
Our first C++ Class Why do we need Classes and Objects?
Summary
![Page 14: Intro to Classes Chapter 18 AND 19. Agenda Classes – getting the Real World onto the Virtual World Defining a Class – Data and functions Our first C++](https://reader035.fdocuments.us/reader035/viewer/2022062717/56649e235503460f94b1188c/html5/thumbnails/14.jpg)
Modeling a Balloon in C++class Balloon
{
public:
void initialize(int initRad);
void inflate(int howMuch);
void pop( );
void display();
private:
int radius;
}; Member Data
Member Functions
![Page 15: Intro to Classes Chapter 18 AND 19. Agenda Classes – getting the Real World onto the Virtual World Defining a Class – Data and functions Our first C++](https://reader035.fdocuments.us/reader035/viewer/2022062717/56649e235503460f94b1188c/html5/thumbnails/15.jpg)
Modeling a Balloon in C++class Balloon
{
public:
void initialize(int initRad);
void inflate(int howMuch);
void pop( );
void display();
private:
int radius;
}; Users of Balloon
can’t access
Users of Balloon
can access
![Page 16: Intro to Classes Chapter 18 AND 19. Agenda Classes – getting the Real World onto the Virtual World Defining a Class – Data and functions Our first C++](https://reader035.fdocuments.us/reader035/viewer/2022062717/56649e235503460f94b1188c/html5/thumbnails/16.jpg)
The C++ Classclass Balloon
{
public:
void initialize(int initRad);
void inflate(int howMuch);
void pop( );
void display();
private:
int radius;
};
A class defines the data and the functions that operate on the data
A class is like defining your own data type, with associated functions which can act on objects of your type.
![Page 17: Intro to Classes Chapter 18 AND 19. Agenda Classes – getting the Real World onto the Virtual World Defining a Class – Data and functions Our first C++](https://reader035.fdocuments.us/reader035/viewer/2022062717/56649e235503460f94b1188c/html5/thumbnails/17.jpg)
Using a class -- objects
When you declare a variable of your new type, it’s called an object
Balloon hotAir;
Balloon bal, weather;
hotAir is an object
Balloon is a class
More objects
![Page 18: Intro to Classes Chapter 18 AND 19. Agenda Classes – getting the Real World onto the Virtual World Defining a Class – Data and functions Our first C++](https://reader035.fdocuments.us/reader035/viewer/2022062717/56649e235503460f94b1188c/html5/thumbnails/18.jpg)
You can use the public functions defined for the class
Balloon bal;
bal.initialize(5);
bal.inflate(15);
bal.pop();
![Page 19: Intro to Classes Chapter 18 AND 19. Agenda Classes – getting the Real World onto the Virtual World Defining a Class – Data and functions Our first C++](https://reader035.fdocuments.us/reader035/viewer/2022062717/56649e235503460f94b1188c/html5/thumbnails/19.jpg)
Classes have Access Control
Unlike struct, you can’t access the data directly (it’s private)
You have to use the functions already defined in the class
Balloon hotAir;
hotAir.radius=10; ILLEGAL
Balloon hotAir;
hotAir.initialize(10); LEGAL
![Page 20: Intro to Classes Chapter 18 AND 19. Agenda Classes – getting the Real World onto the Virtual World Defining a Class – Data and functions Our first C++](https://reader035.fdocuments.us/reader035/viewer/2022062717/56649e235503460f94b1188c/html5/thumbnails/20.jpg)
Why the extra restrictions?
For many objects it’s too dangerous to allow ignorant (or malicious) users the ability to modify the data in an un-authorized mannerLike encasing a complicated device (your iPod) in a protective package—opening package voids the warranty
You can only play, download, select song (functions)
We put “walls” around the object so it acts more thing-like…that’s why the keyword private
![Page 21: Intro to Classes Chapter 18 AND 19. Agenda Classes – getting the Real World onto the Virtual World Defining a Class – Data and functions Our first C++](https://reader035.fdocuments.us/reader035/viewer/2022062717/56649e235503460f94b1188c/html5/thumbnails/21.jpg)
Implement the initialize and inflate functions
void Balloon::initialize(int initRad)
{
radius = initRad;
}
void Balloon::inflate(int howMuch)
{
radius = radius + howMuch;
}
This says it is a member function of Class Balloon
Notice how the parameter modifies
the member data
![Page 22: Intro to Classes Chapter 18 AND 19. Agenda Classes – getting the Real World onto the Virtual World Defining a Class – Data and functions Our first C++](https://reader035.fdocuments.us/reader035/viewer/2022062717/56649e235503460f94b1188c/html5/thumbnails/22.jpg)
Implement the pop and display functions
void Balloon::pop()
{
cout<<"Pop!“<<endl;
radius = -1;
}
void Balloon::display()
{
cout<<"("<<radius<<")"<<endl;
}
A “sentinel” value
Meaning it’s popped
![Page 23: Intro to Classes Chapter 18 AND 19. Agenda Classes – getting the Real World onto the Virtual World Defining a Class – Data and functions Our first C++](https://reader035.fdocuments.us/reader035/viewer/2022062717/56649e235503460f94b1188c/html5/thumbnails/23.jpg)
A ‘client’ program is one that uses a classint main(){
Balloon myBalloon; myBalloon.initialize(3);cout<<"myBalloon currently has Radius ";myBalloon.display();cout<<"\nInflating myBalloon by 8 \n";myBalloon.inflate(8);cout<<"Now myBalloon has Radius ";myBalloon.display();cout<<"\nPopping myBalloon \n";myBalloon.pop();cout<<"myBalloon currently has Radius ";myBalloon.display();
}
![Page 24: Intro to Classes Chapter 18 AND 19. Agenda Classes – getting the Real World onto the Virtual World Defining a Class – Data and functions Our first C++](https://reader035.fdocuments.us/reader035/viewer/2022062717/56649e235503460f94b1188c/html5/thumbnails/24.jpg)
Results when executing previous program:
myBalloon currently has Radius (3)
Inflating myBalloon by 8
Now myBalloon has Radius (11)
Popping myBalloon
Pop!
myBalloon currently has Radius (-1)
![Page 25: Intro to Classes Chapter 18 AND 19. Agenda Classes – getting the Real World onto the Virtual World Defining a Class – Data and functions Our first C++](https://reader035.fdocuments.us/reader035/viewer/2022062717/56649e235503460f94b1188c/html5/thumbnails/25.jpg)
Improvements to Balloon functionsWe can model the balloon better:
If the balloon is already popped, you can’t inflate it If you inflate to a radius over 25, balloon pops
void Balloon::inflate(int howMuch){if (radius >= 0)
radius = radius + howMuch;if (radius > 25)
pop();} Invokes a different member function
![Page 26: Intro to Classes Chapter 18 AND 19. Agenda Classes – getting the Real World onto the Virtual World Defining a Class – Data and functions Our first C++](https://reader035.fdocuments.us/reader035/viewer/2022062717/56649e235503460f94b1188c/html5/thumbnails/26.jpg)
Voila – You have your first class !
Remember – the definition is called a class
An instance of a class is called an objectExample: •int y;• Here int is the type– is analogous to a class•y is the instance of the type and is analogous to
an object
![Page 27: Intro to Classes Chapter 18 AND 19. Agenda Classes – getting the Real World onto the Virtual World Defining a Class – Data and functions Our first C++](https://reader035.fdocuments.us/reader035/viewer/2022062717/56649e235503460f94b1188c/html5/thumbnails/27.jpg)
Classes and Objects
Data type
(int)
x y z
int x, y, z;
![Page 28: Intro to Classes Chapter 18 AND 19. Agenda Classes – getting the Real World onto the Virtual World Defining a Class – Data and functions Our first C++](https://reader035.fdocuments.us/reader035/viewer/2022062717/56649e235503460f94b1188c/html5/thumbnails/28.jpg)
Classes and Objects
The Class (Balloon)
bal hotAir weather
Balloon bal, hotAir, weather;
Each object can have its own attributes
![Page 29: Intro to Classes Chapter 18 AND 19. Agenda Classes – getting the Real World onto the Virtual World Defining a Class – Data and functions Our first C++](https://reader035.fdocuments.us/reader035/viewer/2022062717/56649e235503460f94b1188c/html5/thumbnails/29.jpg)
Agenda
Classes – getting the Real World onto the Virtual World
Defining a Class – Data and functions
Our first C++ Class
Why do we need Classes and Objects?Summary
![Page 30: Intro to Classes Chapter 18 AND 19. Agenda Classes – getting the Real World onto the Virtual World Defining a Class – Data and functions Our first C++](https://reader035.fdocuments.us/reader035/viewer/2022062717/56649e235503460f94b1188c/html5/thumbnails/30.jpg)
Why Classes and Objects ?
It may seem overwhelming or unnecessary at firstAs the complexity/size of your code increases, classes will help you modularize your codeHelps you visualize and solve problems betterBrings more order into your code
![Page 31: Intro to Classes Chapter 18 AND 19. Agenda Classes – getting the Real World onto the Virtual World Defining a Class – Data and functions Our first C++](https://reader035.fdocuments.us/reader035/viewer/2022062717/56649e235503460f94b1188c/html5/thumbnails/31.jpg)
Agenda
Classes – getting the Real World onto the Virtual World
Defining a Class – Data and functions
Our first C++ Class
Why do we need Classes and Objects?
Summary
![Page 32: Intro to Classes Chapter 18 AND 19. Agenda Classes – getting the Real World onto the Virtual World Defining a Class – Data and functions Our first C++](https://reader035.fdocuments.us/reader035/viewer/2022062717/56649e235503460f94b1188c/html5/thumbnails/32.jpg)
The full Balloon class definitionclass Balloon{public:void initialize(int initRad);void inflate(int howMuch); void pop( );void display();void input(); //reads data like (10) from keybdfloat volume(); // returns volume of balloon
private: int radius;
};
![Page 33: Intro to Classes Chapter 18 AND 19. Agenda Classes – getting the Real World onto the Virtual World Defining a Class – Data and functions Our first C++](https://reader035.fdocuments.us/reader035/viewer/2022062717/56649e235503460f94b1188c/html5/thumbnails/33.jpg)
You will also work with a (buggy) Time class
class Time{public: // for public or client use void set(int h, int m, char mrd); void advance(int h, int m); void display();
private: // for internal use int hr, min; // the hour and minute char merid; // meridian: a(m) or p(m) void validate();};
![Page 34: Intro to Classes Chapter 18 AND 19. Agenda Classes – getting the Real World onto the Virtual World Defining a Class – Data and functions Our first C++](https://reader035.fdocuments.us/reader035/viewer/2022062717/56649e235503460f94b1188c/html5/thumbnails/34.jpg)
And complete the implementation of an Accounts class (don’t use on Project 2)
class Account{public: // for public or client use void initialize(string newName, int newID,
int newPIN, float newBalance ); void deposit(float money); void withdraw(int money); void display();
private: // for internal usestring name;int userID, PIN;float balance;
};
![Page 35: Intro to Classes Chapter 18 AND 19. Agenda Classes – getting the Real World onto the Virtual World Defining a Class – Data and functions Our first C++](https://reader035.fdocuments.us/reader035/viewer/2022062717/56649e235503460f94b1188c/html5/thumbnails/35.jpg)
Don’t you feel a bit more ‘Class’y ?
Classes are fundamental to the Java Programming language
and further programming in C++