Daca acest cod il rulez fara clase template, codul ruleaza perfect, in schimb daca implementez clasele template codul nu mai ruleaza.
De ce ?
Code: Select all
#include <iostream>
#include <cstring>
#pragma GCC diagnostic ignored "-Wwrite-strings"
using namespace std;
template <class T, class U, class V> class Persoana
{
private:
T *firstName;
T *secondName;
T *job;
T *email;
U age;
V phone;
public:
Persoana()
{
this->firstName = NULL;
this->secondName = NULL;
this->job = NULL;
this->email = NULL;
this->age = 0;
this->phone = "";
}
Persoana(T firstName[], T secondName[], T job[], T email[], U age, V phone)
{
this->firstName = new T[strlen(firstName)+1];
strcpy(this->firstName, firstName);
this->secondName = new T[strlen(secondName)+1];
strcpy(this->secondName, secondName);
this->job = new T[strlen(job)+1];
strcpy(this->job, job);
this->email = new T[strlen(email)+1];
strcpy(this->email, email);
this->age = age;
this->phone = phone;
}
~Persoana()
{
if (this->firstName != NULL)
delete[] firstName;
firstName = NULL;
if (this->secondName != NULL)
delete[] secondName;
secondName = NULL;
if (this->job != NULL)
delete[] job;
job = NULL;
if (this->email != NULL)
delete[] email;
email = NULL;
}
T *getFirstName()
{
return firstName;
}
T *getSecondName()
{
return secondName;
}
T *getJob()
{
return job;
}
T *getEmail()
{
return email;
}
U getAge()
{
return age;
}
V getPhone()
{
return phone;
}
template <class X, class Y, class Z> friend istream& operator>> (istream& in, Persoana<X,Y,Z> &box);
template <class X, class Y, class Z> friend ostream& operator<< (ostream& out, const Persoana<X,Y,Z> &box);
};
template <class X, class Y, class Z> istream& operator>> (istream& in, Persoana<X,Y,Z> &obj)
{
char temp[50];
cout << "\n Firstname: ";
if (obj.firstName != NULL)
delete[] obj.firstName;
in >> temp;
obj.firstName = new char[strlen(temp)+1];
strcpy(obj.firstName, temp);
cout << "\n Secondname: ";
if (obj.secondName != NULL)
delete[] obj.secondName;
in >> temp;
obj.secondName = new char[strlen(temp)+1];
strcpy(obj.secondName, temp);
cout << "\n Age: ";
in >> obj.age;
cout << "\n Job: ";
if (obj.job != NULL)
delete[] obj.job;
in >> temp;
obj.job = new char[strlen(temp)+1];
strcpy(obj.job, temp);
cout << "\n Email: ";
if (obj.email != NULL)
delete[] obj.email;
in >> temp;
obj.email = new char[strlen(temp)+1];
strcpy(obj.email, temp);
cout << "\n Phone: ";
in >> obj.phone;
return in;
}
template <class X, class Y, class Z> ostream& operator<< (ostream& out, Persoana<X,Y,Z> &obj)
{
out << "\n Firstname: " << obj.firstName << endl;
out << "\n Secondname: " << obj.secondName << endl;
out << "\n Age: " << obj.age << endl;
out << "\n Job: " << obj.job << endl;
out << "\n Email: " << obj.email << endl;
out << "\n Phone: " << obj.phone << endl;
return out;
}
int main()
{
Persoana<char, int, string> obj;
cin >> obj;
cout << obj;
return 0;
}