The document discusses function overloading and operator overloading in C++. It provides examples of overloading functions and operators like +, -, > to perform operations on user-defined data types like cylinders and days of the week. Function overloading allows multiple functions to have the same name but different parameters, while operator overloading allows operators to be redefined to work with user-defined types. The examples demonstrate overloading functions and operators to add, subtract and compare cylinder and day objects.
1. CSE240 – Introduction to
Programming Languages
Lecture 15:
Programming with C++ | Overloading
Javier Gonzalez-Sanchez
javiergs@asu.edu
javiergs.engineering.asu.edu
Office Hours: By appointment
2. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 2
Function Overloading
• Multiple functions can have the same name, but different parameter list in
type or in number.
• Destructor cannot be overloaded, because it does not have parameter
void function(int v) {
// code
}
void function(double v) {
// code
}
3. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 3
Overloaded Functions vs. Virtual Functions
• Multiple functions can have the same name, but different parameter list in
type or in number.
• Overloading can be applied to constructors and normal functions.
• Overloading does not allow functions to have the same parameter list but
different return type.
• Virtual functions in the base class and derived class must have the same
parameter list and return type.
4. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 4
Overloaded Functions vs. Virtual Functions
• Multiple functions can have the same name, but different parameter list in
type or in number.
• Overloading can be applied to constructors and normal functions.
• Overloading does not allow functions to have the same parameter list but
different return type.
• Virtual functions in the base class and derived class must have the same
parameter list and return type.
void enqueue(int v) {
if (rear < queue_size) buffer[rear++] = v;
else if (compact()) buffer[rear++] = v;
}
void enqueue(double v) {
if (rear < queue_size) buffer[rear++] = v;
else if (compact()) buffer[rear++] = v;
}
virtual void display() {
// different implementations in different classes
}
5. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 5
Operator Overloading in C++
Like function overloading, C++ allows user to define operator (built-in function)
overloading.
Why do we need operator overloading?
• string1 = string2; instead of using strcpy(string1, string2);
• string1 >= string2; instead of using strcmp(string1, string2);
• rectangleArea(3, 5) < rectangleArea(2, 6)
• time1(3, 23) + time2(5, 56), resulting in: time3(9, 19)
• Increament a Date(year, month, day) object, what is the next date?
6. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 6
Example 1
int main() {
Cylinder cylinder1, cylinder2, cylinder3;
double volume = 0.0;
// cylinder1 and cylinder2 initialization
cylinder1.setRadius(5.0); cylinder1.setHeight(5.0);
cylinder2.setRadius(4.0); cylinder2.setHeight(10.0);
// get and print volumes of cylinder1 and cylinder2
volume = cylinder1.getVolume();
cout << "Volume of cylinder1 : " << volume << endl;
volume = cylinder2.getVolume();
cout << "Volume of cylinder2 : " << volume << endl;
// Add two objects using overloaded operator +, and get and print volume
cylinder3 = cylinder1 + cylinder2;
volume = cylinder3.getVolume();
cout << "Volume of cylinder3 : " << volume << endl;
// Subtract two object as follows:
cylinder3 = cylinder1 - cylinder2;
// get and print volume of cylinder 3
volume = cylinder3.getVolume();
cout << "Volume of cylinder3 : " << volume << endl;
if (cylinder1 > cylinder2) // using overloaded operator >
cout << "cylinder1 volume is greater than cylinder2 volume" << endl;
else cout << "cylinder1 volume is not greater than cylinder2 volume" << endl;
return 0;
}
7. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 7
Example 1
#include <iostream>
#include <cmath>
using namespace std;
class Cylinder {
private:
double radius, height;
public:
double getVolume(void) {
// M_PI defined in <cmath>
return M_PI * radius * radius * height;
}
void setRadius(double r) {
radius = r;
}
void setHeight(int h) {
height = h;
}
radius
height
8. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 8
Example 1
// Overload + operator to add two Cylinder objects.
Cylinder operator+(Cylinder &c) {
Cylinder cylinder;
cylinder.radius = this->radius + c.radius;
cylinder.height = this->height + c.height;
return cylinder;
}
// Overload - operator to subtract two Cylinder objects.
Cylinder operator-(Cylinder &c) {
Cylinder cylinder;
cylinder.radius = this->radius - c.radius;
cylinder.height = this->height - c.height;
return cylinder;
}
// Overload - operator > (greater than of two Cylinder objects).
bool operator>(Cylinder &c) {
Cylinder cylinder; double vol0, vol1;
vol0 = this->getVolume();
vol1 = c.getVolume();
if (vol0 > vol1) return true;
else return false;
} };
9. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 9
int main() {
Days day1(Mon), day2, day3;
day2.setDay(Sat); day3.setDay(Sun);
cout << "The days before ++ operations" << endl;
day1.display(); day2.display(); day3.display();
++day1; ++day2; ++day3;
cout << "The days after prefix ++ operations" << endl;
day1.display(); day2.display();
day3.display();
day1++; day2++; day3++;
cout << "The days after postfix ++ operations" << endl;
day1.display(); day2.display(); day3.display();
return 0;
}
Example 2
10. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 10
Example 2
#include <iostream>
using namespace std;
typedef enum { Sun = 0, Mon, Tue, Wed, Thu, Fri, Sat } DayType;
class Days {
private:
DayType day;
public:
Days() { day = Sun; } // constructor without parameter
Days(DayType d) { day = d; } // constructor with a parameter
DayType getDay(void) { return day; }
void setDay(DayType d) { if (d >= Sun && d <= Sat) this->day = d;}
void display() {
switch (day) { case Sun: cout << "Sun" << endl; break;
case Mon: cout << "Mon" << endl; break;
case Tue: cout << "Tue" << endl; break;
case Wed: cout << "Wed" << endl; break;
case Thu: cout << "Thu" << endl; break;
case Fri: cout << "Fri" << endl; break;
case Sat: cout << "Sat" << endl; break;
default: cout << "Incorrect day" << endl; } }
11. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 11
Example 2
// Overload prefix ++ operator to add one to Days object: ++days.
Days operator++() {
Days days(day); // Save the original value
switch (this->day) {
case Sun: this->day = Mon; break;
case Mon: this->day = Tue; break;
case Tue: this->day = Wed; break;
case Wed: this->day = Thu; break;
case Thu: this->day = Fri; break;
case Fri: this->day = Sat; break;
case Sat: this->day = Sun; break;
default: cout << "Incorrect day" << endl;
}
days.day = this->day;
return days;
}
12. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 12
Example 2
// Overload postfix ++ operator to add one to Days object: days++.
Days operator++(int) { // This parameter indicates ++ follows a parameter
Days days(day); // Save the original value
switch (this->day) {
case Sun: this->day = Mon; break;
case Mon: this->day = Tue; break;
case Tue: this->day = Wed; break;
case Wed: this->day = Thu; break;
case Thu: this->day = Fri; break;
case Fri: this->day = Sat; break;
case Sat: this->day = Sun; break;
default: cout << "Incorrect day" << endl;
} // The value in the this object has been changed.
// days.day = this->day;
return days; // return the value before the changes.
}
};
13. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 13
Operator Overloading
• can be
overloaded
• Cannot be
overloaded
+ - * / % ^
& | ~ ! , =
< > <= >= ++ --
<< >> == != && ||
+= -= /= %= ^= &=
|= *= <<= >>= [] ()
-> ->* new new [] delete delete []
:: .* . ?:
14. CSE240 – Introduction to Programming Languages
Javier Gonzalez-Sanchez
javiergs@asu.edu
Fall 2017
Disclaimer. These slides can only be used as study material for the class CSE240 at ASU. They cannot be distributed or used for another purpose.