Enter your Sign on user name and password.

Forgot password?
Sign In | Subscribe
Start learning today, and be successful in your academic & professional career. Start Today!
Loading video...
This is a quick preview of the lesson. For full access, please Log In or Sign up.
For more information, please see full course syllabus of Intermediate C++
  • Discussion

  • Study Guides

  • Download Lecture Slides

  • Table of Contents

  • Related Services

Start Learning Now

Our free lessons will get you started (Adobe Flash® required).
Get immediate access to our entire library.

Sign up for Educator.com

Membership Overview

  • Unlimited access to our entire library of courses.
  • Search and jump to exactly what you want to learn.
  • *Ask questions and get answers from the community and our teachers!
  • Practice questions with step-by-step solutions.
  • Download lesson files for programming and software training practice.
  • Track your course viewing progress.
  • Download lecture slides for taking notes.
  • Learn at your own pace... anytime, anywhere!

Operator Overloading

  • Implement the following overloaded operators to each of the classes created in the previous lesson:
    • = (assignment)
    • == (equality) for the “Auto” class, compare each location to within some margin of error such as 0.001. Decide on an attribute important enough for comparison for the other classes
    • != (inequality) opposite of (==)
    • << (output to stream)
      Decide whether to use the “Print” method you should have already written or output in a different format, such as a format that can be parsed easily when input using (>>).
  • Use the assignment operator to copy your “Auto” objects into several instances.
    • Create a program that loops in a similar fashion to the previous lesson, except each Auto will be updated in each loop, and each Auto will have slightly different pedal pressures so they will travel at different speeds.
    • At each loop iteration, compare the locations of each auto to each of the others, and if any two are in the same location, print out a message to that effect
    • Design test loops such that you will know in advance which two autos should pass each other, thus having at least one non-trivial case where they are in the same location.
  • For the “Auto” class, also implement the less-than (<) operator. Use the location attribute where a larger location is “less than” a smaller location.
    • After running the loops above, add each Auto to a Vector, then call the Vector’s “sort” method.
    • Print each Auto in the Vector – the ones that print first are the “winners”, since their greater overall speeds allowed them to travel the farthest.
  • Extra: in the output operator (<<), output each attribute separated by commas. E.g., instead of something like:
    • Name: {auto name} Location: {location} Speed: {speed} Acceleration: {acceleration}
    • or whatever format you chose, print it in this format:
      {auto name} , {location} , {speed} , {acceleration}
      • Leave the “Print” method in the more human-readable format
      • As the code is processing the loops, in each iteration of each loop, direct the output to a file – use a different file for each auto instance
      • This is known as a “CSV” or Comma Separated File. You can load this file into a spreadsheet program such as Excel, and use its graphing capabilities to make some pretty spectacular looking graphs

Operator Overloading

Lecture Slides are screen-captured images of important points in the lecture. Students can download and print out these lecture slide images to do practice problems as well as take notes while watching the lecture.

  1. Intro
    • Overview
      • When Not to Overload Operators
      • When It Can Be a Good Idea
      • Kinds of Operators to Overload
      • Example: Named Vector
      • Assignment Operator=
      • Overloading Operator=
      • Overloading Binary Operator==
      • Overloading Binary Operator<
      • Overloading Arithmetic Operators
      • Overloading Binary Object-to-Object
      • Overloading Unary Operator
      • Overloading Prefix/Postfix Operator
      • Overloading Prefix Operator++
      • Overloading Postfix Operator++
      • Overloading, Object-to-Other Object
      • Overloading Operator<<
      • Array Operator[]
      • Overload Array Operator []
      • Function Operator()
      • Overloading Function Operator()
      • Bad Overloading!
        • Intro 0:00
        • Overview 0:13
        • When Not to Overload Operators 4:32
          • Arithmetic Operators Should Only Apply to 'Numeric' Kinds of Classes
          • Complex Number
          • 'add' Operation vs. 'add()' Method
        • When It Can Be a Good Idea 7:42
          • Your Class is Inherently Numeric In Nature
          • Some Operators Still Make Sense to be Overloaded
        • Kinds of Operators to Overload 11:03
          • Binary Operator
          • Unary Operator
          • Non-Member Functions
        • Example: Named Vector 15:03
          • Named Vector is a Directional Vector with a Name
          • Directional Vectors
          • Overloading Demonstration
        • Assignment Operator= 20:26
          • Deep Copy
          • Copy Constructor
        • Overloading Operator= 22:21
          • Example Member Function
        • Overloading Binary Operator== 27:10
          • Example 'Equal' Member Method
          • Other Comparison Methods Can Call This One
        • Overloading Binary Operator< 32:30
          • Example: 'Less Than' Member Method
        • Overloading Arithmetic Operators 35:04
          • '@=' Operator
          • Operations Work Symmetrically
        • Overloading Binary Object-to-Object 38:19
          • '@=' Is Member Function with Full Access
          • '@' is Non-Member Function
          • Multiple a Vector by a Number to Make it Larger
          • Need Two Symmetrical Non-Member Functions
        • Overloading Unary Operator 47:16
          • No Parameter for the Operator@ Function
        • Overloading Prefix/Postfix Operator 49:12
          • Unary Operators with Different Semantics for Prefix and Postfix
          • '++' and '--'
        • Overloading Prefix Operator++ 51:21
          • Add 1.0 to X/Y Components Before Accessing Value for Caller
          • Also Include the Operator-- to Subtract 1.0
        • Overloading Postfix Operator++ 52:02
          • Give It a Dummy Operator
        • Overloading, Object-to-Other Object 54:18
          • Make Non-Member Function a 'Friend' to Access Private Members of Your Class
          • Parameters Must Include Other Objects When Overloading
          • Useful for Saving an Object to a File
        • Overloading Operator<< 56:45
          • Include Non-Member 'Friend' Function in Class
          • Implement Function in the Compilation Unit
          • Allows Output of Your Objects with Other Data
        • Array Operator[] 1:00:55
          • Example: The Standard Library Vector
          • Named Vector
        • Overload Array Operator [] 1:03:19
          • Implement Operator[] As a Member Function
          • Write the Code
          • It Lets You Define a Non-Int for the 'Array Index'
        • Function Operator() 1:06:39
          • Examples
          • Define (idx)
        • Overloading Function Operator() 1:09:45
          • Implement Operator() As a Member Function
          • Calling Example
        • Bad Overloading! 1:12:27