Thread: WARNING NOOB ALERT! Linked List Node get set

  1. #1
    Registered User
    Join Date
    Oct 2016
    Posts
    9

    WARNING NOOB ALERT! Linked List Node get set

    Okay so i'm gonna start with the old cliche "I'm new to C++...", I dont understand the use of the getNext() setNext() etc functions i am supposed to implement in my ListNode.cpp file..

    Code:
    class ListNode{
        private:
            Person* data;
            ListNode* next;
        
        public:
            ListNode* getNext();
            void setNext(ListNode* n);
            Person* getData();
            void setData(Person* p);
    }
    I am trying to create a chained hash table implementing a singly linked list, I thought i could just use pointers in the LinkedList.cpp implementation to minipulate the nodes and their data when adding and deleting?? Or have i got this all backwards and the get and set methods are something else entirely??

    I'm sorry if this is a silly question, im still trying to understand a lot of these concepts. If I have left out anything feel free to ask..

    Thanks

  2. #2
    Registered User
    Join Date
    Oct 2016
    Posts
    9
    Looking at my code again there.. The thought occurred that the data and next pointer that are stored in my ListNode class are PRIVATE, i could be wrong but does this mean they can only be accessed by the PUBLIC functions of that class? If so i think i 'might' know where i got confused.. The get and set have to be used in order to access these values?

    Like i said anyone can feel free to jump in and save me if they wish haha

  3. #3
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Yeah, the privatization of variables in a class or struct in C++ means that only member functions of that class have access to those variables. This is where the concept of getters and setters comes into play. By exposing public functions, you're creating an interface that controls how the data is interacted with. This can oftentimes make it more difficult to the wrong thing.

    I'm also not completely sure what you're asking but I'm happy that you're the first person I've seen in a while to actually be making a C++-style linked list.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    This code
    Code:
    class ListNode{
        private:
            Person* data;
            ListNode* next;
        
        public:
            ListNode* getNext();
            void setNext(ListNode* n);
            Person* getData();
            void setData(Person* p);
    }
    Does mean that "data" and "next" are NOT able to be seen/used by the code using this class. Note: They can be used inside of the class.
    To get/set the values you need to use the assessors like getNext() when NOT inside the class.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    To clear up your confusion, all functions inside a class can always access that data regardless of whether it's public, protected or private. Those access modifiers are only used to specify if code outside the class can access them or not (and if derived classes can access the data of the class it is inheriting from). Functions work similarly since they can also be marked public, protected or private.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Oct 2016
    Posts
    9
    Quote Originally Posted by MutantJohn View Post
    Yeah, the privatization of variables in a class or struct in C++ means that only member functions of that class have access to those variables. This is where the concept of getters and setters comes into play. By exposing public functions, you're creating an interface that controls how the data is interacted with. This can oftentimes make it more difficult to the wrong thing.

    I'm also not completely sure what you're asking but I'm happy that you're the first person I've seen in a while to actually be making a C++-style linked list.
    Thank you, appreciate the help! Yeah i'm not even sure i know what i'm asking some times to be honest haha... Practice makes perfect i guess (or at least a little less hopeless).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-04-2010, 01:18 PM
  2. Get Nth node in a linked list
    By budala in forum C Programming
    Replies: 4
    Last Post: 04-11-2009, 11:42 PM
  3. Replies: 0
    Last Post: 09-16-2008, 05:04 AM
  4. traversing a linked list with a node and list class
    By brianptodd in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2003, 11:57 AM
  5. Replies: 5
    Last Post: 10-04-2001, 03:42 PM

Tags for this Thread