32
Thinking Cap Implementation
In the heading, the function’s name is preceded by the
class name and :: – otherwise C++ won’t realize this
is a class’s member function.
void ThinkingCap::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);
}
33
Thinking Cap Implementation
Within the body of the function, the class’s member
variables and other member functions may all be
accessed.
void ThinkingCap::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);
}
34
Thinking Cap Implementation
Within the body of the function, the class’s member
variables and other member functions may all be
accessed.
void ThinkingCap::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
?
35
Thinking Cap Implementation
Within the body of the function, the class’s member
variables and other member functions may all be
accessed.
void ThinkingCap::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
36
Thinking Cap Implementation
Within the body of the function, the class’s member
variables and other member functions may all be
accessed.
void ThinkingCap::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
37
Thinking Cap Implementation
void ThinkingCap::push_green
{
cout << green_string << endl;
}
Here is the implementation of the push_green
member function, which prints the green message:
38
Thinking Cap Implementation
void ThinkingCap::push_green
{
cout << green_string << endl;
}
Here is the implementation of the push_green
member function, which prints the green message:
Notice how this member function implementation
uses the green_string member variable of the object.
39
A Common Pattern
Often, one or more member functions will
place data in the member variables…
class ThinkingCap {
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
40
Classes have member variables and member
functions. An object is a variable where the data
type is a class.
You should know how to declare a new class type,
how to implement its member functions, how to
use the class type.
Frequently, the member functions of an class type
place information in the member variables, or use
information that’s already in the member variables.
In the future we will see more features of OOP.
Summary
41
THE END
Presentation copyright 1997, Addison Wesley Longman
For use with
Data Structures and Other Objects Using C++
by Michael Main and Walter Savitch.
Some artwork in the presentation is used with permission from Presentation Task Force
(copyright New Vision Technologies Inc.) and Corel Gallery Clipart Catalog (copyright