Home > database >  How to delete an element from a abstract class
How to delete an element from a abstract class

Time:10-27

#include <iostream>
#include <string>
#include <conio.h>
#include <functional>

using namespace std;

//

class Persoana {
public:
    int varsta;
    string nume, prenume;
    //constructor vid cu scopu pentru declararea vectorului
    Persoana() {}
    Persoana(string nume, string prenume, int age)
    {
        this->nume = nume;
        this->prenume = prenume;
        varsta = age;
    }
    virtual void afisare()
    {
        cout << "\n";
        cout << "Numele: " << nume << "\n";
        cout << "Prenumele: " << prenume << "\n";
        cout << "Varsta: " << Persoana::varsta << "\n";
    }
    string returnName()
    {
        return nume;
    }

    ~Persoana() {};

};

class Angajat :public Persoana {
public:
    string departament;
    int salariu;
    Angajat() {}
    Angajat(string nume, string prenume, int varsta, string departament, int salariu)
        :Persoana(nume, prenume, varsta)
    {
        this->departament = departament;
        this->salariu = salariu;
    }
    virtual void afisare()
    {
        Persoana::afisare();
        cout << "Departament: " << departament << endl;
        cout << "Salariu: " << salariu << endl;
    }
    //Mai jos este sunt 3 "gathers" , care returneaza nume, prenume, varsta
    string returnNume() {
        return nume;
    }

    string returnPrenume() {
        return prenume;
    }

    int returnVarsta()
    {
        return varsta;
    }

};

class Student : public Persoana {
public:
    string tip;
    int an;
    Student() {}
    Student(string nume, string prenume, int varsta, string tip, int an)
        :Persoana(nume, prenume, varsta)
    {
        this->tip = tip;
        this->an = an;
    }

    string returnNume()
    {
        return nume;
    }

    string returnPrenume()
    {
        return prenume;
    }
    int returnAge()
    {
        return varsta;
    }

    virtual void afisare() {
        Persoana::afisare();
        cout << "Tip:" << tip << endl;
        cout << "Varsta:" << varsta << endl;
    }
    
};

void menu()
{
    string n, p, d;
    int i_2=0,v, s, i=0, ok = 0, ok2 = 0, nr=0 , opt, nr2 = 0,nr3=0;
    string num, num2;
    Student student[40];
    Angajat angajat[40];
    Persoana * persoane[80];
    int choice;
    do
    {
        cout << "0.Adaugare student" << endl;
        cout << "1.Adaugare angajat" << endl;
        cout << "2. Cautare" << endl;
        cout << "3. Stergere" << endl;
        cout << "4. Exit" << endl;
        cin >> choice;
        switch (choice)
        {
        case 0:
            
            nr  ;
            cout << "Dati numele: ";
            cin >> n;
            cout << "Dati prenumele: ";
            cin >> p;
            cout << "Dati varsta: ";
            cin >> v;
            cout << "Dati tip: ";
            cin >> d;
            cout << "Dati an:";
            cin >> s;   
            student[nr2] = Student(n, p, v, d, s);
            persoane[i] = &student[nr2];
            nr2 = nr2   1;
            i = i   1;
                for (i = 0; i < nr; i  )
                {
                    persoane[i]->afisare();
                }
            break;
        case 1:
            
            //retin numaru de studenti si angajati(in total) prin acest nr
            nr  ;
            cout << "Dati numele: ";
            cin >> n;
            cout << "Dati prenumele: ";
            cin >> p;
            cout << "Dati varsta: ";
            cin >> v;
            cout << "Dati departamentul: ";
            cin >> d;
            cout << "Dati salariul:";
            cin >> s;
            
            angajat[nr3] = Angajat(n, p, v, d, s);
            persoane[i] = &angajat[nr3];
            nr3  ;
            i  ;
            break;
        case 2:
                       //this search is working perfectly fine :)
            cout << "Introduceti numele persoanei: ";
            cin >> num;
            ok = 0;
            
            
            for (i = 0; i < nr; i  )
            {
                
                if ((num.compare(persoane[i]->returnName())) == 0)
                {
                    persoane[i]->afisare();
                }
                ok  ;
            }
            if (ok == 0)
                cout << "Aceasta persoana nu se afla in institutie:(";
            break;
        case 3:
            cout << "Introduceti numele pe care vreti sa il ste/rgeti:";
            cin >> num2;
            //here i want to delete an element, but when this is running will delete everything from my class:(
            for (i = 0; i < nr; i  )
            {
                if (num2.compare(persoane[i]->returnName()) == 0)
                {
                    persoane[i]->~Persoana(); 
                    nr--;
                    ok2  ;
                }
            }
            if (ok2 == 0)
            {
                cout << "Element not found";
            }
            else
            {
                cout << "Elementul a fost sters:";
            }
            break;
        case 4:         
            exit(0);
        }
    } while (choice != 4);
}


int main()
{
    menu();
}

In this program i create 2 child class from class Persoana, class Student and class Angajat ,in the menu at option 3 , it will ask you for a name to delete , my problem is that the function i created is deleting the whole class Persoana, i really dont know how to use destructors and abstract classes

CodePudding user response:

If you are instantiating Students and Angajats statically. You will not be able to delete them.

    Student student[40]; // static allocation of 40 students
    Angajat angajat[40]; // static allocation of 40 angajats
    Persoana * persoane[80]; // static allocation of 80 pointers to students and angajats base classes.
  • First of all, you need to declare the Persoana destructor as virtual, to invoke derived classes destructors: virtual ~Persoana(){/*whatever*/}

  • Instantiate the Students and Angajats dynamically. I.e: To create a new instance of Students you should use :

    Persoana* student = new Student();
    

And consider using the STL list : std::list<Persoana*>, ::push_back(..), and its iterators to search and delete elements.

that example creates a list of pointers to Persoana. Deleting the item of the list will NOT delete the item. For that, you have to use the delete operator.

  • Related