SlideShare a Scribd company logo
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
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
}
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.
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
}
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?
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;
}
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
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;
} };
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
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; } }
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;
}
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.
}
};
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 13
Operator Overloading
• can be
overloaded
• Cannot be
overloaded
+ - * / % ^
& | ~ ! , =
< > <= >= ++ --
<< >> == != && ||
+= -= /= %= ^= &=
|= *= <<= >>= [] ()
-> ->* new new [] delete delete []
:: .* . ?:
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.

More Related Content

PDF
R and cpp
PDF
Single qubit-gates operations
PDF
201801 CSE240 Lecture 12
PDF
Quill - 一個 Scala 的資料庫存取利器
PPTX
Synapse india dotnet development overloading operater part 4
PDF
Functional Programming with JavaScript
PDF
Why Grails?
PDF
뱅크샐러드 파이썬맛 레시피
R and cpp
Single qubit-gates operations
201801 CSE240 Lecture 12
Quill - 一個 Scala 的資料庫存取利器
Synapse india dotnet development overloading operater part 4
Functional Programming with JavaScript
Why Grails?
뱅크샐러드 파이썬맛 레시피

What's hot (17)

PDF
No More, No Less: A Formal Model for Serverless Computing
DOCX
Program pengurutan data
PDF
R and C++
PPT
Dynamically Evolving Systems: Cluster Analysis Using Time
PDF
How the Go runtime implement maps efficiently
PDF
Kapacitor Alert Topic handlers
PDF
Multi qubit entanglement
TXT
Script PyThon
PDF
Real World Optimization
PDF
Probability of finding a single qubit in a state
PPTX
Scott Anderson [InfluxData] | InfluxDB Tasks – Beyond Downsampling | InfluxDa...
PDF
The Ring programming language version 1.5.4 book - Part 62 of 185
PDF
The Ring programming language version 1.9 book - Part 75 of 210
PDF
Probabilistic data structures. Part 2. Cardinality
PDF
Regular expressions, Alex Perry, Google, PyCon2014
PDF
Standing the Test of Time: The Date Provider Pattern
No More, No Less: A Formal Model for Serverless Computing
Program pengurutan data
R and C++
Dynamically Evolving Systems: Cluster Analysis Using Time
How the Go runtime implement maps efficiently
Kapacitor Alert Topic handlers
Multi qubit entanglement
Script PyThon
Real World Optimization
Probability of finding a single qubit in a state
Scott Anderson [InfluxData] | InfluxDB Tasks – Beyond Downsampling | InfluxDa...
The Ring programming language version 1.5.4 book - Part 62 of 185
The Ring programming language version 1.9 book - Part 75 of 210
Probabilistic data structures. Part 2. Cardinality
Regular expressions, Alex Perry, Google, PyCon2014
Standing the Test of Time: The Date Provider Pattern
Ad

Similar to 201801 CSE240 Lecture 15 (20)

PDF
Basics _of_Operator Overloading_Somesh_Kumar_SSTC
PPTX
Object Oriented Programming using C++: Ch08 Operator Overloading.pptx
PPT
3d7b7 session4 c++
PDF
Ch-4-Operator Overloading.pdf
PDF
overloading in C++
PPT
Overloading
PPTX
Operator Overloading
PPTX
Week7a.pptx
PPT
14 operator overloading
PPT
Lec 26.27-operator overloading
PDF
Lec 8.pdf a
PPTX
PPTX
3. Polymorphism.pptx
PPT
Polymorphism
PPT
Lec 28 - operator overloading
PDF
Operator overloading
PDF
Unit3_OOP-converted.pdf
PPT
Operator overloading
PPTX
2CPP13 - Operator Overloading
PPT
Synapse india complain sharing info on chapter 8 operator overloading
Basics _of_Operator Overloading_Somesh_Kumar_SSTC
Object Oriented Programming using C++: Ch08 Operator Overloading.pptx
3d7b7 session4 c++
Ch-4-Operator Overloading.pdf
overloading in C++
Overloading
Operator Overloading
Week7a.pptx
14 operator overloading
Lec 26.27-operator overloading
Lec 8.pdf a
3. Polymorphism.pptx
Polymorphism
Lec 28 - operator overloading
Operator overloading
Unit3_OOP-converted.pdf
Operator overloading
2CPP13 - Operator Overloading
Synapse india complain sharing info on chapter 8 operator overloading
Ad

More from Javier Gonzalez-Sanchez (20)

PDF
201804 SER332 Lecture 01
PDF
201801 SER332 Lecture 03
PDF
201801 SER332 Lecture 04
PDF
201801 SER332 Lecture 02
PDF
201801 CSE240 Lecture 26
PDF
201801 CSE240 Lecture 25
PDF
201801 CSE240 Lecture 24
PDF
201801 CSE240 Lecture 23
PDF
201801 CSE240 Lecture 22
PDF
201801 CSE240 Lecture 21
PDF
201801 CSE240 Lecture 20
PDF
201801 CSE240 Lecture 19
PDF
201801 CSE240 Lecture 18
PDF
201801 CSE240 Lecture 17
PDF
201801 CSE240 Lecture 16
PDF
201801 CSE240 Lecture 14
PDF
201801 CSE240 Lecture 13
PDF
201801 CSE240 Lecture 11
PDF
201801 CSE240 Lecture 10
PDF
201801 CSE240 Lecture 09
201804 SER332 Lecture 01
201801 SER332 Lecture 03
201801 SER332 Lecture 04
201801 SER332 Lecture 02
201801 CSE240 Lecture 26
201801 CSE240 Lecture 25
201801 CSE240 Lecture 24
201801 CSE240 Lecture 23
201801 CSE240 Lecture 22
201801 CSE240 Lecture 21
201801 CSE240 Lecture 20
201801 CSE240 Lecture 19
201801 CSE240 Lecture 18
201801 CSE240 Lecture 17
201801 CSE240 Lecture 16
201801 CSE240 Lecture 14
201801 CSE240 Lecture 13
201801 CSE240 Lecture 11
201801 CSE240 Lecture 10
201801 CSE240 Lecture 09

Recently uploaded (20)

PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PPTX
ai tools demonstartion for schools and inter college
PDF
AI in Product Development-omnex systems
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
Digital Strategies for Manufacturing Companies
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PPTX
Operating system designcfffgfgggggggvggggggggg
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PPTX
history of c programming in notes for students .pptx
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PPTX
Transform Your Business with a Software ERP System
Which alternative to Crystal Reports is best for small or large businesses.pdf
Design an Analysis of Algorithms I-SECS-1021-03
How to Choose the Right IT Partner for Your Business in Malaysia
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
VVF-Customer-Presentation2025-Ver1.9.pptx
ai tools demonstartion for schools and inter college
AI in Product Development-omnex systems
PTS Company Brochure 2025 (1).pdf.......
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Odoo POS Development Services by CandidRoot Solutions
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Digital Strategies for Manufacturing Companies
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Operating system designcfffgfgggggggvggggggggg
CHAPTER 2 - PM Management and IT Context
Design an Analysis of Algorithms II-SECS-1021-03
history of c programming in notes for students .pptx
2025 Textile ERP Trends: SAP, Odoo & Oracle
Transform Your Business with a Software ERP System

201801 CSE240 Lecture 15

  • 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.