Accessing private Data members

This is a discussion on Accessing private Data members within the C++ Programming forums, part of the General Programming Boards category; I am looking to access private date members in this struct . The original question i am trying to solve ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    80

    Accessing private Data members

    I am looking to access private date members in this struct . The original question i am trying to solve asks
    A character string can be implemented as a linked list of characters . Implement a C++ ADT called Newstring that uses linked listto implement the following string operations:


    Can anye explain how i can access char item2 or use the pointer to?
    here is my code that i am trying to start out with. Is this where i want to start out?



    Code:
    #include<iostream>
    using namespace std;
    
    struct node
    {
           char item;
           node *next;
           
           private:
           char item2;
           char *p;
           
          
           
    };
    
    
    
    int main(){
        
        
        system("pause");
        
        
        return 0;
    }

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    write a public access function
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    80
    when u say public access function are u meaning something like
    Code:
    private:
    
    int data;
    
    public:
    
    int access()
    {
    return data;
    }

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I don't see any purpose for the private members in the first place.

    What you need is a Newstring class that encapsulates the linked list (by storing a pointer to the first/last node) and implements the requested functionality.

    The node may well have all public data, since they should be only used internally within Newstring and the user of this string class should never have to access or deal with the nodes.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    80
    My apologies i didnt post the whole question

    A character string can be implemented as a linked list of characters. Implement a C++ ADT called Newstring that uses linked lists to implement the following string operations:
    display()
    // display private data on standard output
    length()
    // returns the length of string
    concatenate(Newstring)
    // copies contents of parameter Newstring onto the end of private data;
    // does not merely make the last node of the private data
    // point to the first node of Newstring object
    concatenate(char)
    // concatenates a single char onto the end of the private data
    substring(Newstring)
    // returns true if Newstring object is a substring of the private
    // data of the ADT and false otherwise
    In all cases string refers to the private data of your ADT. In implementing this ADT, you may want to write additional, private methods to assist the others. Write a short program to test your ADT. You should have a minimum of three files for this project: a header file for the ADT, an implementation file for the ADT, and a file that uses the ADT

  6. #6
    Registered User
    Join Date
    Jul 2008
    Posts
    80
    Currently i have this as my code, im working on the display().

    Im looking to access the public function named access so i can return item2. or should i go about this a different way. I dont know how to access the public function.

    Code:
    #include<iostream>
    using namespace std;
    
    struct node
    {
           char item;
           node *next;
           
           private:
           char item2;
           char *p;
           
          public:
          
          char access(){
               
               return item2;
               
               }
           
    };
    
    
    
    
    
    int main(){
     
       node *stringdata;
    
    
        system("pause");
        
        
        return 0;
    }

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I still don't understand the purpose of all the members of node. Why are some private and some public?

    Anyway, a node is not a linked list and not a string. You need a Newstring class that might look like this (and the node class which has no relevance to the user of the string class):

    Code:
    struct node
    {
        char c;
        node* next;
    };
    
    class Newstring
    {
        public:
        //constructor, copy constructor, overloaded operator== and destructor
        void display() const;
        unsigned length() const;
        void concatenate(const Newstring&);
        void concatenate(char);
         bool substring(const Newstring&) const;
      
        private:
        node* head;
        //other members if needed
        //other functions if needed
    };
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #8
    Registered User
    Join Date
    Jul 2008
    Posts
    80
    Thanks for the eye opener. One last question before i analyze this and how use it.

    When using the node *next ,is it creating a pointer of in the struct node?
    also I read somewhere that public: member functions can access private data members.

    is it something like

    Newstring::display() // or is this to access the functions?

    please forgive me as the book didnt give very good examples of how to do this.

  9. #9
    Registered User
    Join Date
    Jul 2008
    Posts
    80
    After reading it a few times i figured out how to access the member functions, still clueless on the private data head.

    heres what i came up with.

    Code:
    #include<iostream>
    using namespace std;
    
    struct node
    {
        char c;
        node* next;
    };
    
    class Newstring
    {
        public:
        //constructor, copy constructor, overloaded operator== and destructor
        void display() ;
     
        
        unsigned length() const;
        void concatenate(const Newstring&);
        void concatenate(char);
         bool substring(const Newstring&) const;
      
        private:
        node* head;
        //other members if needed
        //other functions if needed
        
        
    };
    
    void Newstring::display() //initializing Newstring memberfunction
    {
        
          // Here the private data can be, but how??? head is a pointer, but can u only access it by reference?
         
         }
    
    int main() {
        
       Newstring a; // Creating Newstring object
        a.display(); // invoking objects display member function
        
        system("pause");
        return 0;
        
    }

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Do you at all understand a linked list?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Jul 2008
    Posts
    80
    Code:
    struct node
    {
       int item;
       node *next;
    }
    
    head->item=5;
    
    head->next=second;
    second->next=third
    third->next=tail;
    tail->next=NULL;
    I understand the concept of Integer linked lists and have put doubly and circular together, by keeping everything mostly global for ease. When the book isnt easy to understand i make everything global to get the problem so i can at least perform the task. Anything more advance, the book would need an example at least. This is my second CRAPPY class i've taken online that once again gives books that arent aimed for beginners.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I personally think you should consider learning about pointers and memory allocation before you attempt to solve this particular task - you will probably not succeed otherwise.

    Global variables should be an exception, not something commonly used.

    Edit: Memory allocation/freeing in C++ is done with "new" and "delete"

    --
    Mats
    Last edited by matsp; 08-14-2008 at 06:39 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Jul 2008
    Posts
    80
    i totally see you point, wish the book wasnt made like it was.
    Would it have been a huge difference if i started out in the Visual basic course and went that route or does it not make a difference at all.

    Before i go insane trying to figure this out, do i need to use the pointers to access the private data in Newstring or can it be done by just a member function?

  14. #14
    Registered User
    Join Date
    Jul 2008
    Posts
    80
    As part of the code from earlier i tried this and it came out with something i can work with.

    Code:
    void Newstring::display() //initializing Newstring memberfunction
    {
       // char item is needed to be or point to the head node which is private
       node *help=new node;
       help->item='c';
       cout << help->item << endl;
         }
    but finding out how to use the private data still unknown.

  15. #15
    Registered User
    Join Date
    Jul 2008
    Posts
    80
    Ok, I'm almost bald from all the hair pulling. I came up with this result that gets display function to out put the info i put in the *head pointer. Am i on the right track to completeing ths problem?

    Code:
    #include<iostream>
    using namespace std;
    
    struct node
    {
        char item;
        node* next;
    };
    
    class Newstring
    {
        public:
        //constructor, copy constructor, overloaded operator== and destructor
        void display() ;
     
        
        unsigned length() const;
        void concatenate(const Newstring&);
        void concatenate(char);
         bool substring(const Newstring&) const;
      
        private:
        node* head;
        
        
        //other members if needed
        //other functions if needed
        
        
    };
    
    
    void Newstring::display() //initializing Newstring memberfunction
    {
      Newstring::head=new node;
      head->item='c';
      cout << head->item << endl;
           
         }
         
         
    
    int main() {
        
       Newstring a; // Creating Newstring object
        a.display(); // invoking objects display member function
        
        system("pause");
        return 0;
        
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. still confused with constructors and private data members
    By freddyvorhees in forum C++ Programming
    Replies: 2
    Last Post: 07-26-2008, 09:29 AM
  2. C# and SQL
    By siten0308 in forum C# Programming
    Replies: 2
    Last Post: 07-09-2008, 12:34 PM
  3. Using private class members in static functions
    By sethjackson in forum C++ Programming
    Replies: 2
    Last Post: 09-23-2005, 09:54 AM
  4. C diamonds and perls :)
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 05-16-2003, 10:19 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21