C++ Making functions not accessed by the client private

46 Views Asked by At

Here is Main.cpp:

#include <iostream>
#include <cstdlib>
#include "linklist.h"

using namespace std; 

void menu(List &);

int main()
{
    //new list 
    List list;
    menu(list);
    return 0;
}

void menu(List &list)
{
    char choice;
    int item;
    
    do {
        system("CLS");  // for ise of std library 
        cout<<"\t Menu\n";
        cout<<"\t\tOptions\n\n";
        cout<<"\t\tInsert a Student (press I)\n";
        cout<<"\t\tRemove a Student (press R)\n";
        cout<<"\t\tDisplay List of Student (press D)\n";
        cout<<"\t\tClear List (press C)\n";
        cout<<"\t\tExit (press E)\n";
        cout<<"What would you like to do ? Press the coresponding key:";
        cin>>choice;
        choice = toupper(choice);
        cin.ignore();
        
        switch(choice)
        {
            case 'I':
                list.Insert();
                break;
            case 'R':
                list.Remove();
                break;
            case 'D':
                list.Display();cin.get();
                break;
            case 'C':
                list.Clear();cin.get();
                break;
        }
        

Here is list.cpp

#include "linklist.h"
#include <iostream>

using namespace std;


// insert at begining of the linked list 
void List::Insert()
{
    int student_id;
    short test_one,test_two;
    float average;
    
    cout << "Enter the Student's ID: "; cin >> student_id;
    cout << "Enter the score of Test One: "; cin>> test_one;
    cout << "Enter the score of Test Two: "; cin >> test_two;
    average = (test_one + (double)test_two)/2; 
    
    // new node 
    Node* newnode = new Node();
    newnode->setnode(student_id,test_one,test_two,average);
    newnode->setnext(head);
    head = newnode;
    
}

// deletes from the last node in the list 
void List::Remove()
{
    Node* curr = head;
    
    if (curr = NULL) {
        cout << "No nodes to remove\n ";
        return;
    }
    else if (curr->getnext() == NULL ) // it only has 1 ! 
    {
        delete curr;
        head = NULL;
     } 
     else 
     {
        Node *prev; 
        do
            {
                prev = curr;                    // the prev becomes the curr
                curr = curr->getnext();
             }while(curr->getnext() != NULL); // while the next one is not empty
             
             prev->setnext(NULL);
             
             delete curr; // delte the curr and it will repeat as prev becomes curr
                          // unill curr=NULL
     }
}


// displays the nodes to the console
void List::Display()
{
    Print(head);
}
    void List::Print(Node* temp)
    {
        if (temp = NULL)
            {
                cout << "End" << endl;
                return;
            }else
                {
                    temp->getnode(); 
                    Print(temp->getnext()); // these two steps will continue
                }                           // until temp=NULL
                
            
    }

// clears the linked list of all nodes  
void List::Clear()
{
    Node *temp = head;    //store current head in temp
    while(head != NULL)
    {
        head = head->getnext(); // set next head to head
        delete temp;            // delete current head in temp 
    }
}

Here is header file for the node class with functions:

#ifndef NODE_H
#define HODE_H

#include <iostream>

using namespace std;

// Here is the node class
class Node {
    int student_id;
    short test_one;
    short test_two;
    float average;
    Node* next;
    
    public:
            Node(){}
            void setnode(int student_id,short test_one,short test_two,float average)
            {
                student_id = student_id;
                test_one = test_one;
                test_two = test_two;
                average = average; 
            }
            void setnext(Node *link)
            {
                next = link;
            }
            void getnode( )const
            {
                cout << "The student's ID #: " << student_id << endl;
                cout << "Score of Test One: " << test_one << endl;
                cout << "Score of Test Two: " << test_two << endl;
                cout << "The average: " << average << endl; 
            }
            // returns address of the next node 
            Node* getnext()
            {
                return next;
            }
};
#endif 

Here is the header file for the class

#include "node.h"
#include <iostream>

class List 
{
    Node *head;
    public:
        List()
        {
            head = NULL;
        }
        void Insert();
        void Remove();
        void Clear();
        void Display();
        void Print(Node*);
    
};

Now what has to be done is a refactor of the following code so that:

"functions that are not accessed by the client should be private, mutator functions parameters are constant referenced, and all accessor functions are constants."

I've tired just switching to private, I've tried creating friends, and I have read something about runtime polymorphism and didn't try to implement because it did not seem to apply. I don't know where to start now because I'm under the impression that main.cpp is the client and it has to have access in order for this to work.

How would the above refactor ("functions that are not accessed by the client should be private, mutator functions parameters are constant referenced, and all accessor functions are constants.") work/look ?

After reading some of the comments and some more research. I've decided that setnode, getnode, getnext, setnext and Print should be private. They do not need any interaction with a user. Or I should say, that a user can incorrectly input parameters.

How about pass by reference ?

I'm starting to refactor but I'm still getting error's that the functions are private. Can anyone show a code snippet to refactor one of (or all that should be private) the functions to private so it works when its called by main.cpp ?

0

There are 0 best solutions below