Inheritance ( simple please help)

  • 18 years ago

    Ok children, before i giveyou the problem id just like to explain what im trying to do.


    The 1 major difference between C++ and every other language ive learned, is the strings, you cant compare & or + them, also there are no built in functions to change there case (upper or lower) determine there length or to get a small section of them, (for searching).


    So, to learn how to use these strings i decided to make a string Class, which would have all these functions, as i thought it would be a good was to learn these new string rules.


    I have created my class, it has all the described functions, ( all work fine ) ( and the operators '=' {makes the string equal to  a sting on the right} '==' which compares 2 classes and returns true of false, and '&' which appends two string classes together.


    anouther problem, was the string length, unlike in VB it has to be fixed ( thee array has to be declaired with a constant).


    As this class i created was for my use whenever i wanted to use a clas,,, BIG or Small and wanting to be a 'GOOD' programmer didnt want to use massive strings in cases when i only needed to store a name, I thought i could use inheritance to solve my problem.


    As a variables in a derived class override the variables in the base class ( when names are identicle ) i thought i could derive MANY classes from the base class which used al of the same functions and variables but with 1 difference,,,, the member char array would be of different sizes,, ( i used protected, not private incase u thought i made that mistake)


    The only problem,,, The Over loaded operators dont seems to be inherited by the drived class Why the hell not ??



    I realise i may seem very dumb, but ive only been learning c++ for a couple of weeks, so forgive me for any obviouse mistakes or bad practices i am using.
    Here is my code for any1 who thinks they can point out whats wrong,


    ThanX


    (NOTE, if you try to use this in a c++ program, you will notice that if you use the base class CStrClass Everything works fine, but the classes derived from it do not.)




    // StrClass.h
    // Basic String Functions
    //-----------------------------------------------------------------------------------------------------------------------------
    // Base Class    | CStrClass     |                          |  Holds Strings of Max Length 1024  + Null
    // Derived Class | CStrClass64   |  Derived From CStrClass  |  Holds Strings of Max Length 64    + Null
    // Derived Class | CstrClass128  |  Derived From CStrClass  |  Holds Strings of Max Length 128   + Null
    // Derived Class | CstrClass256  |  Derived From CStrClass  |  Holds Strings of Max Length 256   + Null
    // Derived Class | CstrClass128  |  Derived From CStrClass  |  Holds Strings of Max Length 512   + Null
    // Derived Class | CstrClass128  |  Derived From CStrClass  |  Holds Strings of Max Length 2048  + Null
    // Derived Class | CstrClass128  |  Derived From CStrClass  |  Holds Strings of Max Length 5120  + Null
    // Derived Class | CstrClass128  |  Derived From CStrClass  |  Holds Strings of Max Length 10240 + Null
    //-----------------------------------------------------------------------------------------------------------------------------
    // Class Functions::      |   Returns:  |  Parameters:                    | Description:
    // UCase                  |   void      |  void                             | Converts all lowercase characters to uppercase.
    // LCase                  |   void      |  void                             | Converts all uppercase characters to lowercase.
    // GetChar                |   char      |  (int N)                          | Returns Nth Character of the member char array.
    // SetChar                |   void      |  (int N, char C)                  | Gets the Nth character to character C.
    // GetPointer             |   char*     |  void                             | Returns a pointer to the string, use to acces the string directly, Eg cout << MyString.GetPointer();
    // GelLen                 |   int       |  void                             | Returns the number of characters in the string (excluding the terminating NULL '\0')
    // Mid                    |   void      |  (char pchar, int start, int len) | Read (len) Number of Characters From the member char arrar starting from character (start). and put the characters in the character array which pchar points to,, eg. MyString.Mid( &AString[0], 2, 5) or, MyString.Mid( MyString.GetPointer(), 2, 5)
    //-----------------------------------------------------------------------------------------------------------------------------
    // OverLoaded Operators:  |  Returns:    |  Parameters: |    Description:
    //                      = |  void        |  char* pchar |    Copys the string pointer pchar points to into the member char array.
    //                      = |  void        |  Same Class  |    Copys the string from the parameter class into its own.
    //                     == |  bool        |  Same Class  |    Compares the parameter class' string with its own, returns true if the two are identicle.
    //                      & |  Same Class  |  Same Class  |    Appends the Parameters class' string to its own.
    //
    // *************************************************************************************************************************
    // Base Class
    class CStrClass
    {
    protected:
       char m
    charString[1024];
       int  miMaxIndex;
       void m
    vChangeCase(bool Upper);        


    public:
       CStrClass()                  {miMaxIndex = 1024; mcharString[0] = '\0'; mcharString[miMaxIndex] = '\0';}
       void UCase()                 {mvChangeCase(true);}
       void LCase()                 {m
    vChangeCase(false);}
       char GetChar(int N)          {return mcharString[N];}
       void SetChar(int N, char C)  {m
    charString[N] = C;}
       char* GetPointer()           {return &mcharString[0];}
       int  GetLen();
       void Mid(char* pchar,int start, int len);
       
       void       operator =   (char* pchar)
       {
           int N=0;
           while ((*(pchar + N) != '\0') && ( N < m
    iMaxIndex ))
           {
               mcharString[N] = *(pchar + N);
               N++;
           }
           m
    charString[N] = '\0';
       }
       void       operator =   (CStrClass TempClass);
       bool       operator ==  (CStrClass TempClass);
       CStrClass  operator &   (CStrClass TempClass);
    };
    // ****************************************
    //
    ****************************************
    // Derived Class'
    class CStrClass64: CStrClass
    {
    protected:
       char mcharString[64];
    public:
       CStrClass64(){m
    iMaxIndex = 64; mcharString[0] = '\0'; mcharString[miMaxIndex] = '\0';}
    };
    //-----------------------------------------------------------------------------------------------------------------------------
    class CStrClass128: CStrClass
    {
    protected:
       char m
    charString[128];
    public:
       CStrClass128(){miMaxIndex = 128; mcharString[0] = '\0'; mcharString[miMaxIndex] = '\0';}
    };
    //-----------------------------------------------------------------------------------------------------------------------------
    class CStrClass256: CStrClass
    {
    protected:
       char mcharString[256];
    public:
       CStrClass256(){m
    iMaxIndex = 256; mcharString[0] = '\0'; mcharString[miMaxIndex] = '\0';}
    };
    //-----------------------------------------------------------------------------------------------------------------------------
    class CStrClass512: CStrClass
    {
    protected:
       char m
    charString[512];
    public:
       CStrClass512(){miMaxIndex = 512; mcharString[0] = '\0'; mcharString[miMaxIndex] = '\0';}
    };
    //-----------------------------------------------------------------------------------------------------------------------------
    class CStrClass2048: CStrClass
    {
    protected:
       char mcharString[2048];
    public:
       CStrClass2048(){m
    iMaxIndex = 2048; mcharString[0] = '\0'; mcharString[miMaxIndex] = '\0';}
    };
    //-----------------------------------------------------------------------------------------------------------------------------
    class CStrClass5120: CStrClass
    {
    protected:
       char m
    charString[5120];
    public:
       CStrClass5120(){miMaxIndex = 5120; mcharString[0] = '\0'; mcharString[miMaxIndex] = '\0';}
    };
    //-----------------------------------------------------------------------------------------------------------------------------
    class CStrClass10240: CStrClass
    {
    protected:
       char mcharString[10240];
    public:
       CStrClass10240(){m
    iMaxIndex = 10240; mcharString[0] = '\0'; mcharString[miMaxIndex] = '\0';}
    };
    // *************************************************************************************************************************
    // *************************************************************************************************************************
    // Member Functions
    void CStrClass::m
    vChangeCase(bool Upper)
    {
       int N=0;


       while (N < GetLen())
       {
           if (Upper = true)
           {
               if ((mcharString[N] > 96) && (mcharString[N] < 123)) { mcharString[N] = mcharString[N] - 32; }
           }
           else
           {
               if ((mcharString[N] > 64) && (mcharString[N] < 91 )) { mcharString[N] = mcharString[N] + 32; }
           }
           N++;
       }
    }
    //-----------------------------------------------------------------------------------------------------------------------------
    // Public Functions
    int CStrClass::GetLen()
    {
       int N=0;
       while ((N < miMaxIndex) && (mcharString[N] != '\0' )) {N++;}
       return N;
    }
    //-----------------------------------------------------------------------------------------------------------------------------
    void CStrClass::Mid(char* pchar, int start, int len)
    {
       for (int N=start; N < (start + len); N++)
       {
           (pchar+(N-start)) = m_charString[N];
       }
       
    (pchar+(N-start)) = '\0';
    }
    // ****************************************
    // OverLoaded Operators
    *********************************
    // void CStrClass:perator = (char* pchar)
    //{
    //    int N=0;
    //    while ((*(pchar + N) != '\0') && ( N < miMaxIndex ))
    //    {
    //        m
    charString[N] = *(pchar + N);
    //        N++;
    //    }
    //    mcharString[N] = '\0';
    //}
    //-------------------------------------------------------------------------------------------------------------------------
    void CStrClass:perator = (CStrClass TempClass)
    {
       int N=0;
       while ((TempClass.GetChar(N) != '\0' ) && ( N < m
    iMaxIndex ))
       {
           mcharString[N] = TempClass.GetChar(N);
           N++;
       }
       m
    charString[N] = '\0';
    }
    //-------------------------------------------------------------------------------------------------------------------------
    bool CStrClass:perator == (CStrClass TempClass)
    {
       bool Equal = true;
       int N=0;
       if (GetLen() != TempClass.GetLen())
       {return false;}
       else
       {
           while (( N < GetLen()) && (Equal = true))
           {
               if (mcharString[N] != TempClass.GetChar(N)) { Equal = false ;}
               N++;
           }
       }
       return Equal;
    }
    //-----------------------------------------------------------------------------------------------------------------------------
    CStrClass CStrClass:perator & (CStrClass TempClass)
    {
       CStrClass ReturnClass;
       int       N=0,X=0;
       while ((GetChar(N) != '\0') && (N < m
    iMaxIndex))
       {
           ReturnClass.SetChar( N, GetChar(N));
           N++;
       }
       while ((TempClass.GetChar(X) != '\0') && ( N < m_iMaxIndex ))
       {
           ReturnClass.SetChar(N, TempClass.GetChar(X));
           N++;
           X++;
       }
       ReturnClass.SetChar(N,'\0');
       return ReturnClass;
    }


    // **************



Post a reply

No one has replied yet! Why not be the first?

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Theory is when you know something, but it doesn't work. Practice is when something works, but you don't know why. Programmers combine theory and practice: Nothing works and they don't know why.”