21
Using the Thinking Cap
The instance
name is followed
22
Using the Thinking Cap
After the period
23
Using the Thinking Cap
Finally, the
arguments for
the member
#include “thinker.h”
int main( ) {
Arguments
24
A Quiz
How would you
activate student’s
push_green
member function ?
What would be the
output of student’s
push_green
member function
at this point in the
program ?
int main( )
{
ThinkingCap student;
ThinkingCap fan;
student.slots( “Hello”, “Goodbye”);
25
A Quiz
Notice that the
push_green member
function has no
arguments.
At this point,
activating
student.push_green
will print the string
Hello.
int main( ) {
ThinkingCap student;
ThinkingCap fan;
student.slots( “Hello”, “Goodbye”);
student.push_green( );
26
A Quiz
Trace through this
program, and tell
me the complete
output.
int main( )
{
ThinkingCap student;
ThinkingCap fan;
student.slots( “Hello”, “Goodbye”);
fan.slots( “Go Cougars!”, “Boo!”);
student.push_green( );
fan.push_green( );
student.push_red( );
. . .
27
The important thing to notice is that student and fan are separate
objects of the ThinkingCap class. Each has its own green_string and
red_string data. Or to throw one more piece of jargon at you: Each has
A Quiz
Hello
Go Cougars!
Goodbye
int main( )
{
ThinkingCap student;
ThinkingCap fan;
student.slots( “Hello”, “Goodbye”);
fan.slots( “Go Cougars!”, “Boo!”);
student.push_green( );
fan.push_green( );
student.push_red( );
. . .
28
What you know about Objects
Class = Data + Member Functions.
You know how to define a new class type, and
place the definition in a header file.
29
Thinking Cap Implementation
class ThinkingCap
{
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
generally appear in a separate .cxx file.
Function bodies
will be in .cxx file.
30
Thinking Cap Implementation
class ThinkingCap
{
public:
void slots(char new_green[ ], char new_red[ ]);
We will look at the body of slots, which must copy its
two arguments to the two private member variables.
31
For the most part, all that’s needed is a pair of calls to strcpy to copy
the two arguments (new_green and new_red) to the two member
variables (green_string and red_string). By the way, how many of you
have seen this use of strcpy before? If you haven’t seen it, don’t worry–
Thinking Cap Implementation
void ThinkingCap::slots(char new_green[ ], char new_red[ ])
{
assert(strlen(new_green) < 50);
For the most part, the function’s body is no different
than any other function body.