Operator Overloading Questions

Refresh

November 2018

Views

207 time

1

So my prof has a sample .h file with the following operators at the end

//ComplexNumber.h

#include <iostream>
using namespace std;

#ifndef MY_COMPLEX_H
#define MY_COMPLEX_H

class complexNumber {
public:
    complexNumber();
    complexNumber(double a, double b);
    void setReal(double a);
    void setImaginary(double b);
    double getReal();
    double getImaginary();
    void printComplex();

private:
    double realPart;
    double imaginaryPart;
};

complexNumber add(complexNumber A, complexNumber B);
complexNumber subtract(complexNumber A, complexNumber B);
complexNumber multiply(complexNumber A, complexNumber B);
complexNumber divide(complexNumber A, complexNumber B);

complexNumber operator +(complexNumber A, complexNumber B);
complexNumber operator -(complexNumber A, complexNumber B);
complexNumber operator *(complexNumber A, complexNumber B);
complexNumber operator /(complexNumber A, complexNumber B);

ostream & operator << (ostream &outs, complexNumber A);

// istream & operator >> (istream &ins, complexNumber &A);

#endif

where is he getting complexNumber A and complexNumber B? I don't see these as variables anywhere...

1 answers

4

A and B are function parameters of type complexNumber. In a declaration that is not a definition, the names A and B don't mean anything; the following two are the same:

complexNumber add(complexNumber A, complexNumber B);
complexNumber add(complexNumber  , complexNumber  );

It's a good idea to name the parameters, though, because it usually makes function declarations easier to understand and helps make the code self-documenting.

In the definition, if you want to use the argument passed for a given parameter, the parameter has to have a name, otherwise there's no way to identify it.