vendredi 6 août 2021

How to access private class members with out being in the in class

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
and our teachers said you should only use this method
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;
}

Aucun commentaire:

Enregistrer un commentaire