A few days ago I was doing a project in CodeBloocks, referring to classes, member functions and pointers, which is about entering 2 numbers (1 in each function) and then adding it in another function; Just like this.
#include <iostream>
using namespace std;
class retorno
{
public:
int num1 ()
{
cout <<"\nescriba numero 1";
cin >> a;
}
int num2 ()
{
cout <<"\nescriba numero 2";
cin >> b;
}
int getnum1(int *ptra)
{
*ptra = a;
return *ptra;
}
int getnum2(int *ptrb)
{
*ptrb = b;
return *ptrb;
}//////////////////////////////////////
//Aca estaba trantando de hacerlo mediante referencia pero no resulto.
int getsum1(int *p1)
{
*p1=getnum1(&a)+getnum2(&b);
return *p1;
}
///////////////////////////////
private:
int a,b;
};
int main()
{
int d,a,s;
retorno listo1;
retorno listo2;
retorno listo3;
listo1.num1();
listo2.num2();
//aca imprimero los numeros introducidos mediante referencia
cout << "\n" << listo1.getnum1(&a) << "..." << listo2.getnum2(&d);
//aca es donde quiero que me muestre la suma de ambos numeros
cout <<"suma"<<listo3.getsum1(&s);
}
In getnum1
I set a pointer, which serves as a buffer (that's what I understood of the concept), then I set the gets above and return it with return
. At the moment of calling the functions with the object I should add but it does not work.
I have tried to match the variables in a single function with pointers and return the sum in another function but it does not work either.