class counter { friend ostream &operator<<(std::ostream &, const counter &) // s
ID: 3655520 • Letter: C
Question
class counter {
friend ostream &operator<<(std::ostream &, const counter &)
// stream insertion operator
friend istream &operator>>(istream & in, counter &c) ;
// stream extraction operator. This should read arbitrarily long numbers.
?
private:
char * digits; // to hold the digits with digits[i] representing the 10^i's place.
int arraySize; // size of digits array
int numDigits; // number of digits in the number, e.g 5 has one digit but 45 has two. ZERO should have zero digits
void addDigit(char digit, int position, char & carry);
// optional helper function for operator+=
// sets digits[position] to (digits[position] + digit) % 10
// and carry to (digits[position] + digit) / 10
Explanation / Answer
EXAMPLE You know how to write a C++ function, right? Well, the implementation for a method is just a function definition, only with :: in front of the method name to say that it is an implementation of a method in a previously-defined class (or struct...structs can have methods too.) For example, in your class definition (presumably in Counter.h) you should have declared: int getCounter(); That's your no-argument method (aka "member function" in C++) declaration. The implementation (presumably in Counter.cpp) would look like: int Counter::getValue() { return counter; } Oops, first-things-first, though. Your constructor definition should look like: Counter::Counter() { counter = 0; } ...or, even better, like this: Counter::Counter() : counter(0) { } Just as in the class definition, there is no result type (not even void) in the implementation of the constructor, and the constructor name is always the class name. You may not have learned the second option, but it's the preferred way to initialize members, and the only way to initialize a member object using a constructor with arguments. If there are multiple initializers, separate them with commas. The opening brace for the constructor body ends the list.Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.