L18 - Operator Overloading

L18 - Operator Overloading - Overloading Operators in C+ C+...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
Overloading Operators in C++ C++ allows the programmer to redefine the function of most built-in operators on a class-by-class basis the operator keyword is used to declare a function that specifies what an operator symbol (such as = or +) means when it is applied to instances of a class – this gives the operator more than one meaning, and the compiler determines what meaning is intended by looking at the types of its operands syntax: type operator <operator-symbol> (parameter-list) Example: overloading the addition operator and the assignment operator for the Complex class
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Example Class to represent complex numbers: header (class definition file) #pragma once #include <iostream> using namespace System; using namespace std; class Complex { public: Complex (); Complex (double r, double i); Complex operator+ (Complex &other); Complex & Complex::operator = (Complex &n); void display(); private: double re,im; };
Background image of page 2
.cpp source (implementation) file for for class Complex #include "stdafx.h" #include " Complex.h" Complex::Complex (){ re = 0.0; im = 0.0; } Complex::Complex(double r, double i){ re = r; im = i; } return Complex (re + other.re, im + other.im); } re = n.re; im = n.im; return *this; } void Complex::display(){ cout << re << "," << im << endl; } Overloaded addition operator Overloaded assignment operator
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Example of a small program using class Complex: #include "stdafx.h" #include <iostream> #include "Complex.h" using namespace System; using namespace std; int main(array<System::String ^> ^args) { Complex a = Complex(1.2, 3.4); Complex b(3.4, 5.6); Complex c; c = a + b; c.display(); return 0; }
Background image of page 4
Let’s look at the overloading of the + operator: { return Complex (re + other.re, im + other.im) } this defines a function that tells what the + operator does when it is applied to instances of class Complex this is also a method of class Complex, – so one of its operands (the left operand) is an object of class Complex to which the method is being applied – the other operand (the right operand) is another object of class complex the method returns an object of type complex with data members that are the sums of the real and imaginary parts of the other two operands
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Overloading the Assignment Operator • The assignment operator (=) is really a binary operator – it has two arguments: its left side and its right side – in the expression x = 5 + 3; we expect to evaluate the right side and give that value to the thing on the left side (in this case, the int variable x) • If we are going to build a Complex class, we will need to have some operator that allows us to assign values to objects of this class – this means that we might want to overload the = operator
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 20

L18 - Operator Overloading - Overloading Operators in C+ C+...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online