![]() |
||
Lesson 5
Objective
|
External member functions
Rewrite Person class using external member function |
|
|
Use an external member function to print out the member data of the Person class.
To define a member function outside the class, the scope resolution operator is used. To illustrate this, let's revisit the ch_stack class from the previous module. We can change the declaration of push, inside the declaration of class ch_stack, to just a function prototype and define push elsewhere. When we define it, we write the name out fully using the scope resolution operator. In this case, the function is not implicitly inline.
const int max_len = 40;
class ch_stack {
public:
void reset() { top = EMPTY; }
void push(char c);
char pop() {
assert(top!= EMPTY);
return s[top--];
}
char top_of() {
assert(top!= EMPTY);
return s[top];
}
bool empty()
{ return (top == EMPTY); }
bool full()
{ return (top == FULL); }
private:
char s[max_len];
int top;
enum { EMPTY = -1, FULL = max_len - 1 };
};
void ch_stack::push(char c) //not inline
{
assert(top != FULL);
top++;
s[top] = c;
}
External Member Function - Exercise
Click the Exercise link below to rewrite the person class so it uses an external member function to print out its member
data.
External Member Function - Exercise |
||
|
|
||
