41
Chapter 2 introduces Chapter 2 introduces Object Oriented Object Oriented Programming. Programming. OOP is a relatively new OOP is a relatively new approach to programming approach to programming which supports the which supports the creation of new data creation of new data types and operations to types and operations to manipulate those types. manipulate those types. This presentation This presentation introduces OOP. introduces OOP. Object Oriented Programming Data Structures Data Structures and Other Objects and Other Objects Using C++ Using C++

PowerPoint for Chapter 2: Object-Oriented Programming

Embed Size (px)

Citation preview

Page 1: PowerPoint for Chapter 2: Object-Oriented Programming

Chapter 2 introduces Object Chapter 2 introduces Object Oriented Programming.Oriented Programming.

OOP is a relatively new OOP is a relatively new approach to programming approach to programming which supports the creation which supports the creation of new data types and of new data types and operations to manipulate operations to manipulate those types.those types.

This presentation introduces This presentation introduces OOP.OOP.

Object OrientedProgramming

Data StructuresData Structuresand Other Objectsand Other ObjectsUsing C++Using C++

Page 2: PowerPoint for Chapter 2: Object-Oriented Programming

What is this Object ?

There is no real There is no real answer to the question, answer to the question, but we’ll call it a but we’ll call it a “thinking cap”.“thinking cap”.

The plan is to describe The plan is to describe a thinking cap by a thinking cap by telling you what telling you what actions can be done to actions can be done to it.it.

Page 3: PowerPoint for Chapter 2: Object-Oriented Programming

Using the Object’s Slots

You may put a piece of You may put a piece of paper in each of the two paper in each of the two slots (green and red), with a slots (green and red), with a sentence written on each.sentence written on each.

You may push the green You may push the green button and the thinking cap button and the thinking cap will speak the sentence from will speak the sentence from the green slot’s paper.the green slot’s paper.

And same for the red And same for the red button.button.

Page 4: PowerPoint for Chapter 2: Object-Oriented Programming

Example

Page 5: PowerPoint for Chapter 2: Object-Oriented Programming

Example

That test was a breeze !

Page 6: PowerPoint for Chapter 2: Object-Oriented Programming

Example

I shouldstudy harder !

Page 7: PowerPoint for Chapter 2: Object-Oriented Programming

Thinking Cap Implementation

We can implement the We can implement the thinking cap using a thinking cap using a data type called a data type called a classclass..

class thinking_cap {

. . .

};

Page 8: PowerPoint for Chapter 2: Object-Oriented Programming

Thinking Cap Implementation

The class will have The class will have two components called two components called green_stringgreen_string and and red_stringred_string. These . These compnents are strings compnents are strings which hold the which hold the information that is information that is placed in the two slots.placed in the two slots.

Using a class permits Using a class permits two new features . . .two new features . . .

class thinking_cap { . . . char green_string[50]; char red_string[50]; };

Page 9: PowerPoint for Chapter 2: Object-Oriented Programming

Thinking Cap Implementation

The two components The two components will be will be private private member variablesmember variables. . This ensures that This ensures that nobody can directly nobody can directly access this access this information. The information. The only access is through only access is through functions that we functions that we provide for the class.provide for the class.

class thinking_cap { . . .private: char green_string[50]; char red_string[50];};

Page 10: PowerPoint for Chapter 2: Object-Oriented Programming

Thinking Cap Implementation

In a class, the In a class, the functions which functions which manipulate the class manipulate the class are also listed.are also listed.

class thinking_cap {public: . . .private: char green_string[50]; char red_string[50];};Prototypes for the

thinking cap functions go here,after the word public:

Page 11: PowerPoint for Chapter 2: Object-Oriented Programming

Thinking Cap Implementation

In a class, the In a class, the functions which functions which manipulate the class manipulate the class are also listed.are also listed.

class thinking_cap {public: . . .private: char green_string[50]; char red_string[50];};Prototypes for the

thinking cap member functions go here

Page 12: PowerPoint for Chapter 2: Object-Oriented Programming

Thinking Cap Implementation

class thinking_cap {public: void slots(char new_green[ ], char new_red[ ]); void push_green( ) const; void push_red( ) const;private: char green_string[50]; char red_string[50];};

Our thinking cap has at least three member functions:

Function

bodies

will be e

lsewhere

.

Page 13: PowerPoint for Chapter 2: Object-Oriented Programming

Thinking Cap Implementation

class thinking_cap {public: void slots(char new_green[ ], char new_red[ ]); void push_green( ) const; void push_red( ) const;private: char green_string[50]; char red_string[50];};

The keyword const appears after two prototypes:

This means that thesefunctions will not changethe data stored in athinking_cap.

Page 14: PowerPoint for Chapter 2: Object-Oriented Programming

Files for the Thinking Cap

The thinking_cap class The thinking_cap class definition, which we have definition, which we have just seen, is placed with just seen, is placed with documentation in a file called documentation in a file called thinker.hthinker.h, outlined here., outlined here.

The implementations of the The implementations of the three three member functions will be member functions will be placed in a separate file placed in a separate file called called thinker.cxxthinker.cxx, which we , which we will examine in a few will examine in a few minutes.minutes.

Documentation

Class definition:• thinking_cap class definition which we have already seen

Page 15: PowerPoint for Chapter 2: Object-Oriented Programming

Using the Thinking Cap

A program that A program that wants to use the wants to use the thinking cap thinking cap must must includeinclude the the thinker header thinker header file (along with file (along with its other header its other header inclusions).inclusions).

#include <iostream.h>#include <stdlib.h>#include "thinker.h"

...

Page 16: PowerPoint for Chapter 2: Object-Oriented Programming

Using the Thinking Cap

Just for fun, the Just for fun, the example program example program will declare two will declare two thinking_cap thinking_cap variables named variables named student and fan.student and fan.

#include <iostream.h>#include <stdlib.h>#include "thinker.h"

int main( ){ thinking_cap student: thinking_cap fan;

Page 17: PowerPoint for Chapter 2: Object-Oriented Programming

Using the Thinking Cap

Just for fun, the Just for fun, the example program example program will declare two will declare two thinking_cap thinking_cap objectsobjects named named student and fan.student and fan.

#include <iostream.h>#include <stdlib.h>#include "thinker.h"

int main( ) { thinking_cap student; thinking_cap fan;

Page 18: PowerPoint for Chapter 2: Object-Oriented Programming

Using the Thinking Cap

The program The program starts by starts by calling the calling the slots member slots member function for function for student.student.

#include <iostream.h>#include <stdlib.h>#include "thinker.h"

int main( ) { thinking_cap student; thinking_cap fan; student.slots( "Hello", "Goodbye");

Page 19: PowerPoint for Chapter 2: Object-Oriented Programming

Using the Thinking Cap

The program The program starts by starts by activatingactivating the the slots slots member member function function for for student.student.

#include <iostream.h>#include <stdlib.h>#include "thinker.h"

int main( ) { thinking_cap student: thinking_cap fan;

student.slots( "Hello", "Goodbye");

Page 20: PowerPoint for Chapter 2: Object-Oriented Programming

Using the Thinking Cap

The member The member function function activation activation consists of four consists of four parts, starting parts, starting with the object with the object name.name.

int main( ) { thinking_cap student; thinking_cap fan;

student.slots( "Hello", "Goodbye");

Name of the object

Page 21: PowerPoint for Chapter 2: Object-Oriented Programming

Using the Thinking Cap

The instance The instance name is followed name is followed by a period.by a period. int main( )

{ thinking_cap student; thinking_cap fan;

student.slots( "Hello", "Goodbye");A

Perio

d

Page 22: PowerPoint for Chapter 2: Object-Oriented Programming

Using the Thinking Cap

After the period After the period is the name of is the name of the member the member function that you function that you are activating.are activating.

int main( ) { thinking_cap student; thinking_cap fan;

student.slots( "Hello", "Goodbye");

Name of the

Function

Page 23: PowerPoint for Chapter 2: Object-Oriented Programming

Using the Thinking Cap

Finally, the Finally, the arguments for arguments for the member the member function. In this function. In this example the first example the first argument argument (new_green) is (new_green) is "Hello" and the "Hello" and the second argument second argument (new_red) is (new_red) is "Goodbye"."Goodbye".

#include "thinker.h"

int main( ) { thinking_cap student; thinking_cap fan;

student.slots( "Hello", "Goodbye");

Argum

ents

Page 24: PowerPoint for Chapter 2: Object-Oriented Programming

A Quiz

How would you How would you activate student's activate student's push_green push_green member function ?member function ?

What would be the What would be the output of student's output of student's push_green push_green member function member function at this point in the at this point in the program ?program ?

int main( ) { thinking_cap student; thinking_cap fan;

student.slots( "Hello", "Goodbye");

Page 25: PowerPoint for Chapter 2: Object-Oriented Programming

A Quiz

Notice that the Notice that the push_green push_green member member function has no function has no arguments.arguments.

At this point, At this point, activating activating student.push_green student.push_green will print the stringwill print the stringHelloHello..

int main( ) { thinking_cap student; thinking_cap fan;

student.slots( "Hello", "Goodbye");

student.push_green( );

Page 26: PowerPoint for Chapter 2: Object-Oriented Programming

A Quiz

Trace through this Trace through this program, and tell program, and tell me the complete me the complete output.output.

int main( ) { thinking_cap student; thinking_cap fan; student.slots( "Hello", "Goodbye");

fan.slots( "Go Cougars!", "Boo!");

student.push_green( );

fan.push_green( );

student.push_red( ); . . .

Page 27: PowerPoint for Chapter 2: Object-Oriented Programming

A Quiz

HelloHelloGo Cougars!Go Cougars!GoodbyeGoodbye

int main( ) { thinking_cap student; thinking_cap fan; student.slots( "Hello", "Goodbye");

fan.slots( "Go Cougars!", "Boo!");

student.push_green( );

fan.push_green( );

student.push_red( ); . . .

Page 28: PowerPoint for Chapter 2: Object-Oriented Programming

What you know about Objects

Class = Data + Member Functions.Class = Data + Member Functions. You know how to define a new class type, and You know how to define a new class type, and

place the definition in a header file.place the definition in a header file. You know how to use the header file in a You know how to use the header file in a

program which declares instances of the class program which declares instances of the class type.type.

You know how to activate member functions.You know how to activate member functions. But you still need to learn how to write the But you still need to learn how to write the

bodies of a class’s member functions.bodies of a class’s member functions.

Page 29: PowerPoint for Chapter 2: Object-Oriented Programming

Thinking Cap Implementation

class thinking_cap{public: void slots(char new_green[ ], char new_red[ ]); void push_green( ); void push_red( );private: char green_string[50]; char red_string[50];};

Remember that the member function’s bodies Remember that the member function’s bodies generally appear in a separate .cxx file.generally appear in a separate .cxx file.

Function

bodies

will be i

n .cxx

file.

Page 30: PowerPoint for Chapter 2: Object-Oriented Programming

Thinking Cap Implementation

class thinking_cap {public: void slots(char new_green[ ], char new_red[ ]); void push_green( ); void push_red( );private: char green_string[50]; char red_string[50];};

We will look at the body of slots, which must copy its We will look at the body of slots, which must copy its two arguments to the two private member variables.two arguments to the two private member variables.

Page 31: PowerPoint for Chapter 2: Object-Oriented Programming

Thinking Cap Implementation

void thinking_cap::slots(char new_green[ ], char new_red[ ]){ assert(strlen(new_green) < 50); assert(strlen(new_red) < 50); strcpy(green_string, new_green); strcpy(red_string, new_red);}

For the most part, the function’s body is no different For the most part, the function’s body is no different than any other function body.than any other function body.

But there are two special features about a But there are two special features about a member function’s body . . .member function’s body . . .

Page 32: PowerPoint for Chapter 2: Object-Oriented Programming

Thinking Cap Implementation

In the heading, the function's name is preceded by the In the heading, the function's name is preceded by the class name and :: - otherwise C++ won't realize this class name and :: - otherwise C++ won't realize this is a class’s member function.is a class’s member function.

void thinking_cap::slots(char new_green[ ], char new_red[ ]){ assert(strlen(new_green) < 50); assert(strlen(new_red) < 50); strcpy(green_string, new_green); strcpy(red_string, new_red);}

Page 33: PowerPoint for Chapter 2: Object-Oriented Programming

Thinking Cap Implementation

Within the body of the function, the class’s member Within the body of the function, the class’s member variables and other member functions may all be variables and other member functions may all be accessed.accessed.

void thinking_cap::slots(char new_green[ ], char new_red[ ]){ assert(strlen(new_green) < 50); assert(strlen(new_red) < 50); strcpy(green_string, new_green); strcpy(red_string, new_red);}

Page 34: PowerPoint for Chapter 2: Object-Oriented Programming

Thinking Cap Implementation

Within the body of the function, the class’s member Within the body of the function, the class’s member variables and other member functions may all be variables and other member functions may all be accessed.accessed.

void thinking_cap::slots(char new_green[ ], char new_red[ ]){ assert(strlen(new_green) < 50); assert(strlen(new_red) < 50); strcpy(green_string, new_green); strcpy(red_string, new_red);}

But, whose member variables are these? Are they student.green_string student.red_string fan.green_string fan.red_string

?

Page 35: PowerPoint for Chapter 2: Object-Oriented Programming

Thinking Cap Implementation

Within the body of the function, the class’s member Within the body of the function, the class’s member variables and other member functions may all be variables and other member functions may all be accessed.accessed.

void thinking_cap::slots(char new_green[ ], char new_red[ ]){ assert(strlen(new_green) < 50); assert(strlen(new_red) < 50); strcpy(green_string, new_green); strcpy(red_string, new_red);}

If we activate student.slots: student.green_string student.red_string

Page 36: PowerPoint for Chapter 2: Object-Oriented Programming

Thinking Cap Implementation

Within the body of the function, the class’s member Within the body of the function, the class’s member variables and other member functions may all be variables and other member functions may all be accessed.accessed.

void thinking_cap::slots(char new_green[ ], char new_red[ ]){ assert(strlen(new_green) < 50); assert(strlen(new_red) < 50); strcpy(green_string, new_green); strcpy(red_string, new_red);}

If we activate fan.slots: fan.green_string fan.red_string

Page 37: PowerPoint for Chapter 2: Object-Oriented Programming

Thinking Cap Implementation

void thinking_cap::push_green {

cout << green_string << endl;

}

Here is the implementation of the push_green Here is the implementation of the push_green member function, which prints the green message:member function, which prints the green message:

Page 38: PowerPoint for Chapter 2: Object-Oriented Programming

Thinking Cap Implementation

void thinking_cap::push_green {

cout << green_string << endl;

}

Here is the implementation of the push_green Here is the implementation of the push_green member function, which prints the green message:member function, which prints the green message:

Notice how this member function implementation uses the green_string member variable of the object.

Page 39: PowerPoint for Chapter 2: Object-Oriented Programming

A Common Pattern

Often, one or more member functions will Often, one or more member functions will place data in the member variables...place data in the member variables...

class thinking_cap {public: void slots(char new_green[ ], char new_red[ ]); void push_green( ) const; void push_red( ) const;private: char green_string[50]; char red_string[50];};

...so that other member functions may use that data.

slots push_green & push_red

Page 40: PowerPoint for Chapter 2: Object-Oriented Programming

Classes have member variables and member Classes have member variables and member functions. An object is a variable where the data functions. An object is a variable where the data type is a class.type is a class.

You should know how to declare a new class type, You should know how to declare a new class type, how to implement its member functions, how to how to implement its member functions, how to use the class type.use the class type.

Frequently, the member functions of an class type Frequently, the member functions of an class type place information in the member variables, or use place information in the member variables, or use information that's already in the member variables.information that's already in the member variables.

In the future we will see more features of OOP.In the future we will see more features of OOP.

Summary

Page 41: PowerPoint for Chapter 2: Object-Oriented Programming

THE END

Presentation copyright 2010, Addison Wesley LongmanPresentation copyright 2010, Addison Wesley LongmanFor use with For use with Data Structures and Other Objects Using C++Data Structures and Other Objects Using C++by Michael Main and Walter Savitch.by Michael Main and Walter Savitch.

Some artwork in the presentation is used with permission from Presentation Task ForceSome artwork in the presentation is used with permission from Presentation Task Force(copyright New Vision Technologies Inc.) and Corel Gallery Clipart Catalog (copyright(copyright New Vision Technologies Inc.) and Corel Gallery Clipart Catalog (copyrightCorel Corporation, 3G Graphics Inc., Archive Arts, Cartesia Software, Image ClubCorel Corporation, 3G Graphics Inc., Archive Arts, Cartesia Software, Image ClubGraphics Inc., One Mile Up Inc., TechPool Studios, Totem Graphics Inc.).Graphics Inc., One Mile Up Inc., TechPool Studios, Totem Graphics Inc.).

Students and instructors who use Students and instructors who use Data Structures and Other ObjectsData Structures and Other Objects Using C++ Using C++ arearewelcome to use this presentation however they see fit, so long as this copyright notice welcome to use this presentation however they see fit, so long as this copyright notice remains intact.remains intact.