0

I have started learning C++ recently, doing some simple class/friend function practice, What i was trying to do is, get 2 numbers from user for 2 object of a class by using friend functions only then again using friend function only, multiply those 2 numbers and show on screen. Let's say i typed 2, 3, 4, 5 in order. Expected outputs are 6 and 20, but i can only see 0 and 0 on screen.

#include<iostream>
using namespace std;

class iluvcpp {
    int no_1, no_2;
public:
    iluvcpp(){}
    ~iluvcpp(){}
    friend void inputno(iluvcpp obj);
    friend int multiply(iluvcpp obj);
}multi_1, multi_2;

void inputno(iluvcpp obj) {

    cout <<"Enter no 1: ";
    cin >> obj.no_1;
    cout <<"Enter no 2: ";
    cin >> obj.no_2;
}
int multiply(iluvcpp obj) {
    return ((obj.no_2)*(obj.no_1));
}

int main() {
    inputno(multi_1);
    inputno(multi_2);   
    cout << multiply(multi_1) <<"   "<< multiply(multi_2);

    cout << endl;
    system("pause");
    return 0;
}
skink
  • 5,133
  • 6
  • 37
  • 58
Than
  • 3
  • 1
  • 2
    `inputno` takes `obj` by value and changes to `obj` are not visible to the caller (i.e. a copy is made). – crashmstr Oct 24 '17 at 18:58
  • the `iluvcpp obj` is a parameter of the function `inputno` and once the function returns that parameter and any modifications to it are gone. You never set any field of the `multi_1`. – 463035818_is_not_an_ai Oct 24 '17 at 18:58
  • See here for a detailed explanation on the differences between passing by reference and by value: https://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value – Alex Huszagh Oct 24 '17 at 19:15

3 Answers3

6

You pass your objects by value, meaning when you call inputno you're working on a copy. Try changing the function signature to:

void inputno(iluvcpp& obj) {
  ...
}

Here's an explanation with examples about passing parameters by value vs by reference.

orip
  • 73,323
  • 21
  • 116
  • 148
1

2 suggestions:

A variable/method of a class is private by default. Either make your variables no_1 and no_2 public as well, or write a setter, if you're familiar with it. To have them public by default, use struct, as opposed to class.

As others already have pointed out, you're not modifying multi_1 and multi_2 directly. Either have them passed in by reference (The answer of orip mentions exactly that), or make them return this iluvcpp obj Object and call them like:

multi_1 = inputno(multi_1);
AProgrammer
  • 81
  • 11
  • That's valid, although in that case you don't need any parameter: `auto multi_1 = inputno();` – orip Oct 25 '17 at 08:11
  • True, but in my opinion it's always better to write a function in a way that lets you use it with any instance, even just for this small example. – AProgrammer Oct 26 '17 at 08:54
0

void inputno(iluvcpp obj) has one parameter named obj and no return value. That's the wrong way around here, inputno doesn't need anything from main but it should return something:

iluvcpp inputno(void) { ... or (equivalent) iluvcpp inputno() { ....

You'll need a return something; statement at the end, so C++ knows what value to return from inputno.

MSalters
  • 173,980
  • 10
  • 155
  • 350