Observer type bool C ++

2

I'm trying to solve this exercise:

The Dice class manages the launch of 2 dice.

  • A) The constructor receives two optional parameters with the initial value for the given 1 and the given 2 respectively. If the dice do not receive an initial value, it will be assigned to 1. Error control: if any initial value is not correct, the assigned value will be equal to 1.
  • B) Observer get (). It will have 2 parameters. The identifier of the die (1 or 2) and a reference to an integer that will store the corresponding die value. Error control: if there is an error with the identifier of the die, get () must return false, otherwise get () returns true.

I have doubts in the bool-type observer, since I do not understand how I can return true or false in addition to the object.

This is the class:

class Dados{

private:
  int dado1_, dado2_;

public:


  Dados(int valor1=1, int valor2=1){

    if (valor1>=1 && valor1<=6)
       {
        dado1_=valor1;
       }
    else dado1_=1;
    if (valor2>=1 && valor2<=6)
       {
        dado2_=valor2;
       }
    else dado2_=1;  
  }

I tried to make the observer like that, but I do not return dice1_ or dado2_:

bool get(int dado,int valor)
{
if (dado>=1 && dado<=2)
   {
    return true;
   }
else
   {
    return false;
   }
}

Thank you!

    
asked by Anita 25.08.2018 в 13:33
source

1 answer

2
  

b) Observer get (). It will have 2 parameters. The identifier of the die (1   or 2) and a reference to an integer that will store the value of the die   corresponding.

This means that get () will have the following prototype:

bool get( int dado, int& valor);

valorDado in this case acts as a parameter of output . When a parameter is a reference the value that is assigned within the function will be usable when you exit the function.

Consider the following example:

#include <iostream>
using namespace std;

bool f1(int valor)
{
    valor = 5;
    return false;
}

bool f2(int& valor)
{
    valor = 7;
    return false;
}

int main() {
    int v;
    v = 3;
    f1(v);
    std::cout << v << std::endl;
    f2(v);
    std::cout << v << std::endl;
    return 0;
}

When executing that code you will get this result:

  

3
  7

In f1 we are passing the parameter by value. This means that f1 receives a copy of the passed parameter and that changing that parameter in f1 does not affect the variable with which f1 was called. That is why assigning 5 to valor does not affect v .

However f2 receives the parameter by reference, that is what the & means. In this case f2 receives a reference to the variable v and therefore when we assign 7 to valor that makes v go to be worth 7 .

I imagine that in the future you will be taught more advanced techniques. Keep in mind that what you are doing is just an exercise and is not representative of what is done in a real program. Normally the value of the die would be returned by get () and error control would be done with exceptions.

    
answered by 25.08.2018 / 14:50
source