Results 1 to 10 of 10

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User MrShadow's Avatar
    Join Date: Jul:2007
    Location:
    Posts: 2,606

    C++ - ostream output

    ,
    string ( ). , .

    Code:
    abcdefg
    1
    1
    1&  @☼>Press any key to continue . . .
    - .

    main()

    Code:
    int main(){
        String a("abcdefgh");
        cout<<a<<endl;
        String b;
        String c = a;
        cin >> b;
        cout << b << endl;
        cout << c;
        system("PAUSE");
        return 0;
    }
    overload-a << ( friend, data members):

    Code:
    ostream& operator<<(ostream& out,const String a){
             if(a.length == 0) { return out;}
             for(int i = 0; i<a.length; i++){
                     out<<a.s[i];
             }
             return out;
    }
    , . >> , b . b + c.


    : C B ( abcdefgh). .

  2. #2
    Registered User
    Join Date: Jul:2004
    Location:
    Posts: 621
    - "h" , () . , , member-.

    // : , operator<< "if(a.length == 0)". , for- out. ,

  3. #3
    Registered User MrShadow's Avatar
    Join Date: Jul:2007
    Location:
    Posts: 2,606
    Quote Originally Posted by DimKo View Post
    - "h" , () . , , member-.

    // : , operator<< "if(a.length == 0)". , for- out. ,
    "h"- ,

    + .

    Code:
    class String{
          public:
                 String();
                 String(char* c);
                 ~String();
                 String(const String& s);
                 String& operator =(const String& b);
                 bool operator==(String b);
                 bool operator<(String b);
                 bool operator>(String b);
                 String& operator+(String b);
                 int strlen();
                 friend ostream& operator<<(ostream& out,const String a);
                 friend istream& operator>>(istream& in, String& a);
          private:
                  char* s;      
                  int length;
                  int size;
    };
    
    String::String(){
                     length = 0;
                     size = 0;
    }
    
    String::String(char* c){
                         int counter = 0;
                         while( c[counter] != '\0'){
                                counter++;      
                         }
                         
                         length = counter;
                         size = 2*counter;
                         s = new char[size];
                         
                         for(int i=0; i<length; i++){
                                 s[i] = c[i];
                         }
    }
    
    
    String::String(const String& STR){
                          length = STR.length;
                          size = STR.size;
                          s = new char[size];
                          for(int i=0; i<length; i++){
                                  s[i] = STR.s[i];
                          }
    }
    
    
    
    String::~String(){
                      delete[] s;
    }
    
    String& String::operator=(const String& b){
            if ( this == &b ) { return *this; }
            delete[] s;
            s = new char[b.size];
            size = b.size;
            length = b.length;
            for(int i=0; i<length; i++){
                    s[i] = b.s[i];
            }
            return *this;
    }

  4. #4
    Registered User
    Join Date: Jul:2004
    Location:
    Posts: 621
    : operator<<, , .. copy-- operator<< . :-) , - reference: "const String& a."

    ...

  5. #5
    Registered User MrShadow's Avatar
    Join Date: Jul:2007
    Location:
    Posts: 2,606
    Quote Originally Posted by DimKo View Post
    : operator<<, , .. copy-- operator<< . :-) , - reference: "const String& a."

    ...
    . ?

  6. #6
    Registered User
    Join Date: Jul:2004
    Location:
    Posts: 621
    - . -

    ... "s = NULL;" default- , "delete[] s" - ++ . "c == NULL". strlen() memcpy() .

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Copyright © 1999-2011 . .
iskamPC.com | mobility.BG | Bloody's Techblog | | 3D Vision Blog |