0

Hi fellas
I'm reading "Teach yourself C++" book by Herbert Schildt to learn C++. there is an example in chapter 10 which is tough to me. this is the code I'm talking about:

#include <iostream>
#include <cstdlib>
#include <cctype>

using namespace std;

class list {
public:
    list *head; // pointer to start of list
    list *tail; //pointer to end of list
    list *next; //pointer to next item
    int num; // value to be stored

    list() { head = tail = next = null;}
    virtual void store(int i) = 0;
    virtual void retrieve() = 0;
};

//create a queue-type list
class queue : public list {
public:
    void store(int i);
    int retrieve();
};

void queue::store(int i)
{
    list *item;

    item = new queue;
    if(!item) {
        cout << "Allocation error.\n";
        exit(1);
    }
    item->num = i;

    //put on end of list
    if(tail) tail->next = item;
    tail = item;
    item->next = null;
    if(!head) head = tail;
}

int queue::retrieve()
{
    int i;
    list *p;

    if(!head) {
        cout << "list empty.\n";
        return 0;
    }
    // remove from start of list
    i = head->num;
    p = head;
    head = head->next;
    delete p;

    return i;
}

// create a stack-type list
class stack : public list {
public:
    void store(int i);
    int retrieve();
};

void stack::store(int i)
{
    list *item;

    item = new stack;
    if(!item) {
        cout << "Allocation error.\n";
        exit(1);
    }
    item->num = i;

    //put on front of list for stack-like operation
    if(head) item->next = head;
    head = item;
    if(!tail) tail = head; 
}

int stack::retrieve()
{
    int i;
    list *p;

    if(!head) {
        cout << "list empty.\n";
        return 0;
    }
    // remove from start of list
    i = head->num;
    p = head;
    head = head->next;
    delete p;

    return i;
}

int main()
{
    list *p;

    // demonstrate queue
    queue q_ob;
    p = &q_ob; // point to queue

    p->store(1);
    p->store(2);
    p->store(3);

    cout << "Queue: ";
    cout << p->retrieve();
    cout << p->retrieve();
    cout << p->retrieve();

    cout << '\n';

    //demonstrate stack
    stack s_ob;
    p = &s_ob; // point to stack

    p->store(1);
    p->store(2);
    p->store(3);

    cout << "Stack: ";
    cout << p->retrieve();
    cout << p->retrieve();
    cout << p->retrieve();

    cout << '\n';

    return 0;
}

This is alone tough code that I've seen so far in this book. now the questions:

1- He has defined three class pointer named head,tail and next as "list" object. are we allowed to define these pointers within class "list"? it a bit made me confused.

2- In some parts of the code you can see something e.g. this:

"tail->next = item;"

We use "->" to refer a data-object of a class. am I right? but here "tail" and "next" are both pointer to an object of "list". then why does he use "->"? totally these parts e.g. "tail->next" make me confused.

3- Would you please tell me how "store" and "retrive" functions work?

Notice this part of the book talks about polymorphism by virtual functions.

2
Contributors
1
Reply
34
Views
1 Year
Discussion Span
Last Post by Gribouillis
2

If X is a list instance, one can access its members with X.tail or X.next. On the other hand if X is a pointer to a list instance, one writes X->tail and X->next. It is only the syntax of C++. Notice that in the case of a pointer, there is a risk that X is the null pointer, in which case there will be a segmentation fault at run time.

One can declare pointers to list as fields in the list class because a pointer is only an address in memory. It takes a constant amount of memory which can be computed by the compiler. The size of an actual list instance is much bigger: it is the size of 3 pointers and an integer.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.