dommeier
dommeier

Reputation: 53

C++ - How can we call a class without calling its attributes?

I need to implement the class Multiplier for a school exercise, but I do not understand how the teacher was able to call prod() without calling its inputs.

The goal of the code is to read a sequence of integers until the product of their absolute values is greater than 200.

Can somebody help me understanding please?

Here is the code:

#include <iostream>
using namespace std; 

int main()
{
    Product mult(200); 
    cout << "Enter numbers: " << endl;
    do{
      cin >> mult;
    } while(!mult.exceed_limit()); 

    cout << "The absolute values product is " << mult() << " . " << endl; 

    return 0; 
}

Upvotes: 4

Views: 155

Answers (3)

463035818_is_not_an_ai
463035818_is_not_an_ai

Reputation: 122657

Lets see what we need

int main()
{
    Multiplier prod(3); 

A constructor. The parameter is probably the number of factors to be multiplied.

    std::cout << "Enter numbers: " << std::endl;
    do{
      std::cin >> prod;

A way to "input" the factors.

    } while(!prod.limit_exceeded()); 

A method to see if the entered factors equals the number of desired factors.

    std::cout << "The product of the absolute values is " << prod() << " . " << std::endl; 

A call operator that returns the resulting product.

    return 0; 
}

So lets do that:

struct Multiplier {
    Multiplier(size_t n) : max_factors(n),num_factors(0),product(1) {}
    size_t max_factors;
    size_t num_factors;
    double product;
    double operator()() const { return product;}
    bool limit_exceeded() const { return max_factors <= num_factors;}
};

Constructor takes number of factors, product holds the result, operator() returns the result and limit_exceeded() checks if all factors have been entered. Finally, a an overload for operator>> to read the factors:

std::istream& operator>>(std::istream& in, Multiplier& m){
    double x;
    if (in >> x) {
        m.product *= x;
        ++m.num_factors;
    }
    return in;
}

It is a bit uncommon for std::cin >> prod; to not read prod but instead to modify prod, but thats fine.

Live Demo

Upvotes: 1

Remy Lebeau
Remy Lebeau

Reputation: 596823

Multiplier prod(100); - Multiplier must have defined a constructor that takes an integer as input, eg:

class Multiplier
{
...
public:
    Multiplier(int value);
...
};

cin >> prod - Multiplier must have overloaded operator>> for input, eg:

class Multiplier
{
...
};

istream& operator>>(istream&, Multiplier&);

prod.limit_exceeded() - Multiplier must have defined a member limit_exceeded() method, eg:

class Multiplier
{
...
public:
    bool limit_exceeded() const;
...
};

cout << prod() - Multiplier must have overloaded operator() (and the return value is then streamed to cout via operator<<), eg:

class Multiplier
{
...
public:
    int operator()() const;
...
};

Upvotes: 2

user4442671
user4442671

Reputation:

A class can implement the "call" operation by overloading the operator() member function.

For example

class MyType {
public:
  void operator()(int param) const {
    std::cout << "MyType(int) called with: " << param << "\n";
  }

  void operator()() const {
    std::cout << "MyType() called\n";
  }
};

int main() {
  MyType  instance;

  instance(12);
  instance();

  return 0;
}

Upvotes: 3

Related Questions