It's moderator's Office

Hello! We are back! Let's move one step up.

We have discussed the important functions of the passenger class until now. We also discussed the important building blocks of C++ - the overloading and dynamic memory allocation. Let us look at actual implementation now.

In this blog, we will be discussing more about the functions of the Moderator class. Moderator is the higher authority class which actually is able to modify the details of trains and can alter the timetable. Let us look at the functions one by one. 

As we have discussed in function overloading we have two functions having the same name but different parameters as input. Here is the illustration of a function named add_trains(). 

class moderator
{
private:
trains *head,*tail;
public:
    moderator()
    {
        head = NULL;
        tail = NULL;
    }

    void add_trains();
    void add_trains(int a,char *t, char *s,char *d,int c,int h, int m);

    void deleteTrains(struct trains *head_ref, int position);

   trains* gethead() {
        return head;
    }

    static void display(trains *head);

};

The function of having no parameters is used to add new trains dynamically.  The one who is accessing the Moderator class can alter the trains and used add_trains() functions. But for creating the database of trains that are previously scheduled we used the parameterized function and overloading. The below code refers to the creation of a timetable of trains.

void moderator :: add_trains(int a, char *t, char *s, char *d, int c, int h, int m)
{
        trains *tmp = new trains;
        tmp->tr_no = a;
        strcpy(tmp->train_name, t);
        strcpy(tmp->start, s);
        strcpy(tmp->destiantion, d);
        tmp->charge = c;
        tmp->time_h = h;
        tmp->time_m = m;
        tmp->next = NULL;

        if(head == NULL)
        {
            head = tmp;
            tail = tmp;
        }
        else
        {
            tail->next = tmp;
            tail = tail->next;
        }
}


As illustrated in the above code, we use dynamic memory allocation to add a new node when the moderator wants to add new trains. 
Besides, there are two functions, deleteTrains() and display trains(). These functions also have the same concepts. Data structures and class inheritance is used for implementation. 




- Sampada Petkar (K 62)



Comments

Post a Comment

Popular posts from this blog

Do you have a ticket?

Dynamic Memory Allocation in C++