Header file:
// pe10-8arr.h -- header file for a simple list class
#ifndef SIMPLEST_
#define SIMPLEST_
// program-specific declarations
const int TSIZE = 45; // size of array to hold title
struct film
{
char title[TSIZE];
int rating;
};
// general type definitions
typedef struct film Item;
const int MAXLIST = 10;
class simplist
{
private:
Item items[MAXLIST];
int count;
public:
simplist(void);
bool isempty(void);
bool isfull(void);
int itemcount();
bool additem(Item item);
void transverse( void (*pfun)(Item &item));
};
#endif
Code using header:
#include "pe10-8arr.h"
simplist::simplist(void)
{
count = 0;
}
bool simplist::isempty(void)
{
return count == 0;
}
bool simplist::isfull(void)
{
return count == MAXLIST;
}
int simplist::itemcount()
{
return count;
}
bool simplist::additem(Item item)
{
if (count == MAXLIST)
return false;
else
items[count++] = item;
return true;
}
void simplist::transverse( void (*pfun)(Item &item))
{
for (int i = 0; i < count; i++)
(*pfun)(items[i]);
}
#include <iostream>
#include <cstdlib> // prototype for exit()
#include "pe10-8arr.h" // simple list class declaration
// array version
void showmovies(Item &item); // to be used by transverse()
int main(void)
{
using namespace std;
simplist movies; // creates an empty list
Item temp;
if (movies.isfull()) // invokes isfull() member function
{
cout << "No more room in list! Bye!\n";
exit(1);
}
cout << "Enter first movie title:\n";
while (cin.getline(temp.title,TSIZE) && temp.title[0] != '\0')
{
cout << "Enter your rating <0-10>: ";
cin >> temp.rating;
while(cin.get() != '\n')
continue;
if (movies.additem(temp) == false)
{
cout << "List already is full!\n";
break;
}
if (movies.isfull())
{
cout << "You have filled the list.\n";
break;
}
cout << "Enter next movie title (empty line to stop):\n";
}
if (movies.isempty())
cout << "No data entered. ";
else
{
cout << "Here is the movie list:\n";
movies.transverse(showmovies);
}
cout << "Bye!\n";
return 0;
}
void showmovies(Item &item)
{
std::cout << "Movie: " << item.title << " Rating: "
<< item.rating << std::endl;
}
The code above just compile and run successfully. Can anyone tell me why the function showmovies() can access the item member of simplist using reference without being declared as friend function or member function?
The function is handed an
Itemobject (by reference), it does not know or care where it came from. The caller of the function is the one that actually pulled the object from inside the complete object, buttransversehas access by being a member of the class.