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...
Intro to ClassesChapter 18 AND 19
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
The Real World
How do you look at things in the real world ?
Objects
Look at a carWheelsChassisSteeringDoorsColorModel
The Car as an object
Define it4 Wheels
Metal Chassis
Can move left, right, forward and back
2 Doors
Bright Red Color
BMW Z3
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
Even Simpler:How about representing a balloon?
Define it:
radius we’ll just use this
shape
color
can inflate it
can pop it
radius
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
How would you define it?Balloon
initialize
inflate
pop
display
radius
Data and Functions AssociatedClass NameBalloon
initialize
inflate
pop
display
radius
Data and Functions Associated
Functions
Balloon
initialize
inflate
pop
display
radius
Data and Functions Associated
Attributes
(the data)
Balloon
initialize
inflate
pop
display
radius
A class is a schematic
for a hypothetical balloon
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)
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
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
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
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.
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
You can use the public functions defined for the class
Balloon bal;
bal.initialize(5);
bal.inflate(15);
bal.pop();
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
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
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
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
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();
}
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)
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
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
Classes and Objects
Data type
(int)
x y z
int x, y, z;
Classes and Objects
The Class (Balloon)
bal hotAir weather
Balloon bal, hotAir, weather;
Each object can have its own attributes
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
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
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
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;
};
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();};
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;
};
Don’t you feel a bit more ‘Class’y ?
Classes are fundamental to the Java Programming language
and further programming in C++