26
Spring 2008 Mark Fontenot [email protected] CSE 2341 - Honors Principles of Computer Science I Note Set 5 1

CSE 2341 - Honors Principles of Computer Science I

  • Upload
    paley

  • View
    30

  • Download
    0

Embed Size (px)

DESCRIPTION

CSE 2341 - Honors Principles of Computer Science I. Spring 2008 Mark Fontenot [email protected]. Note Set 5. Quick Look. Overloading. Copy Constructor. Copy constructor called whenever new object created and initialized with another object’s data - PowerPoint PPT Presentation

Citation preview

Page 1: CSE 2341 - Honors Principles of Computer Science I

Spring 2008

Mark [email protected]

CSE 2341 - HonorsPrinciples of Computer Science I

Note Set 51

Page 2: CSE 2341 - Honors Principles of Computer Science I

Quick Look

2

Overloading

Page 3: CSE 2341 - Honors Principles of Computer Science I

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

Page 4: CSE 2341 - Honors Principles of Computer Science I

Copy Constructors

4

Required to use referenceShouldn’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;}

Page 5: CSE 2341 - Honors Principles of Computer Science I

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

Page 6: CSE 2341 - Honors Principles of Computer Science I

operator=(…)

6

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

void operator=(const PersonInfo& r)

Page 7: CSE 2341 - Honors Principles of Computer Science I

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; }};

Page 8: CSE 2341 - Honors Principles of Computer Science I

Aside: The this pointer

8

this is a special built-in pointer available to any member functioncontains the address of the object that called the member

functionpassed as a hidden argument to all non-static member

functions

Page 9: CSE 2341 - Honors Principles of Computer Science I

operator=(…)

9

Multiple operators in one statement

Person3 = Person2 = Person1;

Person3 = Person2 = Person1;

First

Second

• If operator=() returns void, above won’t work

• To do this, operator=() must return type PersonInfo

Page 10: CSE 2341 - Honors Principles of Computer Science I

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; }};

Page 11: CSE 2341 - Honors Principles of Computer Science I

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

Page 12: CSE 2341 - Honors Principles of Computer Science I

Class FeetInches

12

#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

Page 13: CSE 2341 - Honors Principles of Computer Science I

Class FeetInches

13

#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

Page 14: CSE 2341 - Honors Principles of Computer Science I

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

Page 15: CSE 2341 - Honors Principles of Computer Science I

Class FeetInches

15

//Ensures feet/inches in simplest terms//no inches >= 12void 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

Page 16: CSE 2341 - Honors Principles of Computer Science I

Prefix ++ operator

16

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

Page 17: CSE 2341 - Honors Principles of Computer Science I

Postfix ++ operator

17

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

Page 18: CSE 2341 - Honors Principles of Computer Science I

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;}

Page 19: CSE 2341 - Honors Principles of Computer Science I

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;}

Page 20: CSE 2341 - Honors Principles of Computer Science I

Overloading << and >>

20

FeetInches x;//other code

//Goodcout << x.getFeet() << “ feet”;cout << x.getInches() << “ inches”;

//Bettercout << x;cin >> x; //that’s better also

Page 21: CSE 2341 - Honors Principles of Computer Science I

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

Page 22: CSE 2341 - Honors Principles of Computer Science I

22

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

Overloading << and >>

Page 23: CSE 2341 - Honors Principles of Computer Science I

23

istream& operator>> (istream& strm, FeetInches& obj){ cout << “Feet: “; strm >> obj.feet;

cout << “Inches: “; strm >> obj.inches;

obj.simplify();

return strm;}

Overloading << and >>

Page 24: CSE 2341 - Honors Principles of Computer Science I

Creating a String Class

24

Great example to demonstrate operator overloadingMyString 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.

Page 25: CSE 2341 - Honors Principles of Computer Science I

MyString

25

#ifndef MYSTRING_H#define MYSTRING_H

class MyString; //Forward Declarationostream& 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();

Page 26: CSE 2341 - Honors Principles of Computer Science I

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