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

3
  • ah seems those are functions which he expands on in the .cpp file Commented Sep 14, 2010 at 0:45
  • 2
    Note also that (a) it's a bad idea for a wide variety of reasons to use a using directive (e.g. using namespace std;) in a header file (or, in my opinion, any source file) and (b) usually you would implement such functions and operators as taking parameters of type const complexNumber& (though that depends; I wouldn't necessarily balk at using pass-by-value for a class this small). Commented Sep 14, 2010 at 1:22
  • 1
    <obligatory mention of std::complex/> Commented Sep 14, 2010 at 2:57

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

12 Comments

so in the .cpp file i would then build on those definitions from the .h file?
@HollerTrain: Yes. You would define those functions in a .cpp file. Make sure you have a good introductory C++ book; such a book will help you to understand the language.
are these considered prototypes? why aren't they part of the class?
@Tommy - they are prototypes and they don't need to be part of the class to function. Typically, implementing free functions is preferable to implementing class members, if those functions can do everything that they need to do through the class's public interface. (Oftentimes I see A and B called lhs and rhs, for left- and right-hand side.)
so it can be same as complexNummber operator +(complexNumber A, complexNumber B);
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.