-1

I am trying to set value for object member by using updateRollNo() function . But it's not working , Can i use friend function to manipulate object data ? If i am using Student class member then it's working good but it's not working with friend function .

#include<iostream>
using namespace std ;

//Forward declaration
class Student ;
void updateRollNo(Student);
void checkRollNo(Student);

//Define class Student
class Student{
    private :
    int roll_no ;

    //Make friend function
    friend void updateRollNo(Student);
    friend void checkRollNo(Student);
};
int main(){

    Student s1 ;
    updateRollNo(s1);
    checkRollNo(s1);

    cout<<"\n\n";
    return 0;
}


//Define function updateRollNo()

void updateRollNo(Student stu){
    cout<<"\nEnter student Roll No : ";
    cin>>stu.roll_no ;
    cout<<endl ;
}


//Define function checkRollNo()

void checkRollNo(Student stu){
    cout<<"Roll No : "<<stu.roll_no ;
    cout<<endl;
}


Sachin
  • 1
  • 3
  • *"But it's not working"* -- how is it not working? Did it overwrite your program with a copy of Minecraft? What symptoms can you describe that would give others a chance to realize they have the same problem as you (hence they can benefit from the answers). – JaMiT Sep 15 '21 at 01:29

2 Answers2

0

You problem is unrelated to the friend keyword (which is working fine); the problem is that you are passing your Student object by-value into updateRollNo(), which means that when the code inside updateRollNo() sets the roll_no member-variable, it is setting that member-variable on the local copy (stu) rather than on the s1 object from main(). That's why, when you subsequently try to print out the value inside checkRollNo(), you find that the value you print is still unset/uninitialized.

One simple way to get the behavior you want would be to modify updateRollNo() to pass the argument by-reference instead; that is, replace all instances of

void updateRollNo(Student);

with

void updateRollNo(Student &);

That way it will update s1 directly rather than updating a temporary/local copy of s1.

Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
0
void updateRollNo(Student);

By default, in C++ function parameters get passed by value. This means that in the updateRollNo function its Student parameter is a duplicate copy of whatever was passed into this function. Any changes that updateRollNo() makes to this parameter will have absolutely no effect, whatsoever, on the original value that the caller passed to updateRollNo().

In order for updateRollNo to modify whatever object gets passed to it, then either a pointer, such as

void updateRollNo(Student *);

or a reference, such as

void updateRollNo(Student &);

to the object must get passed in, instead of the object itself. A full discussion of pointers, references, and how to use them in C++ cannot be completely summarized in a couple of short paragraphs, so I'll refer you to your C++ textbook for more information.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148