Thread: Accessing private Data members

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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

  2. #2
    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;
    }

  3. #3
    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).

  4. #4
    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.

  5. #5
    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;
        
    }

  6. #6
    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.

  7. #7
    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.

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