260 likes | 365 Views
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
E N D
CSE 2341 - HonorsPrinciples 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 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
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
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
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
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; } };
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
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
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; } };
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
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
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
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
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
Prefix ++ operator FeetInches FeetInches::operator++() { ++inches; simplify(); return *this; }
Postfix ++ operator FeetInchesFeetInches::operator++(int) { FeetInches temp(feet, inches); inches++; simplify(); return temp; }
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; }
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
Overloading << and >> FeetInches x; //other code //Good cout << x.getFeet() << “ feet”; cout << x.getInches() << “ inches”; //Better cout << x; cin >> x; //that’s better also
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;
Overloading << and >> ostream& operator<< (ostream& strm, const FeetInches& obj) { strm << obj.feet << “ feet, “ << obj.inches << “ inches”; return strm; }
Overloading << and >> istream& operator>> (istream& strm, FeetInches& obj) { cout << “Feet: “; strm >> obj.feet; cout << “Inches: “; strm >> obj.inches; obj.simplify(); return strm; }
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
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();
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