// Example Program.
class RaceEvent
{
public:
RaceEvent() {}
RaceEvent(int distance) : _distance(distance) {}
void setDistance(int distance);
int getDistance() const { return _distance; }
private:
int _distance;
};
void RaceEvent::setDistance(int distance)
{
_distance = distance;
}
// Example declarations of overloaded operators
const RaceEvent operator +(const RaceEvent& R1, const RaceEvent& R2);
const RaceEvent operator -(const RaceEvent& R1, const RaceEvent& R2);
bool operator ==(const RaceEvent& R1, const RaceEvent& R2);
bool operator <=(const RaceEvent& R1, const RaceEvent& R2);
// Example definitions of overloaded operators
const RaceEvent operator +(const RaceEvent& R1, const RaceEvent& R2)
{
RaceEvent temp;
temp.setDistance(R1.getDistance() + R2.getDistance());
return temp;
}
const RaceEvent operator -(const RaceEvent& R1, const RaceEvent& R2)
{
return RaceEvent(R1.getDistance() - R2.getDistance());
}
bool operator ==(const RaceEvent& R1, const RaceEvent& R2)
{
return (R1.getDistance() == R2.getDistance());
}
bool operator <=(const RaceEvent& R1, const RaceEvent& R2)
{
return (R1.getDistance() <= R2.getDistance());
}
// Example driver code that tests the overloaded operators
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
RaceEvent race1(100), race2(400), race3;
int meters;
do
{
cout << "\nRace 1 = " << race1.getDistance()
<< "\nRace 2 = " << race2.getDistance() << endl;
if(race1 == race2)
cout << "Race 1 and Race 2 are the same distance.\n";
else
cout << "Race 1 and Race 2 difference is "
<< abs((race2 - race1).getDistance())
<< " meters.\n";
race3 = race1 + race2;
cout << "Race 1 and Race 2 sum up to "
<< race3.getDistance() << " meters.\n";
cout << "\nEnter new distance for Race 1: ";
cin >> meters;
race1.setDistance(meters);
} while (race1 <= race2);
return 0;
}
// Output of a sample session
Race 1 = 100
Race 2 = 400
Race 1 and Race 2 difference is 300 meters.
Race 1 and Race 2 sum up to 500 meters.
Enter new distance for Race 1: 300
Race 1 = 300
Race 2 = 400
Race 1 and Race 2 difference is 100 meters.
Race 1 and Race 2 sum up to 700 meters.
Enter new distance for Race 1: 400
Race 1 = 400
Race 2 = 400
Race 1 and Race 2 are the same distance.
Race 1 and Race 2 sum up to 800 meters.
Enter new distance for Race 1: 401
Press any key to continue
// Example. The following is legal and changes
// the anonymous object that is created from
// (race1 + race2).
(race1 + race2).setDistance(1000);
// If the code were changed to the following,
// then the 1000 would override the sum of
// race1 + race2. See the
blue italics text for// the differences between the code below and the
// example above.
class RaceEvent
{
public:
RaceEvent() {}
RaceEvent(int distance) : _distance(distance) {}
int setDistance(int distance);
int getDistance() const { return _distance; }
private:
int _distance;
};
int
RaceEvent::setDistance(int distance){
_distance = distance;
return _distance;
}
// From main()
do
{
cout << "\nRace 1 = " << race1.getDistance()
<< "\nRace 2 = " << race2.getDistance() << endl;
if(race1 == race2)
cout << "Race 1 and Race 2 are the same distance.\n";
else
cout << "Race 1 and Race 2 difference is "
<< abs((race2 - race1).getDistance())
<< " meters.\n";
race3 = race1 + race2;
cout << "Race 1 and Race 2 sum up to "
<< race3.getDistance() << " meters.\n";
// Look how I can mess up the results of race3 !!!
cout << endl;
cout << "Below is an Error for not returning by const\n";
race3.setDistance((race1 + race2).setDistance(10000));
cout << "Race 1 and Race 2 sum up to "
<< race3.getDistance() << " meters.\n";
cout << "\nEnter new distance for Race 1: ";
cin >> meters;
race1.setDistance(meters);
} while (race1 <= race2);
// Output of a sample session
Race 1 = 100
Race 2 = 400
Race 1 and Race 2 difference is 300 meters.
Race 1 and Race 2 sum up to 500 meters.
Below is an Error for not returning by const
Race 1 and Race 2 sum up to 10000 meters.
Enter new distance for Race 1: 200
Race 1 = 200
Race 2 = 400
Race 1 and Race 2 difference is 200 meters.
Race 1 and Race 2 sum up to 600 meters.
Below is an Error for not returning by const
Race 1 and Race 2 sum up to 10000 meters.
Enter new distance for Race 1: 401
Press any key to continue
// Example rewritten.
class RaceEvent
{
public:
// Constructors
RaceEvent();
RaceEvent(int distance);
// Accessor and Mutator Function
void setDistance(int distance);
int getDistance() const;
// Operators
const RaceEvent operator +(const RaceEvent& R2) const;
const RaceEvent operator -(const RaceEvent& R2) const;
bool operator ==(const RaceEvent& R2) const;
bool operator <=(const RaceEvent& R2) const;
private:
int _distance;
};
// Constructor
RaceEvent::RaceEvent()
{}
// Constructor
RaceEvent::RaceEvent(int distance) : _distance(distance)
{}
// Mutator Function
void RaceEvent::setDistance(int distance)
{
_distance = distance;
}
// Accessor Function
int RaceEvent::getDistance() const
{
return _distance;
}
// Example definitions of overloaded operators
const RaceEvent RaceEvent::operator +(const RaceEvent& R2) const
{
return RaceEvent(_distance + R2.getDistance());
}
const RaceEvent RaceEvent::operator -(const RaceEvent& R2) const
{
return RaceEvent(_distance - R2.getDistance());
}
bool RaceEvent::operator ==(const RaceEvent& R2) const
{
return (_distance == R2.getDistance());
}
bool RaceEvent::operator <=(const RaceEvent& R2) const
{
return (_distance <= R2.getDistance());
}
// Example driver that overloaded operators
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
RaceEvent race1(100), race2(400), race3;
int meters;
do
{
cout << "\nRace 1 = " << race1.getDistance()
<< "\nRace 2 = " << race2.getDistance() << endl;
if(race1 == race2)
cout << "Race 1 and Race 2 are the same distance.\n";
else
cout << "Race 1 and Race 2 difference is "
<< abs((race2 - race1).getDistance())
<< " meters.\n";
race3 = race1 + race2;
cout << "Race 1 and Race 2 sum up to "
<< race3.getDistance()
<< " meters.\n";
cout << "\nEnter new distance for Race 1: ";
cin >> meters;
race1.setDistance(meters);
} while (race1 <= race2);
return 0;
}
// Output of a sample session
Race 1 = 100
Race 2 = 400
Race 1 and Race 2 difference is 300 meters.
Race 1 and Race 2 sum up to 500 meters.
Enter new distance for Race 1: 150
Race 1 = 150
Race 2 = 400
Race 1 and Race 2 difference is 250 meters.
Race 1 and Race 2 sum up to 550 meters.
Enter new distance for Race 1: 400
Race 1 = 400
Race 2 = 400
Race 1 and Race 2 are the same distance.
Race 1 and Race 2 sum up to 800 meters.
Enter new distance for Race 1: 401
Press any key to continue
// Example. The example is rewritten again using friend functions.
// See the changes in
blue italics below. All other code is the same.class RaceEvent
{
public:
// Constructors
RaceEvent();
RaceEvent(int distance);
// Accessor and Mutator Function
void setDistance(int distance);
int getDistance() const;
// Operators
friend const RaceEvent operator +(const RaceEvent& R1, const RaceEvent& R2);
friend const RaceEvent operator -(const RaceEvent& R1, const RaceEvent& R2);
friend bool operator ==(const RaceEvent& R1, const RaceEvent& R2);
friend bool operator <=(const RaceEvent& R1, const RaceEvent& R2);
private:
int _distance;
};
// Example of friend functions. Note that the friend functions have
// direct access to the private member data of the object parameters.
const RaceEvent operator +(const RaceEvent& R1, const RaceEvent& R2)
{
RaceEvent temp;
// Friend function can access the private parameters.
temp._distance = R1._distance + R2._distance;
return temp;
}
const RaceEvent operator -(const RaceEvent& R1, const RaceEvent& R2)
{
// Creates and returns an anonymous object.
return RaceEvent(R1._distance - R2._distance);
}
bool operator ==(const RaceEvent& R1, const RaceEvent& R2)
{
return (R1._distance == R2._distance);
}
bool operator <=(const RaceEvent& R1, const RaceEvent& R2)
{
return (R1._distance <= R2._distance);
}
// Output of a sample session
Race 1 = 100
Race 2 = 400
Race 1 and Race 2 difference is 300 meters.
Race 1 and Race 2 sum up to 500 meters.
Enter new distance for Race 1: 211
Race 1 = 211
Race 2 = 400
Race 1 and Race 2 difference is 189 meters.
Race 1 and Race 2 sum up to 611 meters.
Enter new distance for Race 1: 401
Press any key to continue
// Example
#include <iostream>
using namespace std;
int main()
{
int a = 1, c = 2;
int& b = a;
cout << a << " " << b << " " << c << endl;
b = c;
cout << a << " " << b << " " << c << endl;
a = 5;
cout << a << " " << b << " " << c << endl;
return 0;
}
// Output. Note: b is always a reference (alias) to
// a throughout the program. b must be initialized
// in the declaration or there will be a compiler
// error. That is, all references must be
// initialized in the declaration. You could not
// have the following:
int& b; // ERROR!
b = a;
// However, this is valid
int& b = a;
// do some stuff
b = c; // b, alias to a, now equals c
// do some stuff
1 1 2
2 2 2
5 5 2
// Example
#include <iostream>
using namespace std;
int& fun(int& num)
{
return num;
}
int main()
{
int a = 100;
cout << a << endl;
cout << fun(a) << endl;
fun(a) = 5; // Return by reference allows LHS of =
cout << a << endl;
cout << fun(a) << endl;
return 0;
}
// Output
100
100
5
5
//Example
Money amount;
...
cout << "The amount is " << amount << endl;
...
cout << "Enter new amount: ";
cin >> amount;
// Example
int num1 = 50, num2 = 100;
int *p1 = &num1, *p2;
p2 = &num2;
// Example
cout << "num1 = " << num1 << endl;
cout << "num1 = " << *p1 << endl;
cout << "num2 = " << num2 << endl;
cout << "num2 = " << *p2 << endl;
num1 = 10;
*p2 = 20;
cout << "num1 = " << num1 << endl;
cout << "num1 = " << *p1 << endl;
cout << "num2 = " << num2 << endl;
cout << "num2 = " << *p2 << endl;
// Output
num1 = 50
num1 = 50
num2 = 100
num2 = 100
num1 = 10
num1 = 10
num2 = 20
num2 = 20
Press any key to continue
// Example
int num1 = 50, num2 = 100;
int *p1 = &num1, *p2 = &num2;
cout << "num1 = " << num1 << endl;
cout << "num1 = " << *p1 << endl;
cout << "num2 = " << num2 << endl;
cout << "num2 = " << *p2 << endl;
p1 = p2;
cout << "num1 = " << num1 << endl;
cout << "num1 = " << *p1 << endl;
cout << "num2 = " << num2 << endl;
cout << "num2 = " << *p2 << endl;
// Output
num1 = 50
num1 = 50
num2 = 100
num2 = 100
num1 = 50
num1 = 100
num2 = 100
num2 = 100
Press any key to continue
// Example
double *pRate;
pRate = new double;
*pRate = 0.0675;
cout << "The rate = " << (*pRate * 100) << "%\n";
// Output
The rate = 6.75%
Press any key to continue
// Example
class Person
{ };
char *pName = new char;
int *pNumbers = new int[50];
Person *pPerson = new Person;
// Do some stuff until and then free the memory
delete pName; // pName becomes undefined after delete
delete [] pNumbers; // Must use [] for an array
delete pPerson;
The
-> operator is used to combine the actions of the indirection operator and the dot operator to simplify the way we indirectly access members of a structure or class object that has been dynamically allocated.// Example
class Record
{
public:
int num;
};
Record *p;
p = new Record;
(*p).num = 5;
cout << "The number = " << (*p).num << endl;
p->num = 10;
cout << "The number = " << p->num << endl;
delete p;
// Output
The number = 5
The number = 10
Press any key to continue
#include <iostream>
using namespace std;
int main()
{
class Record
{
public:
int getNum1() const { return this->_num1; }
int getNum2() const { return _num2; }
void setNum1(int num1) { _num1 = num1; }
void setNum2(int num2) { this->_num2 = num2; }
private:
int _num1;
int _num2;
};
Record *p;
p = new Record;
p->setNum1(75);
p->setNum2(300);
cout << "The first number = " << p->getNum1() << endl;
cout << "The second number = " << p->getNum2() << endl;
delete p;
return 0;
}
// Output
The first number = 75
The second number = 300
Press any key to continue
class StringClass
{
public:
...
void showStuff() const;
...
StringClass& operator=(const StringClass& rtSide);
...
private:
char *a; // Dynamic array for characters in the string
int capacity; // Size of the dynamic array
int length; // Number of characters in a
};
StringClass& StringClass::operator=(const StringClass& rtSide)
{
if(this == &rtSide) // Right is same as the Left side
return *this;
else
{
capacity = rtSide.capacity;
length= rtSide.length
delete [] a;
a = new char[capacity];
for(int i =0; i < length; i++)
a[i] = rtSide.a[i];
return *this;
}
}
Note: When using the this pointer in the overloaded assignment operator, a return by reference is made. You will probably find that most books will not return by reference for the overloaded assignment operator. Our book does return by reference. What is the added benefit of returning by reference? The added benefit is that return by reference allows you to invoke a member function with the value returned. For example,
StringClass A, B, C;
// Do some stuff (pretend that the C object gets filled in)
// Below will work whether or not we return by reference
A = C;
// Below will work if, and only if, we return by reference
(B = C).showStuff();
Note: In this example, the calling objects are the A and B objects. The C object, in both cases, is the operand (rtSide) of the overloaded assignment operator.
// Example
StringClass::StringClass(const StringClass& rtSide)
{
capacity = rtSide.capacity;
length = rtSide.length
a = new char[capacity];
for(int i =0; i < length; i++)
a[i] = rtSide.a[i];
}
StringClass A;
// Do some stuff (pretend that the A object gets filled in)
StringClass B(A); // This will perform a deep copy