Hi,

I made a program for an exercise that is in a book I use to teach myself C++.

The code works, but I would be grateful if someone could have a look and write something about the structure of it. I'm a bit confused as to how member variables and functions are accessed. Sometimes a dot . is used, like in t[0].sn = 1 and sometimes the operator ->, like in head->nl . Could someone explain the difference between these two? I also wonder how I could detect if there is a memory leak?

The task is to make a stack object to hold videotape objects and then to display the videotape objects from the stack.

Seron

Code:
// videostacktest.cpp
// exercise 4.14

#include "videostack.h"
#include "videostack.cpp"
#include <string>
using namespace std;

void main() {

  const int arraysize = 3;

  videotape t[arraysize];		// creates a videotape array

  t[0].sn = 1;				// creates three videotapes
  t[0].title = "The Warriors";
  t[0].rp = 10;
  t[0].cust = "Mr Pink";
  t[0].dout = 0;
  t[0].din = 0;

  t[1].sn = 2;
  t[1].title = "Ghost in the shell";
  t[1].rp = 10;
  t[1].cust = "Mr White";
  t[1].dout = 0;
  t[1].din = 0;

  t[2].sn = 3;
  t[2].title = "Lord of the rings";
  t[2].rp = 15;
  t[2].cust = "Mr Blue";
  t[2].dout = 0;
  t[2].din = 0;

  node videotapes;			// creates a node (linked list)

  node* head = 0;			// head is a nodepointer with the value 0
  node* nl;			        // creates another nodepointer, (nodelink)
  videotape* vtp[arraysize];		// creates a videotape-pointer-array

  // copies videotapes to the stack

  for(int i = 0; i < arraysize; i++) {
  	
    vtp[i] = new videotape;		// allocates memory for a videotape and stores the  address in an array
    *(vtp[i]) = t[i];			// copies tape information to memory
  
    nl = head;				// copies the head pointer
    head = videotapes.add(vtp[i], nl);	// passes videotape and node pointers
					// and returns a pointer to this node,
					// which becomes the head pointer
					// the node now contians a videotape pointer
					// and a node pointer (node link)
  }

  // prints videotape info, moves to the next node and
  // unallocates memory occupied by the nodes
  
  while(head != 0) {

    (*(head->vtp)).print();		// prints the data stored @ head->vtp 
    nl = head->nl;			// gets the nodelink from head->nl
    delete head;			// unallocates memory used by node
    head = nl;				// sets head to the node pointed to by nl
  }
  
  // unallocates memory occupied by videotapes
  
  for(int i = 0; i < arraysize; i++)
  	
  	delete vtp[i];			// unallocates memory used by videotape data
}
Code:
// videostack.h
// exercise 4.14

#include <string>
using namespace std;

struct videotape {

  int sn;                     // serial number
  string title;
  int rp;                     // rental price
  string cust;                // customer
  int dout;                   // date out (rented)
  int din;                    // date in (returned)

  void print();
};

struct node {

  videotape* vtp;             // videotape pointer
  node* nl;                   // node pointer

  void initialize();
  node* add(videotape* vtp0, node* nl0);
};
Code:
// videostack.cpp
// exercise 4.14

#include <iostream>
#include <string>
using namespace std;

void videotape::print() {

  cout << endl;
  cout << "s/n:\t\t" << sn << endl;
  cout << "Title:\t\t" << title << endl;
  cout << "Rental price:\t" << rp << endl;
  cout << "Last customer:\t" << cust << endl;
  cout << "Date rented:\t" << dout << endl;
  cout << "Date returned:\t" << din << endl;
  cout << endl;
}

node* node::add(videotape* vtp0, node* nl0) {

  node* newnode = new node;	// crates a new node and passes the address
				// created for carrying a pointer to the videotape
				// and a pointer to the previous node
  newnode->vtp = vtp0;		// passes pointer to the videotape
  newnode->nl = nl0;		// passes pointer to the previous node

  return newnode;		// return nodeaddress
}