jeudi 3 décembre 2020

Creating a vector of objects and the class contains four members and able to access and delete a whole object

First of all i'm very weak at C++ OOP ... I've this class contains four members i want to make a vector of object and able to insert and delete whole object based on search e.g. insert

Surname='john' Firstname='david' birthdate='1960' and when i want to delete the whole object just search for John in the vector so how to connect between vector index of John and rest of his data and erase it e.g. Add.erase(Add.begin()+index) ; after getting the index of john in the vector or in the object

 class Employees{
public:

    vector <Employees> Add;

    int maxx =500;
    string Surname;
    string Firstname;
    string birthdate ;

};

The function for Adding a new Employee

void AddEmployee(vector <Employees> &Add){

     Employees ZZ;
    for(int i=0;i<500;i++) {
            string s ;
            cin >> s;
       //     Surname= s;
             string d ;
         //    Firstname=d;

            cin >> d;
             string c ;

            cin >> c;
      //      birthdate= c ;

        Add.push_back(ZZ);

        // Add.push_back(ZZ.FirstName) ;

       //  Add.push_back(ZZ.birthdate) ;
}
        cout<< endl <<  Add[0].Surname<< endl ;
   };

And this delete function that when i enter John search in vector of objects and get his index then delete the wole object

   void DeleteEmployee(Employees &ZZ ){



     
    cout << " delete employee" ;
    string delete1 ;
    cin >> delete1 ;

     auto it = std::find(Add.begin(), Add.end(), delete1);
    if (it == Add.end())
        {
cout<< " name not in vector "  << endl ;
    } else
    {
     auto index = distance(Add.begin(), find(Add.begin(), Add.end(), delete1));
        Add.erase(Add.begin()+index) ;
         // here should delete the whole object including Surname,Firstname,datebirth ? 
            // i'm not sure if it's correct
    }

 }




   int main(){
 
  // here any thing to display the vector members but i don't know how to do it 

  }

Aucun commentaire:

Enregistrer un commentaire