Professional Documents
Culture Documents
Topic6 PDF
Topic6 PDF
Topic6 PDF
Operator Overloading
Topic #6
CS202 6- 1
CS202 6- 2
CS202 6- 3
Copy Constructors
CS202 6- 4
Shallow Copy:
The data members of one object are copied into the
data members of another object without taking any
dynamic memory pointed to by those data members
into consideration. (memberwise copy)
Deep Copy:
Any dynamic memory pointed to by the data
members is duplicated and the contents of that
memory is copied (via copy constructors and
assignment operators -- when overloaded)
Copy Constructors
CS202 6- 5
Copy Constructors
CS202 6- 6
Copy Constructors
smith
ptr
length=10
clone
S
u
e
S
m
i
t
h
'\0'
CS202 6- 7
ptr
length=10
Copy Constructors
CS202 6- 8
Copy Constructors
//name.h interface
class name {
public:
name(char* = "");
//default constructor
name(const name &);
//copy constructor
~name();
//destructor
name &operator=(name &); //assignment op
private:
char* ptr; //pointer to name
int length; //length of name including nul char
};
#include "name.h"
//name.c implementation
name::name(char* name_ptr) { //constructor
length = strlen(name_ptr); //get name length
ptr = new char[length+1]; //dynamically allocate
strcpy(ptr, name_ptr);
//copy name into new space
}
name::name(const name &obj) { //copy constructor
length = obj.length;
//get length
ptr = new char[length+1]; //dynamically allocate
strcpy(ptr, obj.ptr);
//copy name into new space
}
CS202 6- 9
Copy Constructors
CS202 6- 10
clone
S
u
S
u
S
m
i
t
h
'\0'
S
m
i
t
h
'\0'
ptr
length=10
Copy Constructors
int main() {
name smith("Sue Smith"); //constructor with arg used
CS202 6- 11
Copy Constructors
smith
Using a copy
constructor avoids
objects sharing
memory -- but causes
this behavior
S
u
e
S
m
i
t
h
'\0'
CS202 6- 12
ptr
length=10
program stack
return address
ptr
S
u
e
S
m
i
t
h
'\0'
call by value
length=10
S
u
e
S
m
i
t
h
'\0'
ptr
length=10
return by value
Copy Constructors
CS202 6- 13
ptr
length=10
program stack
return address
reference
call by reference
reference
return by reference
Introduction to C++
Operator
Overloading
CS202 6- 14
CS202 6- 15
Operator Overloading:
Allows us to define the behavior of operators when
applied to objects of a class
Examine what operators make sense for a new data
type we are creating (think about data abstraction
from last lecture) and implement those that make
sense as operators:
input_data is replaced by >>
display is replaced by <<
assign or copy is replaced by =
Operator Overloading
CS202 6- 16
Operator Overloading
CS202 6- 17
Operator Overloading
CS202 6- 18
Return type
CS202 6- 19
Function name
Formal arguments
Operator Overloading
CS202 6- 20
Operator Overloading
CS202 6- 21
Operator Overloading
CS202 6- 22
As Non-members
CS202 6- 23
Operator Overloading
CS202 6- 24
=
[]
()
->
->*
assignment operator
subscript operator
function call operator
indirect member access operator
indirect pointer to member access operator
Guidelines:
CS202 6- 25
Guidelines:
CS202 6- 26
Guidelines:
CS202 6- 27
Guidelines: (example)
The operand
is not modified
CS202 6- 28
Second operand
is not modified
Second
operand
First operand
is not modified
Efficiency Considerations
CS202 6- 29
Efficiency Considerations
CS202 6- 30
Efficiency Considerations
CS202 6- 31
Introduction to C++
Building
a Class
CS202 6- 32
CS202 6- 33
Overloading = Operators
CS202 6- 34
Overloading = Operator
class string {
public:
string(): str(0), len(0) {}; //constructor
string(const string &); //copy constructor
~string();
//destructor
string & operator = (const string & ); //assignment
private:
char * str;
int len;
};
string & operator = (const string & s2) {
if (this == &s2) //check for self assignment
return *this;
if (str)
//current object has a value
delete [] str; //deallocate any dynamic memory
str = new char [s2.len+1];
strcpy(str,s2.str);
len = s2.len;
return *this;
}
CS202 6- 35
CS202 6- 36
CS202 6- 37
cou t
operan d!
CS202 6- 38
private:
char * str;
int len;
};
istream & operator >> (istream &in, string &s) {
char temp[100];
in >>temp; //or, should this could be in.get?!
s.len = strlen(temp);
s.str = new char[s.len+1];
strcpy(s.str, temp);
return in;
}
ostream & operator << (ostream &o, const string& s){
o << s.str; //notice no additional whitespace sent....
return o;
CS202 6- 39
Overloading +, +=Operators
CS202 6- 40
Overloading +, += Operators
class string {
public:
explicit string (char *); //another constructor
friend string operator + (const string &, char *);
friend string operator + (char *, const string &);
friend string operator + (const string&, const string&);
string & operator += (const string &);
string & operator += (char *);
};
string operator + (const string &s, char *lit) {
char * temp = new char[s.len+strlen(lit)+1];
strcpy(temp, s.str);
strcat(temp, lit);
return string(temp);
}
CS202 6- 41
Overloading +, += Operators
class string {
public:
explicit string (char *); //another constructor
friend string operator + (const string &, char *);
friend string operator + (char *, const string &);
friend string operator + (const string&, const string&);
string & operator += (const string &);
string & operator += (char *);
};
string operator + (const string &s,const string &s2) {
char * temp = new char[s.len+s2.len+1];
strcpy(temp, s.str);
strcat(temp, s2.str);
return string(temp); //makes a temporary object
}
string & string::operator += (const string & s2) {
len += s2.len;
char * temp = new char[len+s2.len+1];
strcpy(temp, str);
strcat(temp, s2.str);
str = temp; //copy over the pointer
return *this; //just copying an address
}
CS202 6- 42
Overloading +, += Operators
CS202 6- 43
Overloading +, += Operators
CS202 6- 44
Relational/Equality Operators
CS202 6- 45
Relational/Equality Operators
class string {
public:
friend bool operator < (const string &, char *);
friend bool operator < (char *, const string &);
friend bool operator < (const string &, const string &);
friend bool operator <= (const string &, char *);
friend bool operator <= (char *, const string &);
friend bool operator <= (const string &,const string &);
friend bool operator > (const string &, char *);
friend bool operator > (char *, const string &);
friend bool operator > (const string &, const string &);
friend bool operator >= (const string &, char *);
friend bool operator >= (char *, const string &);
friend bool operator >= (const string &,const string &);
friend bool operator != (const string &, char *);
friend bool operator != (char *, const string &);
friend bool operator != (const string &,const string &);
friend bool operator == (const string &, char *);
friend bool operator == (char *, const string &);
friend bool operator == (const string &,const string &);
CS202 6- 46
Relational/Equality Operators
bool operator < (const string & s1, char * lit) {
return (strcmp(s1.str, lit) < 0);
}
bool operator < (const string & s1, const string & s2) {
return (strcmp(s1.str, s2.str) < 0);
}
or,
bool operator >= (char * lit, const string & s1) {
return (s1 < lit);
}
Which is better?
CS202 6- 47
Overloading [] Operator
CS202 6- 48
Overloading [] Operator
class string {
public:
char & operator [] (int) const;
};
char & string::operator [] (int index) const {
return str[index];
}
checking
provide access to temporary memory to
ensure the private nature of strs memory.
CS202 6- 49
CS202 6- 50
CS202 6- 52
CS202 6- 53
Introduction to C++
A List
Data Type
CS202 6- 54
Class Interface
class node;
//node declaration
class list { //list.h
public:
list(): head(0){}
list (const list &);
~ list();
list & operator = (const list &);
friend ostream & operator << (ostream &, const list &);
friend istream & operator >> (istream &, list &);
friend list operator + (const list &, const list &);
friend list operator + (const list &, const string &);
friend list operator + (const string &, const list &);
list & operator += (const list &);
list & operator += (const string &);
bool operator == (const list &) const;
bool operator != (const list &) const;
string & operator [] (int) const;
string & operator ++ (); //prefix
string operator ++ (int); //postfix
private:
node*head,*ptr,*tail;//discussproscons
};
CS202 6- 56
Copy Constructor
//List Class Implementation file: list.c
class node {
string obj;
node * next;
};
//node definition
CS202 6- 57
Assignment Operator
list & list::operator = (const list & l) {
if (this == &l) return *this; //why not *this == l?
//If there is a list, destroy it
node * current;
while (head) {
current = head->next;
delete head;
head = current;
}
if (!l.head)
head = ptr = tail = NULL;
else {
head = new node;
head->obj = l.head->obj;
CS202 6- 58
Destructor, Insertion
list::~list() {
node * current;
while (head) {
current = head->next;
delete head; //what does this do?
head = current;
}
ptr = tail = NULL;
}
ostream & operator << (ostream & out, const list & l) {
node * current = l.head; //how can it access head?
while (current) {
out <<current->obj<<;//whatdoesthisdo?
current = current->next;
}
return out;
}
CS202 6- 59
>> Operators
CS202 6- 60
>> Operators
istream & operator >> (istream & in, list & l) {
node * current = l.tail;
if (!current) {
//empty list starting out
l.head = current = new node;
in >>l.head->obj;
l.tail = l.ptr = l.head;
l.head->next = NULL;
}
node * savelist = l.tail->next;
char next_char;
while((next_char=in.peek())!=\n&&
next_char != EOF) {
current->next = new node;
current = current->next;
in >>current->obj; //what does this do?
}
current->next = savelist; ptr = current;
if (!savelist) l.tail = current;
return in;
}
CS202 6- 61
+ Operators
list operator + (const list & l1, const list & l2) {
//remember, neither l1 nor l2 should be modified!
list temp(l1); //positions tail at the end of l1
temp += l2;
//how efficient is this?
return temp;
}
list operator + (const list & l1, const list & l2) {
list temp(l1); //positions tail at the end of l1
if (!temp.head) temp = l2;
else {
node * dest = temp.tail;
node * source = l2.head;
while (source) {
dest->next = new node;
dest = dest->next;
dest->obj = source->obj;
source = source->next;
}
dest->next = NULL; temp.tail = dest;
temp.ptr = temp.head;
} return temp;
}
CS202 6- 62
+= Operators
list & list::operator += (const list & l2) {
//whywouldntweprogramthisway?
*this = *this + l2;
return *this;
}
Or, would it be better to do the following?
== and != Operators
Notice why a first and second shouldnt be data
members:
bool list::operator == (const list & l2) const {
node * first = head;
node * second = l2.head;
while (first && second && first->obj == second->obj) {
first = first->next;
second = second->next;
}
if (first || second) return FALSE;
return TRUE;
}
[] Operator
string & list::operator [] (int index) const {
node * current = head;
for (int i=0; i< index && current; i++)
current = current->next;
if (!current) {
//consider what other alternatives there are
string * temp = new string; //just in case
return *temp;
}
return current->obj;
}
CS202 6- 65