views:

87

answers:

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

+5  A: 

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
so in the .cpp file i would then build on those definitions from the .h file?
HollerTrain
@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
are these considered prototypes? why aren't they part of the class?
Tommy
@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
so it can be same as `complexNummber operator +(complexNumber A, complexNumber B);`
HollerTrain
@sbi - Never wrote a program that had the lifetime of a few key strokes?
Ishtar
@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
@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
@James: Understood. I removed my comment, too.
sbi
It would behoove me to remember that everything posted here may be construed as general advice.
James McNellis
@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
@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