-4

I need to create a header file for this code.

#include "iostream"
using namespace std;
#include <string>
#include <vector>
#include <algorithm>

class persoana {
private:
    string nume;
    int an_nastere;
    char sex;

public:
    persoana() {
        string nume;
        cout << "Nume: ";
        cin.ignore();
        getline(cin, nume);
        set_nume(nume);
        int an_nastere;
        cout << "An nastere: ";
        cin >> an_nastere;
        set_an_nastere(an_nastere);
        char sex;
        cout << "Sex: ";
        cin >> sex;
        set_sex(sex);
    }
    ~persoana() {}

    string get_nume() const { return nume; }
    void set_nume(string nume_nou) {
        if (nume != nume_nou) {
            nume = nume_nou.c_str();
        }
    }
    int get_an_nastere() const { return an_nastere; }
    void set_an_nastere(int an_nastere_nou) {
        if (an_nastere != an_nastere_nou)
            an_nastere = an_nastere_nou;
    }
    char get_sex() const { return sex; }
    void set_sex(char sex_nou) {
        if (sex != sex_nou)
            sex = sex_nou;
    }
};

ostream& operator<<(ostream& out, const persoana& p) {
    return out << p.get_nume() << " - " << p.get_an_nastere() << " - "
               << p.get_sex() << endl;
}

bool dupa_nume(persoana* p1, persoana* p2) {
    return (p1->get_nume() < p2->get_nume());
}
bool dupa_varsta(persoana* p1, persoana* p2) {
    return (p1->get_an_nastere() > p2->get_an_nastere());
}

class baza_de_date {
private:
    vector<persoana*> vector_persoane;
    int n;

public:
    baza_de_date(){};
    baza_de_date(int dim) {
        if (dim > 0) {
            n = dim;
            vector_persoane.reserve(n);
        }
    }
    ~baza_de_date() {
        for (int i = 0; i < vector_persoane.size(); i++)
            delete vector_persoane[i];
        vector_persoane.clear();
    }
    void inserare() { vector_persoane.push_back(new persoana()); }
    void afisare() {
        for (int i = 0; i < vector_persoane.size(); i++)
            cout << *vector_persoane[i];
    }
    void eliminare(string nume) {

        vector<persoana*>::iterator it;
        for (it = vector_persoane.begin(); it != vector_persoane.end();) {
            if ((*it)->get_nume() == nume) {
                delete *it;
                it = vector_persoane.erase(it);
            } else
                ++it;
        }
    }
    void eliminare(int an_nastere) {
        vector<persoana*>::iterator it;
        for (it = vector_persoane.begin(); it != vector_persoane.end();) {
            if ((*it)->get_an_nastere() == an_nastere)
                it = vector_persoane.erase(it);
            else
                ++it;
        }
    }
    void eliminare(char sex) {
        vector<persoana*>::iterator it;
        for (it = vector_persoane.begin(); it != vector_persoane.end();) {
            if ((*it)->get_sex() == sex)
                it = vector_persoane.erase(it);
            else
                ++it;
        }
    }
    void sortare_dupa_nume() {
        sort(vector_persoane.begin(), vector_persoane.end(), dupa_nume);
    }
    void sortare_dupa_varsta() {
        sort(vector_persoane.begin(), vector_persoane.end(), dupa_varsta);
    }
};

int main() {
    baza_de_date bd;
    int o;
    do {
        system("cls");
        bd.afisare();
        cout << "\n1.Adauga persoana\n2.Elimina persoana dupa nume\n3.Elimina "
                "persoana dupa anul nasterii\n4.Elimina persoana dupa "
                "sex\n5.Afiseaza dupa nume\n6.Afiseaza dupa "
                "varsta\n0.Iesire\nOptiunea: ";
        cin >> o;
        switch (o) {
            case 1: {
                bd.inserare();
            } break;

            case 2: {
                string nume;
                cout << "Nume: ";
                cin.ignore();
                getline(cin, nume);
                bd.eliminare(nume);
            } break;

            case 3: {
                int an_nastere;
                cout << "An nastere: ";
                cin >> an_nastere;
                bd.eliminare(an_nastere);
            } break;

            case 4: {
                char sex;
                cout << "Sex: ";
                cin >> sex;
                bd.eliminare(sex);
            } break;

            case 5: {
                bd.sortare_dupa_nume();
            } break;

            case 6: {
                bd.sortare_dupa_varsta();
            } break;
        }
    } while (o);

    return 0;
}
NetVipeC
  • 4,402
  • 1
  • 17
  • 19
  • 1
    Hi. Welcome to Stack Overflow. Dumping your whole code on us when all you want to know is how to create a header for a class is just noise and will most likely attract downvotes and close votes. Is all that code necessary for your question? No. You should post a minimal code consisting of dummy class and a dummy function and you should then extrapolate the answer to your real code. See the answer @BrianCain gave you? Just the minimal code needed to represent his point. Your question should look similar. – bolov Sep 03 '14 at 18:32

1 Answers1

2

You must separate the class' implementation from its interface. The process is similar to how it's done with C, with a little more added complexity.

In short, everywhere there's a pair of curly braces you need a semicolon instead. You must also transform the source to be properly qualified.

Here's the pattern to follow. Consider also one class per header/source pair, but that's not a fixed rule.

header:

class baza_de_date
{
private:
vector<persoana*> vector_persoane;
int n;
public:
baza_de_date();
baza_de_date(int dim);
// ...
};

source:

#include "baza.h"

baza_de_date::baza_de_date(int dim)
{                    
    if (dim>0)
    {
        n=dim;
        vector_persoane.reserve(n);
    }
}
Brian Cain
  • 14,403
  • 3
  • 50
  • 88
  • Separating code definition (cpp) and declaration (.h) isn't a language 'must do', but it is instead a preferable for various reasons. – user3791372 Sep 03 '14 at 18:14
  • @user3791372, the context was "I need to create a header file for this code" so the answer states "[in order to do that] You must separate the ... implementation from its interface". – Brian Cain Sep 03 '14 at 20:11