0

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 ?

  • IMHO, the distinction of what should be `private` is determined by you, the designer. What functions *should* the client be able to call, and what functions *should not* be callable? What functions *should* be callable by derived classes (protected) – AndyG Oct 19 '22 at 20:28
  • General rule of thumb: Use the tightest possible permissions Don't go out of your way to lock everything down, All you can really do is prevent accidents, the malicious little s and the truly stupid will always find a way around simple defenses like `private` and `const`, but the people who simply screw up and will be glad it got caught by the compiler. Over complicate things to force an ideology and otherwise sane people will start embracing the stupid to get around the unnecessary walls you throw up. – user4581301 Oct 19 '22 at 20:58
  • 1
    For a linked list, I wall things up pretty tightly. The node class is not even exposed to the users because the more the code that uses the list knows about the list, the more tightly they are wedded to the list. If the user doesn't know about the nodes, you can perform wholesale replacements on the guts of the list without them having to change a line of code. – user4581301 Oct 19 '22 at 21:02
  • Which functions are supposed to be private? My interpretation would be that the client (or main.cpp) does not need to know what Node does. So you should not include "node.h" in in "linklist.h" to hide that class. – Rasmus-123 Oct 19 '22 at 21:03
  • And since `protected` came up, think hard before you apply inheritance to a linked list. If you want, say, a sorted list, strongly consider leaving the list alone and writing a the sorted list class as a wrapper, an adapter, around the list class. – user4581301 Oct 19 '22 at 21:05
  • 1
    Regarding `void Print(Node*);`, ask yourself, discuss it with [your rubber duck](https://en.wikipedia.org/wiki/Rubber_duck_debugging) if necessary, why the print function should do anything other than print the list starting at its `head` member through to its end. Why should the user have to provide a `Node*`? How does the user get a `Node*` in the first place? – user4581301 Oct 19 '22 at 21:10

0 Answers0