Chapter11 - Dr. Serdar Kirli 1 Chapter 11 Object-Oriented...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Dr. Serdar Kirli 1 Chapter 11 Object-Oriented Programming - II #include <iostream> #include <cstdlib> #include <string> using namespace std; class ezString { private : char * strPtr; unsigned len; public : ezString(); ezString( const char * s); ezString(ezString & s); ~ezString( void ); unsigned length( void ) const { return len; } ezString & operator += ( const ezString & s); ezString & operator = ( const ezString & s); friend ezString operator + ( const ezString & s1, const ezString & s2); friend ostream & operator << (ostream & st, const ezString & s); }; Constructors ezString Class: Big Picture Destructor Member Functions Friend Functions #include <iostream> #include <cstdlib> #include <string> using namespace std; class ezString { private : char * strPtr; unsigned len; public : ezString(); ezString( const char * s); ezString(ezString & s); ~ezString( void ); unsigned length( void ) const { return len; } ezString & operator += ( const ezString & s); ezString & operator = ( const ezString & s); friend ezString operator + ( const ezString & s1, const ezString & s2); friend ostream & operator << (ostream & st, const ezString & s); }; Operator Overloads ezString Class: Big Picture #include <iostream> #include <cstdlib> #include <string> using namespace std; class ezString { private : char * strPtr; unsigned len; public : ezString(); ezString( const char * s); ezString(ezString & s); ~ezString( void ); unsigned length( void ) const { return len; } ezString & operator += ( const ezString & s); ezString & operator = ( const ezString & s); friend ezString operator + ( const ezString & s1, const ezString & s2); friend ostream & operator << (ostream & st, const ezString & s); }; ezString Class: Big Picture inline function: prototype and code together An unsigned ( int ) variable only holds positive values length() returns the private variable len ezString Class: Structure strPtr len An ezString object strPtr len Dynamic character array #include <iostream> #include <cstdlib> #include <string> using namespace std; class ezString { private : char * strPtr; unsigned len; public : ezString(); ezString( const char * s); ezString(ezString & s); ~ezString( void ); unsigned length( void ) const { return len; } ezString & operator += ( const ezString & s); ezString & operator = ( const ezString & s); friend ezString operator + ( const ezString & s1, const ezString & s2); friend ostream & operator << (ostream & st, const ezString & s); }; Constructors ezString Class: Big Picture ezString::ezString() { strPtr = new char [ 1 ]; strPtr[ ] = '\0' ; len = ; } ezString::ezString( const char * s) { len = strlen(s); strPtr = new char [len+ 1 ]; strcpy(strPtr, s); } ezString::ezString(ezString & s) { len = s.len; strPtr = new char [len+ 1 ]; strcpy(strPtr, s.strPtr); } ezString s ; ezString s 1 ( "ABC" ); ezString s 2 (s 1 ); strPtr len ezString Constructors The constructor first allocates a one element character array pointed to by strPtr s0 ezString::ezString() {...
View Full Document

Page1 / 71

Chapter11 - Dr. Serdar Kirli 1 Chapter 11 Object-Oriented...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online