<NOSCRIPT><A href="http://about.com/" target=_blank><IMG src="C++ Tutorial - Lesson 25 Operator Overloading_files/0.gif" border=0></A></NOSCRIPT>   About > Computing & Technology > C/C++ 
Search     
 Hi, I'm John Kopp, your guide to C, C++ and C# programming. This site provides what you need to know to learn C, C++ or C# programming. Browse the subjects on the left, try one of the tutorials or use the search box to get started.

C/C++

with John Kopp
Your Guide to one of hundreds of sites
 Home · Articles · Forums · Chat · Newsletters · Help    
Subjects

  ESSENTIALS
· C Tutorial
· C++ Tutorial
· C/C++ Glossary
· Tips
· More Tutorials
  BUYER'S GUIDE

Product Reviews
Top Picks
C Tutorials
C++ Tutorials
C#
Advanced C
Advanced C++
Beginning C
Beginning C++
Books
C++ Builder
Careers
CGI
CM
Dictionaries
CompilingDebugging
Freeware/Shareware
Humor
Magazines
OOAD
Polls
SoftwareEngineer'n
Style
STL
UNIX/GNU
Visual C++

Subject Library

All articles on this topic

 

Stay up-to-date!
Subscribe to our newsletter.

Web Hosting
Global Servers

 
 
 
Advertisement
> Free Credit Report
 
C++ Tutorial - Lesson 25: Operator Overloading, Part I
 More of this Feature
• Introduction.
• Simple Overloaded Operator, +
• Compound Overloaded Operator, +=
• Prefix Increment Operator, ++a
• Prefix Increment Operator, continued
• Postfix Increment Operator, a++
 
 Related Resources
• Lesson 24: "This" Pointer
• Lesson 15: Function Overloading
• Advanced C++ Tutorials
• Simple Solutions in C++
• C++ Programming Tips
• C++ Tutorial - See all lessons
 

by John Kopp

Welcome to About.com's tutorial on C++ programming. This lesson covers the topic of operator overloading. Operator overloading provides a way to define and use operators such as +, -, *, /, and [] for user defined types such as classes and enumerations. By defining operators, the use of a class can be made as simple and intuitive as the use of intrinsic data types.

Let's develop a "fraction" class to illustrate the use and utility of operator overloading. First, consider which operators are normally associated with fractions and should be included in this class. Operators such as +, -, *, / are givens. C++ allows any of its built in operators to be overloaded in a class, but only those that have an intuitive meaning for a particular class should actually be overloaded. For instance, although the modulo operator, %, could be overloaded for our fraction class, its meaning would be unclear and defining it would only confuse users of the class. %1/2 has no clear interpretation.

To begin, here is an implementation of the fraction class with an add method. Note that in order to simplify the implementation of the add method, I am using the greatest common denominator of the two fractions rather than the least.

#include <iostream>
using namespace std;

class Fraction {
public:
    Fraction(int num = 0, int den = 1)
    {
        this->num = num;
        this->den = den;
    }
    Fraction add(const Fraction &rhs)
    {
        Fraction temp;
        temp.den = this->den * rhs.den;
        temp.num = rhs.den * this->num +
            this->den * rhs.num;
        return temp;
    }
    void print()
    {
        cout << num << "/" << den << endl;
    }
private:
    int num;
    int den;
};

int main() {
    Fraction a(1,2);
    Fraction b(1,4);
    Fraction c;

    a.print();
    b.print();

    c = a.add(b);

    c.print();

    return 0;
}

Output:
1/2
1/4
6/8

Notice that a temporary Fraction object was created within the add method. This was necessary because "add" must return a Fraction, since its result is assigned to a Fraction. The use of the add method is somewhat awkward. Rather than a.add(b), we'd like to be able to write "a + b". This can be done with operator overloading, as shown on the following page.



Next page > Simple Overloaded Operators > Page 1, 2, 3, 4, 5, 6,


Subscribe to the C/C++ Newsletter
Name
Email






Email this page | Sign up for a Newsletter |
Explore More
Most Popular Articles
• C++ Programming Tutorial
• C Programming Tutorial
• C++ Tutorial - Lesson 12: File Input and Output
• C++ Tutorial - Lesson 4: Input and Output - cin, cout, iostr...
• C++ Tutorial - Lesson 10: Arrays and Vectors
• C++ Tutorial - Lesson 1: Writing and Compiling A First Progr...
• Tutorials
What's Hot Now
• Teach Yourself C++ in 21 Days by Jesse Liberty
• C Programming Tutorial
• C++ Tutorial - Lesson 25: Operator Overloading
• The C Programming Language, 2nd Edition by Kernighan and Rit...
• Topics in C++: Function Template Specialization
• C Programming Tips: Using Long Constants
• C Tutorial - Lesson 5: Conditional Processing, Part 1 - Rela...
About Us | Advertise on This Site | User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2004 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.