1

I want to change the data in the private members but the delete_Data() function is outside the class so how to get the members without being declared in the class and erase them using the delete_Data() function
the only way it works for me it to declare delete_data()function in the class on public section but
our teachers said you should only use this method
i need help pls thank you for eg

Enter your Name: John
Enter your id: 1234

Name: John
ID:1234

Name :
ID:0

#include <iostream>
#include <string.h>
using namespace std;
class data{
  private:
     string name;
     int id;
  public:
     void add_Data()
{
     cout<<"Enter you Name :";
     cin>>name;
     cout<<"Enter your id :";
     cin>>id;
}
    void display_Data(){
     cout<<"Name :"<<name<<endl;
     cout<<"ID :"<<id<<endl;
}
};

void delete_Data(){
     name="";
     id=0;
}
  int main(){
    data one;
    one.add_Data();
    one.display_Data();
    delete_Data()
    one.display_Data;
 return 0;
}
john T
  • 23
  • 3
  • 2
    You could declare `delete_Data` as a friend function, but I don't see why you wouldn't have delete data inside the class. Also `name`, and `id` doesn't make sense outside the class. You should either have `delete_Data(data&)` or `delete_Data(data*)`. – Lala5th Aug 07 '21 at 06:51
  • 3
    Why don't you ask your teacher? Isn't that their job? Asking random people on the internet to guess what your teacher wanted seems like a bad idea. – super Aug 07 '21 at 06:52
  • 1
    This is quite a common interview question: not only in C++ but also in C#. There are some answers in https://www.geeksforgeeks.org/can-access-private-data-members-class-without-using-member-friend-function/ – cup Aug 07 '21 at 06:59
  • I tried but I don't get a solution that's why I asked in Stackoverflow the only method it worked for me is to declare delete function inside public class – john T Aug 07 '21 at 07:01
  • 2
    @cup A site that uses `#include ` in their examples should not be trusted to give good advice. – Ted Lyngmo Aug 07 '21 at 07:02

1 Answers1

0

Usually, the member functions and the friends of the class can access the private members of a class, via an instance of the class.

Therefore, in your case, make the delete_Data function as friend function.

class data 
{
private:
    std::string name;
    int id;
public:
    // ...
    friend void delete_Data(data& obj);
};

void delete_Data(data& obj)
{
    obj.name = "";
    obj.id = 0;
}

Now in the main()

data one;
delete_Data(one);

Also note that, you need to pass an instance of the data class to access the members of the class.


Side notes:

JeJo
  • 30,635
  • 6
  • 49
  • 88
  • "*Only the member functions and the `friend`s of the class can access the private members of a class.*" - actually, that is not true. There is another way, using some [template trickery](http://bloglitb.blogspot.com/2011/12/access-to-private-members-safer.html). – Remy Lebeau Aug 07 '21 at 07:00
  • another easy way is to just use pointers and offsets. – Remy Lebeau Aug 07 '21 at 07:08