intro to classes chapter 18 and 19. agenda classes – getting the real world onto the virtual world...

35
Intro to Classes Chapter 18 AND 19

Upload: isabel-fleming

Post on 26-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

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++

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++

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++

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++

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++

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++

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++

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++

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++

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++

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++

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++

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++

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++

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++

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++

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++

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++

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++

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++

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++

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++

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++

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++

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++

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++

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++

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++

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++

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++

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++

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++

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++

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++

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++

Don’t you feel a bit more ‘Class’y ?

Classes are fundamental to the Java Programming language

and further programming in C++