Inheritance

This is a discussion on Inheritance within the C++ Programming forums, part of the General Programming Boards category; What I'm trying to do is pass a string from my derived class to my base class. A function called ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    22

    Inheritance

    What I'm trying to do is pass a string from my derived class to my base class. A function called nextToken() should return a String object up to the delimiter(in this case ':').
    This string
    dunc0035:!:5701:500::/shome/dunc0035:/bin/ksh
    Should be displayed as
    dunc0035 ! 5701:500 /shome/dunc0035 /bin/ksh

    When I run my program it display some of the right info..and some garbage....

    This is the code I have....
    Code:
    class String
    {
            protected:
                    enum { SZ = 100 };
                    char str[SZ];
            public:
                    String ()
                    { str[0] = '\0'; }
                    String (char s[] )
                    { strcpy(str, s); }
                    void display() const
                    { cout << str; }
                    operator char*()
                    { return str; }
    
    };
    
    class StringTokenizer : public String
    {
            protected:
                    int i;
                    char colon;
    
            public:
    
                    StringTokenizer(char obj[], char cn ) : i(0), colon(cn)
                    {
    
                      strcpy(str,obj);
    
                    }
    
                    int countToken(char obj[], char colon)
                    {
    
                            int len = strlen(obj);
                            i++;
                            for(int n =0; n< len; n++)
                            {
    
                                    if(obj[n] == colon)
                                    i++;
    
                            }
    
                            return i;
                    }
    
                     bool hasMoreTokens ()
                    {
    
    
                            int length = strlen(str);
    
                            return (i < length) ? true : false;
    
                    }
    
                    String nextToken()
                    {
                            int a = strlen(str);
                            char temp[80];
                            int j=0;
    
                            while(str[i] != colon)
                            {
                                    temp[j]=str[i];
    
                                    j++;
                                    i++;
                            }
                            i++;
                            
                           return String(temp);
    
    
                    }
    
    };
    
    
    
    
    
    int main()
    {
             String s;   s= "dunc0035:!:5701:500::/shome/dunc0035:/bin/ksh";
    
             StringTokenizer tokens(s, ':');
    
                   
        while ( tokens.hasMoreTokens() )
    	{
             tokens.nextToken();
             String token = tokens.nextToken();
             cout << static_cast<char*>(token);
             cout<< endl;
             }
    
               return 0;
    
    }
    Any help would be great...thanks

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >                String nextToken()
    >                {
    >                        int a = strlen(str);
    >                        char temp[80];
    >                        int j=0;
    >
    >                        while(str[i] != colon)
    >                        {
    >                                temp[j]=str[i];
    >
    >                                j++;
    >                                i++;
    >                        }
    Teminate the string here, so:
    Code:
                    String nextToken()
                    {
                            int a = strlen(str);
                            char temp[80];
                            int j=0;
    
                            while(str[i] != colon)
                            {
                                    temp[j]=str[i];
    
                                    j++;
                                    i++;
                            }
                            temp[j] = '\0';

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > tokens.nextToken();
    > String token = tokens.nextToken();

    And you have two nextToken() calls here, so it will display every other token.

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    22
    Thanks .... it all worked...

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    22
    Ok....now I have to change it to an Array of 10...

    Code:
    *	const int SIZE = 10;
            String s[SIZE]= {
                                 {"root:!:0:0::/:/bin/ksh"},
                                 {"daemon:!:1:1::/etc:"},
                                 {"markm:!:253:501:Maitang Mark:/thome/markm:/usr/bin/ksh"},
                                 {"mcmasti:!:332:501:Ian McMaster:/thome/mcmasti:/usr/bin/ksh"},
                                 {"rozo0001:!:5700:500::/shome/rozo0001:/bin/ksh"},
                                 {"dunc0035:!:5701:500::/shome/dunc0035:/bin/ksh"},
                                 {"lapo0045:!:5702:500::/shome/lapo0045:/bin/ksh"},
                                 {"cast0010:!:5703:500::/shome/cast0010:/bin/ksh"},
                                 {"benn0049:!:5704:500::/shome/benn0049:/bin/ksh"},
                                 {"hhhhh:!:5710:500::/shome/ferr0026:/bin/ksh"}};
    
     StringTokenizer tokens(s, ':');
    How do I make it work with an Array?????

    Thanks again for any help...

  6. #6
    twm
    twm is offline
    root
    Join Date
    Sep 2003
    Posts
    232
    >How do I make it work with an Array?????
    Code:
    StringTokenizer tokens[] = {
      StringTokenizer(s[0], ':'),
      StringTokenizer(s[1], ':'),
      StringTokenizer(s[2], ':'),
      StringTokenizer(s[3], ':'),
      StringTokenizer(s[4], ':'),
      StringTokenizer(s[5], ':'),
      StringTokenizer(s[6], ':'),
      StringTokenizer(s[7], ':'),
      StringTokenizer(s[8], ':'),
      StringTokenizer(s[9], ':'),
    };
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 04:03 PM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  3. inheritance and performance
    By kuhnmi in forum C++ Programming
    Replies: 5
    Last Post: 08-04-2004, 01:46 PM
  4. Inheritance and Polymorphism
    By bench386 in forum C++ Programming
    Replies: 2
    Last Post: 03-18-2004, 10:19 PM
  5. Inheritance vs Composition
    By Panopticon in forum C++ Programming
    Replies: 11
    Last Post: 01-20-2003, 04:41 AM

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