Help! CString Klasse programmieren



  • Hallo!

    Habe ein Problem, undzwar muss ich in C++ eine Klasse "CString" programmieren.

    mit verschiedenen Konstruktoren

    Addition, Zuweisung, Vergleich, Eingabe und Ausgabe funktionen

    ich habe im Internet bisher leider nichts gefunden, was mich weiterbringen würde. Ich weiß, dass es in C++ bereits eine Klasse String gibt, aber nicht wie ich diese nachprogrammieren könnte.

    BITTE UM HILFE!!! 😕

    Vielen Dank schon im Voraus



  • Na indem du die Funktionen einfach nach und nach in deiner Klasse implementierst.

    z.B. kann deine Klasse CString zwei private Variablen haben: die erste enthält einen Pointer zum String, die zweite enthält die Länge des Strings.
    Bei einer Zuweisung müsstest du dann beispielsweise den Quellstring kopieren, die Länge berechnen und die Kopie sowie die Länger in deinen Variablen speichern.

    class CString
    {
    private:
    	char*  cString;
    	size_t iLength;
    
    public:
    	 CString ();
    	 CString (const char*);
    	// weitere Konstruktoren
    	~CString ();
    
    	size_t GetLength() const;
    	// weitere Methoden
    
    	CString& operator + (const char*);
    	CString& operator + (const CString&);
    	CString& operator = (const char*);
    	CString& operator = (const CString&);
    	// weitere Operatoren
    };
    


  • Danke für die Antwort aber ich habe Problenme mit der cpp Datei

    Ich habe keinen Plan wie es mit dem ausprogramieren geht vielleicht kannst du mir da weiterhelfen.

    Thankx im voraus 👍



  • Wenn du C++ noch gar nicht kennst und lernen möchtest bist du hier falsch - da solltest du lieber gute Bücher und Tutorials suchen.

    Wenn du C/C++ schon kannst, dann verstehe ich dein Problem nicht ganz 🕶



  • Die Grundlage kann ich schon aber C++ ist halt schon schwer und es gibt immer ein paar Leute die besser sind und mir weiterhelfen können.



  • Naja um dir weiterhelfen zu können musst Du erstmal anfangen. Hausaufgaben macht hier niemand gern 😉



  • Hallo

    MartinTroeber schrieb:

    Danke für die Antwort aber ich habe Problenme mit der cpp Datei

    Ich habe keinen Plan wie es mit dem ausprogramieren geht vielleicht kannst du mir da weiterhelfen.

    Thankx im voraus 👍

    Hier wird dir wirklich geholfen, aber indem du solche Fragen stellst, hinterlässt du den Eindruck überhaupt kein C++ zu können und nur jemanden zu suchen, de deine Aufgaben erledigt. Stelle also konkrete Fragen und du wirst Antworten bekommen.

    chrische



  • Hi!

    Neku schrieb:

    CString& operator + (const char*);
    	CString& operator + (const CString&);
    	CString& operator = (const char*);
    	CString& operator = (const CString&);
    

    Du solltest keine Referenzen bei den + Operatoren zurückgeben. Und bei den = Operatoren solltest du garnichts zurückzugegben, also einfach void. Ansonsten ist der Ansatz schon ganz brauchbar. 😉

    grüße



  • Hallo

    David_pb schrieb:

    Hi!

    Du solltest keine Referenzen bei den + Operatoren zurückgeben. Und bei den = Operatoren solltest du garnichts zurückzugegben, also einfach void. Ansonsten ist der Ansatz schon ganz brauchbar. 😉

    grüße

    Du hast sicher mehr Plan als ich, aber Scott Meyers schreibt explizit, dass man den operator= eine Referenz auf *this zurückgeben lassen soll.

    chrische



  • Wenn operator= aber nicht zurückliefert sind keine Zuweisungsketten mehr möglich und operator+ sollte imho am Besten ein const Objekt (Kopie, keine Referenz) zurückliefern um Zuweisungen an eine Operation mit diesem Operator zu vermeiden:

    (a+b)="Hallo"; // funktioniert dann nicht
    


  • Hi!

    Stimmt, beim "=" Operator ist eine Referenz auf den this Zeiger sinnvoll. Muss aber nicht unbedingt gewünscht sein! 🙂

    grüße



  • David_pb schrieb:

    Hi!

    Neku schrieb:

    CString& operator + (const char*);
    	CString& operator + (const CString&);
    	CString& operator = (const char*);
    	CString& operator = (const CString&);
    

    Du solltest keine Referenzen bei den + Operatoren zurückgeben. Und bei den = Operatoren solltest du garnichts zurückzugegben, also einfach void. Ansonsten ist der Ansatz schon ganz brauchbar. 😉

    grüße

    Dooh, ich hab + mit += verwechselt 🙄
    Beim Operator = ist es schon richtig, z.B. "a = b = c;"



  • schau mal hier, da dürften Ansätze dabei sein:
    http://www.c-plusplus.net/forum/viewtopic-var-t-is-83699-and-highlight-is-.html
    http://www.c-plusplus.net/forum/viewtopic.php?t=83500
    http://www.c-plusplus.net/forum/viewtopic-var-t-is-83699-and-postdays-is-0-and-postorder-is-asc-and-start-is-10.html

    Vor allem dies hier:

    class MyString
    {
      private:
        char * pData_;
    
      public:
        friend MyString operator+( const MyString &, const MyString & );
        MyString() : pData_( NULL ) {} // inline ctor
        MyString( const char * pText );
        MyString( const MyString & text ); // Copy-Konstruktor
        ~MyString(); // dtor
        const MyString & operator=( const MyString & text );
    
        bool operator==( const MyString & text ) const;
        char operator[]( int index ) const;
        operator const char * () const; // cast zum C-String
        int getLaenge() const;
        void ausgeben() const;
    };
    
    MyString operator+( const MyString & leftstring, const MyString & rightstring );
    
    /******************************************************************/
    
    // ctor mit C-String als Argument
    MyString::MyString( const char * pText ) : pData_( NULL )
    {
      if( pText )
      {
        pData_ = new char[ strlen( pText ) + 1 ];
        strcpy( pData_, pText );
      }
    }
    
    // copy-ctor
    MyString::MyString( const MyString & text ) : pData_( NULL )
    {
      if( text.pData_ )
      {
        pData_ = new char[ strlen( text.pData_ ) + 1 ];
        strcpy( pData_, text.pData_ );
      }
    }
    
    // dtor
    MyString::~MyString()
    {
      delete[] pData_;
    }
    
    // ==
    bool MyString::operator==( const MyString & text ) const
    {
      if( pData_ == NULL || text.pData_ == NULL )
      {
        return ( pData_ == NULL && text.pData_ == NULL );
      }
      return ( strcmp( pData_, text.pData_ ) == 0 );
    }
    
    // []
    char MyString::operator[]( int i ) const
    {
      if( i >= 0 && i < getLaenge() )
      {
        return pData_[i];
      }
      else
      {
        return '\0';
      }
    }  
    
    // Länge des Textes ermitteln
    int MyString::getLaenge() const
    {
      if( pData_ == NULL )
      {
        return 0;
      }
      else
      {
        return strlen( pData_ );
      }
    }
    
    // cast zu C-String
    MyString::operator const char * () const
    {
      return pData_;
    }
    
    // Zuweisungs-Operator
    const MyString & MyString::operator=( const MyString & text )
    {
      if( this == &text ) // Selbstzuweisung abfangen
      {
        return *this;
      }  
      delete[] pData_;
      if( text.pData_ )
      {
        pData_ = new char[ strlen( text.pData_ ) + 1 ];
        strcpy( pData_, text.pData_ );
      }
      else
      {
        pData_ = NULL;
      }
      return *this;
    }
    
    void MyString::ausgeben() const
    {
      cout << pData_ << endl;
    }
    
    /******************************************************************/
    
    MyString operator+( const MyString & leftstring, const MyString & rightstring )
    {
      MyString result;
      if( leftstring.pData_ != NULL && rightstring.pData_ != NULL )
      {
        result.pData_ = new char[ strlen( leftstring.pData_ ) +
                                  strlen( rightstring.pData_ ) + 1 ];
        strcpy( result.pData_, leftstring.pData_ );
        strcat( result.pData_, rightstring.pData_ );
      }
      else if( rightstring.pData_ != NULL )
      {
        result = rightstring;
      }
      else if( leftstring.pData_ != NULL )
      {
        result = leftstring;
      }
      return result;
    }
    

    ... und vor allem diese verfeinerte Klasse:

    #include <conio.h>
    #include <iostream>
    #include <vector>
    #include <cassert>
    using namespace std;
    
    class EString
    {
        private:
            static const unsigned long FWDBUFFER=15;
            static const unsigned long INPBUFFER=200;
    
            char* string;
            unsigned long len;
            unsigned long bufsize;
            void replace(const char*);
            void replace(const vector<char>& v);
             void insert(unsigned long, unsigned long, const char*);
    
        public:
            EString();
            EString(const char*);
            EString(const char);
            EString(const EString&);
            ~EString();
    
            EString& operator=(const EString&);
            EString& operator=(const char*);
            EString& operator=(const char);
    
            friend ostream &operator<<(ostream&, const EString&);
            friend istream &operator>>(istream&, EString&);
    
            EString& operator+=(const EString&);
            EString& operator+=(const char*);
            EString& operator+=(const char);
    
            friend const EString operator+(const EString&, const EString&);
            const EString operator+(const char*) const;
            const EString operator+(const char) const;
    
            friend const EString operator+(const char*, const EString&);
            friend const EString operator+(const char, const EString&);
    
            char &operator[](unsigned long);
            const char &operator[](unsigned long) const;
            EString operator()(unsigned long, unsigned long) const;
    
            bool operator<(const EString&) const;
            bool operator<=(const EString&) const;
            bool operator==(const EString&) const;
            bool operator!=(const EString&) const;
            bool operator>=(const EString&) const;
            bool operator>(const EString&) const;
    
    };
    
    /*
    ** Private Methoden (technische Methoden)
    */
    
    void EString::replace(const char *s)
    {
        if(string) delete[](string);
        len=strlen(s);
        bufsize=FWDBUFFER+len+1;
        string=new(char[bufsize]);
        assert(string!=0);
        strcpy(string,s);
    }
    
    void EString::replace(const vector<char>& v)
    {
        if(string) delete[](string);
        len=v.size();
        bufsize=FWDBUFFER+len+1;
        string=new(char[bufsize]);
        assert(string!=0);
        unsigned int x=0;
        for (;x<v.size();x++)
        string[x]=v[x];
        string[x]=0;
    }
    
    void EString::insert(unsigned long pos, unsigned long slen, const char *s)
    {
        if(!string)
            {
                len=slen;
                bufsize=FWDBUFFER+len+1;
                string=new(char[bufsize]);
                assert(string!=0);
                strcpy(string,s);
                return;
            }
        else
            {
                if((len+slen+1)<=bufsize)
                    {
                        for(unsigned long x=len+1;x>=pos+1;x--)
                            string[x+slen-1]=string[x-1];
                        for(unsigned long x=0;x<slen;x++)
                            string[x+pos]=s[x];
                        len+=slen;
                    }
                else
                    {
                        bufsize=FWDBUFFER+len+slen+1;
                        char *sptr=new(char[bufsize]);
                        assert(sptr!=0);
                        unsigned long y=0,x;
                        for(x=0;x<pos;x++)
                            sptr[y++]=string[x];
                        for(x=0;x<slen;x++)
                            sptr[y++]=s[x];
                        for(x=pos;x<=len;x++)
                            sptr[y++]=string[x];
                        len+=slen;
                        delete[](string);
                        string=sptr;
                    }
            }
    }
    
    /*
    ** Konstruktoren und Destruktoren
    */
    
    EString::EString(void)
    {
        len=0;
        bufsize=0;
        string=0;
    }
    
    EString::EString(const char *s)
    {
        string=0;
        replace(s);
    }
    
    EString::EString(const char c)
    {
        string=0;
        char s[2];
        s[0]=c;
        s[1]=0;
        replace(s);
    }
    
    EString::EString(const EString &s)
    {
        string=0;
        replace(s.string);
    }
    
    EString::~EString()
    {
        if(string) delete[](string);
    }
    
    /*
    ** Zuweisung
    */
    
    EString &EString::operator=(const EString &s)
    {
        if(this==&s) return(*this);
        replace(s.string);
        return(*this);
    }
    
    EString &EString::operator=(const char *s)
    {
        replace(s);
        return(*this);
    }
    
    EString &EString::operator=(const char c)
    {
        char s[2];
        s[0]=c;
        s[1]=0;
        replace(s);
        return(*this);
    }
    
    /*
    ** Ein-/Ausgabe-Operatoren
    */
    
    ostream &operator<<(ostream &ostr, const EString &s)
    {
        if(s.len) ostr << s.string;
        return(ostr);
    }
    
    istream &operator>>(istream &istr, EString &s)
    {
        char c;
      vector<char> inp;
    
      while(!isspace(c=static_cast<char>(istr.get())))
        inp.push_back(c);
        s.replace(inp);
        return(istr);
    }
    
    /*
    ** Additions-Operatoren
    */
    
    EString &EString::operator+=(const EString &s)
    {
        insert(len,strlen(s.string),s.string);
        return(*this);
    }
    
    EString &EString::operator+=(const char *s)
    {
        insert(len,strlen(s),s);
        return(*this);
    }
    
    EString &EString::operator+=(const char c)
    {
        insert(len,1,&c);
        return(*this);
    }
    
    const EString operator+(const EString &s1, const EString &s2)
    {
        EString tmp=s1.string;
        tmp.insert(tmp.len,strlen(s2.string),s2.string);
        return(tmp);
    }
    
    const EString EString::operator+(const char *s) const
    {
        EString tmp=string;
        tmp.insert(len,strlen(s),s);
        return(tmp);
    }
    
    const EString EString::operator+(const char c) const
    {
        EString tmp=string;
        tmp.insert(len,1,&c);
        return(tmp);
    }
    
    const EString operator+(const char *s, const EString &str)
    {
        EString tmp=s;
        tmp.insert(tmp.len,strlen(str.string),str.string);
        return(tmp);
    }
    
    const EString operator+(const char c, const EString &str)
    {
        EString tmp=c;
        tmp.insert(tmp.len,strlen(str.string),str.string);
        return(tmp);
    }
    
    /*
    ** Indizierungsoperator
    */
    
    char &EString::operator[](unsigned long p)
    {
        assert(p<len);
        return(string[p]);
    }
    
    const char &EString::operator[](unsigned long p) const
    {
        assert(p<len);
        return(string[p]);
    }
    
    /*
    ** Funktionsaufrufoperator
    */
    
    EString EString::operator()(unsigned long p, unsigned long l) const
    {
        assert((p<len)&&((p+l)<=len));
        EString tmp="";
        tmp.insert(0,l,string+p);
        return(tmp);
    }
    
    /*
    ** Vergleichsoperatoren
    */
    
    bool EString::operator<(const EString &s) const
    {return(strcmp(string,s.string)<0);}
    
    bool EString::operator<=(const EString &s) const
    {return(strcmp(string,s.string)<=0);}
    
    bool EString::operator==(const EString &s) const
    {return(strcmp(string,s.string)==0);}
    
    bool EString::operator!=(const EString &s) const
    {return(strcmp(string,s.string)!=0);}
    
    bool EString::operator>=(const EString &s) const
    {return(strcmp(string,s.string)>=0);}
    
    bool EString::operator>(const EString &s) const
    {return(strcmp(string,s.string)>0);}
    
    int main()
    {
        EString s1 = "Hallo ";
        EString s2 = "schoene ";
        EString s3 = "Welt";
        cout << s1+s2+s3 << endl;
        getch();
    }
    

Anmelden zum Antworten