简体   繁体   中英

Doing some counter manipulation, how do I make it so that if setCounter() has no arguments, the counter would be zero?

Given the following C++ program:

#include "counterType.h"
#include <iostream>
#include <string>

using namespace std;

counterType::counterType()
{
    counter = 0;
}

counterType::counterType(int c)
{
    setCounter(c);
}

void counterType::setCounter(int ck)
{
    counter = ck;
}

int counterType::getCounter()
{
    return counter;
}

void counterType::incrementCounter()
{
    ++counter;
}

void counterType::decrementCounter()
{
    --counter;
}

void counterType::print()
{
    cout << "Counter = "<< counter << endl;
}

The code seems to only work when setCounter() has an argument in it. The only tests that fail are when is has no argument. So how do I check it in a way that if there is no argument, then the counter would be 0?

This is the perfect place for a default function argument. Since this is a class member function that means you need to change your function declaration to be

void setCounter(int ck = 0);

To tell the compiler that if a value is not provided for ck that it can use 0 as the default value. That means your function definition stays the same since it "pulls in" the default value from he declaration.

Two approaches:

1) Supply a default argument in the function declaration so you can call the function without supplying it.

void setCounter(int ck = 0);

2) Overload the function

void counterType::setCounter()
{
    counter = 0;
}

Some software houses (particularly those where the code standard guys are Java folk) disallow the first style. A less facetious reason for not liking the first way is that the default argument value can be run-time evaluable (a member variable perhaps) and that can harm program stability.

For starters you can initialize the data member counter in the declaration of the data member within the class definition. For example

class counterType
{
    //...
    int counter = 0;
};

In this case the constructors can look like

counterType::counterType()
{
}

counterType::counterType(int c) : counter( c )
{
}

Or you could define the constructors the following way

counterType::counterType() : counterType( 0 ) 
{
}

counterType::counterType(int c) : counter( c )
{
}

The function setCounter can have a default argument

void counterType::setCounter(int ck = 0 )
{
    counter = ck;
}

It is better to declare the function getCounter within the class definition like

int getCounter() const;

because the function does not change the object itself. And define it like

int counterType::getCounter() const
{
    return counter;
}

And this function

void counterType::print()
{
    cout << "Counter = "<< counter << endl;
}

is better to declare and define like

std::ostream & print( std::ostream &os = std::cout ) const;

and define it like

std::ostream & print( std::ostream &os = std::cout ) const;
{
    return os << "Counter = "<< counter << endl;
}

It also does not change the object itself so it should be declared with the qualifier const .

As it seems your class has only one data member then you could define the operator << for the whole class. For example

class counterType
{
    //...
    friend std::ostream & operator <<( std::ostream &os, const counterType &c );
};

std::ostream & operator <<( std::ostream &os, const counterType &c )
{
    return os << "Counter = "<< c.counter << endl;
}

Do you mean something like that?

void counterType::setCounter()
{
    counter = 0;
}

What you're trying to achieve is called function overloading : given a different list of arguments, C++ lets you define a different "version" of the same function.

void counterType::setCounter()           // No argument in there
{
    counter = 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM