Results 1 to 10 of 10
Thread: C++ - ostream output
Hybrid View
-
24th June 2012 14:00 #1
C++ - ostream output
,
string ( ). , .
- .Code:abcdefg 1 1 1& @☼>Press any key to continue . . .
main()
overload-a << ( friend, data members):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; }
, . >> , b . b + c.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; }
: C B ( abcdefgh). .
-
24th June 2012 16:04 #2Registered User
Join Date: Jul:2004
Location:
Posts: 621
- "h" , () . , , member-.
// : , operator<< "if(a.length == 0)". , for- out. ,
-
24th June 2012 16:14 #3
"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; }
-
24th June 2012 16:18 #4Registered User
Join Date: Jul:2004
Location:
Posts: 621
: operator<<, , .. copy-- operator<< . :-) , - reference: "const String& a."
...
-
24th June 2012 16:20 #5
-
24th June 2012 17:02 #6Registered User
Join Date: Jul:2004
Location:
Posts: 621
- . -

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




Reply With Quote

Lenovo ThinkPad 15 IdeaPad 15
5th May 2023, 22:16 in