Presentation is loading. Please wait.

Presentation is loading. Please wait.

Spring 2008 Mark Fontenot CSE 2341 - Honors Principles of Computer Science I Note Set 5 1.

Similar presentations


Presentation on theme: "Spring 2008 Mark Fontenot CSE 2341 - Honors Principles of Computer Science I Note Set 5 1."— Presentation transcript:

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

2 Quick Look 2 Overloading

3 Copy Constructor 3 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 4 Required to use reference Shouldn’t need to change the parameter – so make const PersonInfo::PersonInfo(const PersonInfo &Obj) { Name= new char[strlen(Obj.Name) + 1]; strcpy(Name, Obj.Name); Age = Obj.Age; }

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

6 operator=(…) 6 Will be called with statements such as: person2 = person1; OR person2.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 void operator=(const PersonInfo& r)

7 PersonInfo 7 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 8 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=(…) 9 Multiple operators in one statement Person3 = Person2 = Person1; First Second If operator=() returns void, above won’t work To do this, operator=() must return type PersonInfo

10 PersonInfo 10 class PersonInfo { private: char* name; int age; public: //Other member functions PersonInfo operator=(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 11 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 12 #ifdef FEETINCHES_H #define FEETINCHES_H class 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 13 #include “FeetInches.h” #include 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 14 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 15 //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 16 FeetInches FeetInches::operator++() { ++inches; simplify(); return *this; }

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

18 Overloading Relational Operators 18 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 19 May provide a special operator function to convert a class object to any other type. No Return Type Specified – should always return a double FeetInches::operator double() { double temp = feet; temp += (inches / 12.0); return temp; }

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

21 21 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 >

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

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

24 Creating a String Class 24 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 Note: These slides only contain the interface. The implementation is on a handout.

25 MyString 25 #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 26 // 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


Download ppt "Spring 2008 Mark Fontenot CSE 2341 - Honors Principles of Computer Science I Note Set 5 1."

Similar presentations


Ads by Google