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

Cascabel
  • 479,068
  • 72
  • 370
  • 318
Zach Smith
  • 5,490
  • 26
  • 84
  • 139
  • ah seems those are functions which he expands on in the .cpp file – Zach Smith Sep 14 '10 at 00: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). – James McNellis Sep 14 '10 at 01:22
  • 1

1 Answers1

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.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • so in the .cpp file i would then build on those definitions from the .h file? – Zach Smith Sep 14 '10 at 00:45
  • @HollerTrain: Yes. You would define those functions in a .cpp file. Make sure you have [a good introductory C++ book](http://stackoverflow.com/questions/388242/the-definitive-c++-book-guide-and-list); such a book will help you to understand the language. – James McNellis Sep 14 '10 at 00:50
  • are these considered prototypes? why aren't they part of the class? – T.T.T. Sep 14 '10 at 01:02
  • 2
    @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.) – dash-tom-bang Sep 14 '10 at 01:05
  • so it can be same as `complexNummber operator +(complexNumber A, complexNumber B);` – Zach Smith Sep 14 '10 at 01:23
  • @sbi - Never wrote a program that had the lifetime of a few key strokes? – Ishtar Sep 14 '10 at 05:54
  • @Ishtar: I do this almost daily here, but then I do it for education, and I've learned (through teaching C++) that it isn't a good idea to cut corners when doing so. Other than that... Every big project I've seen has, at its foundations, some pieces of code which were meant as a proof-of-concept throw-away implementations, but which never got thrown away. So I've learned to see small throw-away proof-of-concept projects as potential foundations for big projects. But I might just be trying to rationalize me being anal... – sbi Sep 14 '10 at 08:23
  • @sbi: No, I meant that only in reply to the comment about overloaded operator parameter names (though I see what you mean about my comment, which I've deleted because it might be construed as advice to save keystrokes, which wasn't at all my intent; I meant it more as an offhand remark). If anything, I get complaints in code reviews because my identifiers are too long. – James McNellis Sep 14 '10 at 13:06
  • @James: Understood. I removed my comment, too. – sbi Sep 14 '10 at 15:30
  • It would behoove me to remember that everything posted here may be construed as general advice. – James McNellis Sep 14 '10 at 16:32
  • @James: I learned that "do as I say, not as I do" doesn't work at all. If you skip `std::` in code examples because it uses precious white board space and you're concentrating on other aspects of that code anyway, your students will do so, too, in their programs, no matter whether you told them not to. If you want them to use proper variable names, you'd better do so in the weakest of example code you confront them with. And if you want them to use RAII, only RAII, and nothing but RAII, you need to do so yourself _all the time_, even while you're talking about something else entirely. – sbi Sep 14 '10 at 18:35
  • @sbi: Agreed: there are a lot of blatantly wrong tutorials and examples, but stylistically wrong and idiomatically wrong is insidious because it may look "right" and is habit-forming. I certainly appreciate the critical feedback. – James McNellis Sep 14 '10 at 18:58