Unformatted text preview:

Object Oriented Programming Using C CLASS 16 Objectives Overload the relational and combined assignment operators for class objects Overloading Relational Operators if Distance1 Distance2 code Relational operator example int FeetInches operator const FeetInches Right if Feet Right Feet return 1 else if Feet Right Feet Inches Right Inches return1 else return 0 Overloading the Operator In addition to the traditional operators C allows you to change the way the symbols work include iostream include INTARRAY H main IntArray Table 10 Store values in the array for int x 0 x 10 x Table x x Display the values in the array for x 0 x 10 x cout Table x cout endl cout Now attempting to store a value in Table 11 n Table 11 0 return 0 Program Output 0123456789 Now attempting to store a value in Table 11 ERROR Subscript out of range Object Conversion Special operator functions may be written to convert a class object to any other type FeetInches operator float void float temp feet femp inches 12 0 return temp Note No return type is specified in the function header for the previous example Because it is a FeetInches to float conversion function it will always return a float Creating a String Class This section shows the use of a C class to create a string data type The MyString class Memory is dynamically allocated for any string stored in a MyString Strings may be assigned to a MyString object with the operator One string may be concatenated to another with the operator Strings may be tested for equality with the operator MyString ifndef MYSTRING H define MYSTRING H include cstring For string library functions include stdlib For exit function for handling strings using namespace std class MyString private char str int len public MyString str NULL Len 0 MyString char MyString MyString right Copy constructor MyString continues str new char right length 1 memory leak in book strcpy str right getValue len right length MyString void if len 0 delete str int length void return len char getValue void return str MyString continues overloaded operators MyString operator MyString char operator const char MyString operator MyString char operator const char int operator MyString int operator const char int operator MyString int operator const char MyString continues int operator MyString int operator const char int operator const char int operator MyString int operator MyString int operator const char friend ostream operator ostream MyString friend istream operator istream MyString endif Contents of MYSTRING CPP include mystring h include cstring using namspace std MyString MyString operator MyString right if len 0 delete str str new char right length 1 should check to see if memory was allocated strcpy str right getValue len right length return this Overloaded operator Called when operand on the right is a string Returns the Str member of the calling object char MyString operator const char right if len 0 delete str len strlen right str new char len 1 strcpy str right return str Overloaded operator Called when operand on the right is another MyStringcontinues object Concatenates the Str MyString member of Right to the Str member of the calling object Returns the calling object MyString MyString operator MyString right char temp str str new char strlen str right length 1 strcpy str temp strcat str right getValue if len 0 delete temp len strlen str return this Overloaded operator Called when operand on the right is a string char MyString operator const char right char temp str str new char strlen str strlen right 1 strcpy str temp strcat str right if len 0 delete temp return str Overloaded operator Called when the operand on the right is a MyString object Returns 1 if Right Str is the same as Str int MyString operator MyString right return strcmp str right getValue Overloaded operator Called when the operand on the right is a string Returns 1 if Right is the same as Str int MyString operator const char right return strcmp str right Overloaded operator Called when the operand on the right is a MyString object Returns 1 if Right Str is not equal to Str int MyString operator MyString right return strcmp str right getValue Overloaded operator Called when the operand on the right is a string Returns 1 if Right is not equal to Str int MyString operator const char right return strcmp str right Overloaded operator Called when the MyString continues operand on the right is a MyString object Returns 1 if Right Str is greater than Str int MyString operator MyString right if strcmp str right getValue 0 return 1 else return 0 Overloaded operator Called when the operand on the right is a string Returns 1 if Right is greater than Str int MyString operator const char right if strcmp str right 0 return 1 else return 0 Overloaded operator Called when the operand on the right is a MyString object Returns 1 if Right Str is less than Str int MyString operator MyString right if strcmp str right getValue 0 return 1 else return 0 Overloaded operator Called when the operand on the right is a string Returns 1 if Right is less than Str int MyString operator const char right if strcmp str right 0 return 1 else return 0 Overloaded operator Called when the operand on the right is a MyString object Returns 1 if Right Str is greater than or equal to Str int MyString operator MyString right if strcmp str right getValue 0 return 1 else return 0 Overloaded operator Called when the operand on the right is a string Returns 1 if Right is greater than or equal to Str int MyString operator const char right if strcmp str right 0 return 1 else return 0 Overloaded operator Called when the operand on the right is a MyString object Returns 1 if Right Str is less than or equal to Str int MyString operator MyString right if strcmp str right getValue 0 return 1 else return 0 Overloaded operator Called when the operand on the right is a string Returns 1 if Right is less than or equal to Str int MyString operator const char right if strcmp str right 0 return 1 else return 0 Overloaded stream insertion operator ostream operator ostream strm MyString obj strm obj str return strm Overloaded stream extraction operator istream operator istream strm MyString obj strm getline obj str strm ignore return strm This program demonstrates the MyString class Be sure to compile this program with MYSTRING CPP include iostream include mystring h using namespace std int main MyString Object1 This Object2 is MyString Object3 a test MyString Object4 Object1 Call copy


View Full Document

SMU CSE 2341 - Object-Oriented Programming Using C++

Loading Unlocking...
Login

Join to view Object-Oriented Programming Using C++ and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Object-Oriented Programming Using C++ and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?