开发者

Operator Overloading Questions

开发者 https://www.devze.com 2023-01-16 04:51 出处:网络
So my prof has a sample .h file with the following operators at the end //ComplexNumber.h #include <iostream>

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消