1 / 26

CSE 2341 - Honors Principles of Computer Science I

CSE 2341 - Honors Principles of Computer Science I. Spring 2008 Mark Fontenot mfonten@engr.smu.edu. Note Set 5. Quick Look. Overloading. Copy Constructor. Copy constructor called whenever new object created and initialized with another object’s data

paley
Download Presentation

CSE 2341 - Honors Principles of Computer Science I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 2341 - HonorsPrinciples of Computer Science I Spring 2008 Mark Fontenot mfonten@engr.smu.edu Note Set 5

  2. Quick Look Overloading

  3. Copy Constructor Copy constructor called whenever new object created and initialized with another object’s data Like all other constructors, but accepts a reference parameter of its own type If copy constructor not explicitly defined, one is automatically provided that performs memberwise copy

  4. Copy Constructors • PersonInfo::PersonInfo(const PersonInfo &Obj) • { • Name= new char[strlen(Obj.Name) + 1]; • strcpy(Name, Obj.Name); • Age = Obj.Age; • } Required to use reference Shouldn’t need to change the parameter – so make const

  5. Operator Overloading C-Strings String Objects char s1[50], s2[50]; strcpy(s1, “Hello”); strcpy(s2, “World”); strcat(s1, “ “); strcat(s1, s2); string s1, s2; s1 = “Hello”; s2 = “World”; s1 += “ “ + s2; The standard operators in C++ can be redefined for use with objects Helps to make operations more intuitive

  6. operator=(…) void operator=(const PersonInfo& r) • Will be called with statements such as: • person2 = person1; ORperson2.operator=(person1); • Parameter r – declared as reference • prevents invocation of copy constructor for copy of object • Parameter declared constant – shouldn’t be any changes to r in this function

  7. PersonInfo class PersonInfo { private: char* name; int age; public: //Other member functions void operator=(const PersonInfo &right) { delete [] Name; name = new char[strlen(right.name) + 1]; strcpy(name, right.name); age = right.age; } };

  8. Aside: The this pointer • this is a special built-in pointer • available to any member function • contains the address of the object that called the member function • passed as a hidden argument to all non-static member functions

  9. operator=(…) Person3 = Person2 = Person1; Second Person3 = Person2 = Person1; First • If operator=() returns void, above won’t work • To do this, operator=() must return type PersonInfo Multiple operators in one statement

  10. PersonInfo class PersonInfo { private: char* name; int age; public: //Other member functions PersonInfooperator=(const PersonInfo &right) { delete [] Name; name = new char[strlen(right.name) + 1]; strcpy(name, right.name); age = right.age; return *this; } };

  11. General Issues of Operator Overloading • You can completely alter the intuitive meaning of an operator (can make = mean +)…. NOT a good idea!! • You cannot change the number of operands needed by an operator. • Cannot overload the following operators: • . .* ?: :: sizeof

  12. Class FeetInches #ifdef FEETINCHES_H #define FEETINCHES_Hclass FeetInches { private: int feet; int inches; void simplify(); public: FeetInches(int f = 0, int i = 0); void setFeet(int f); void setInches(int i); int getFeet(); int getInches(); FeetInches operator+(const FeetInches&); FeetInches operator-(const FeetInches&); }; #endif FeetInches.h

  13. Class FeetInches #include “FeetInches.h” #include <cstdlib> using namespace std; FeetInches::FeetInches(int f = 0, int i = 0) { feet = f; inches = i; simplify(); } void FeetInches::setFeet(int f) {feet = f;} void FeetInches::setInches(int i) { inches = i; simplify(); } int FeetInches::getFeet() {return feet;} int FeetInches::getInches() {return inches;} FeetInches.cpp

  14. Class FeetInches FeetInches FeetInches::operator+(const FeetInches& r) { FeetInches temp; temp.inches = inches + r.inches; temp.feet = feet + r.feet; temp.simplify() return temp; } FeetInches FeetInches::operator-(const FeetInches& r) { FeetInches temp; temp.inches = inches – r.inches; temp.feet = feet –r.feet; temp.simplify(); return temp; } FeetInches.cpp

  15. Class FeetInches //Ensures feet/inches in simplest terms //no inches >= 12 void FeetInches::simplify(void) { if (inches >= 12) { feet += (inches / 12); // Integer division inches = inches % 12; } else if (inches < 0) { feet -= ((abs(inches) / 12) + 1); inches = 12 - (abs(inches) % 12); } } FeetInches.cpp

  16. Prefix ++ operator FeetInches FeetInches::operator++() { ++inches; simplify(); return *this; }

  17. Postfix ++ operator FeetInchesFeetInches::operator++(int) { FeetInches temp(feet, inches); inches++; simplify(); return temp; }

  18. Overloading Relational Operators int FeetInches::operator>(const FeetInches &Right) { if (Feet > Right.Feet) return 1; else if (Feet == Right.Feet &&Inches > Right.Inches) return 1; else return 0; }

  19. Object Conversion FeetInches::operator double() { double temp = feet; temp += (inches / 12.0); return temp; } May provide a special operator function to convert a class object to any other type. No Return Type Specified – should always return a double

  20. Overloading << and >> FeetInches x; //other code //Good cout << x.getFeet() << “ feet”; cout << x.getInches() << “ inches”; //Better cout << x; cin >> x; //that’s better also

  21. Overloading << and >> ostream& operator<< (ostream& strm, const FeetInches& obj); istream& operator>> (istream& strm, FeetInches& obj); • - >> and << are part of the istream and ostream classes • returns a stream object to allow for chaining • cout << x << endl;

  22. Overloading << and >> ostream& operator<< (ostream& strm, const FeetInches& obj) { strm << obj.feet << “ feet, “ << obj.inches << “ inches”; return strm; }

  23. Overloading << and >> istream& operator>> (istream& strm, FeetInches& obj) { cout << “Feet: “; strm >> obj.feet; cout << “Inches: “; strm >> obj.inches; obj.simplify(); return strm; }

  24. Creating a String Class Note: These slides only contain the interface. The implementation is on a handout. • Great example to demonstrate operator overloading • MyString class defines an ADT for handing strings • Memory management is handled “behind the scenes” • Use operators for intuitive string manipulation

  25. MyString #ifndef MYSTRING_H #define MYSTRING_H class MyString; //Forward Declaration ostream& operator<<(ostream&, const MyString&); istream& operator>>(istream&, MyString&); class MyString { private: char* str; int len; public: MyString(); MyString(MyString& right); MyString(char* sptr); ~MyString(); int length(); const char* getValue();

  26. MyString // 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 *); 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

More Related