I would like to know if a friend function can change private data in the class without using a
pointer and sending out the object.
I mean does a friend function have access like a member function?
For Example:
class myinfo {
private:
char name[20];
int id;
float income;
public:
void showInfo(void);
myinfo(void);
friend void updateInfo(myinfo);
int main ( ) {
myinfo j;
updateInfo(j); // calling the friend function
return 0;
}
void updateInfo(myinfo c) {
strcat(c.name, ":updated");
c.id++;
c.income += 1.1;
Yes, but not the way you’ve written it… If you want the function to modify the passed in object, accept a reference rather than by value…
It appears you’ve not learned about references in c++.
implementation
Btw. on a side note, prefer to use
std::stringand also learn about rule of three (specially for non-trivial classes such as this).