This is the code:
#include<iostream>
#include <cstring>
using namespace std;
class Muncitor
{
char *nume;
int vechime;
static int count;
public:
Muncitor();
Muncitor(char *,int);
friend istream& operator >>(istream &, Muncitor &);
friend ostream& operator <<(ostream &, const Muncitor &);
static int afisare_count();
};
int Muncitor::count = 0;
Muncitor::afisare_count()
{
return count;
}
Muncitor::Muncitor()
{
this->nume = new char(4);
strcpy(nume,"Gol");
this->vechime = 0;
}
Muncitor::Muncitor(char *nume, int vechime)
{
this->nume = new char(strlen(nume)+1);
strcpy(this->nume,nume);
this->vechime = vechime;
count++;
}
istream& operator >>(istream &is, Muncitor &m)
{
is.getline(m.nume,100);
is >> m.vechime;
return is;
}
ostream& operator <<(ostream &os, const Muncitor &m)
{
os<<m.nume<<" "<<m.vechime;
return os;
}
int main()
{
int n;
cin>>n;
Muncitor **a;
a = new Muncitor*[n];
for(int i=0;i<n;i++)
{
a[i] = new Muncitor;
cin >> a[i];
}
for(int i=0;i<n;i++)
{
cout << a[i] << endl;
}
cout<<"Count = "<<Muncitor::afisare_count();
}
Here I'm trying to take input, display it, and count how many objects I created. The count should work but for some reason cin >> a[i] does not work. Or I didn't allocate the memory corectly. I don't know. Please help. This is the only way I know how to code so if possible don't give me solutions that use the stl library and such.