Default constructor is called only when you create an object using parameter-less constructor...
I have found a few mistakes or possible improvements in your code:
#define MAX 10
It would be preferable to uses a constant instead. Macros should be avoided whenever possible as they don't respect scope.
Person(const Person &p)
{
if (name != NULL)
{
delete name;
name = NULL;
}
if (surname != NULL)
{
delete surname;
surname = NULL;
}
This code is totally useless and in fact wrong. Since copy constructor is called on a new object which is uninitialized, you cannot delete name or surname.
~Person()
{
if (name != NULL)
{
delete name;
name = NULL;
}
if (surname != NULL)
{
delete surname;
surname = NULL;
}
age = 0.0L;
cout << "destructor costructor activated\n";
}
Most of previous code is essentially useless. When an object is destroyed, it cannot be legally used afterward so it pointless to clear all variables. You could simply do that instead:
~Person()
{
delete[] name;
delete[] surname;
cout << "destructor costructor activated\n";
}
void setName(char*);
void setSurname(char*);
Person *operator = (Person*);
friend int calculateBirthYear(Person &);
In previous function you should add some const modifiers and also, you should use standard prototype for
=operator
. It should be
Person& operator=(const Person &);
Also calculateBirthYear should not be friend. You should have a public member to get the age. By the way, the function cannot be implemented correctly because you need to know the current date and the birthday to properly determine the age.
Person *Person::operator = (Person *p) {
}
Your implementation is not exception safe if memory cannot be allocated... So are some other functions. Generally, you want to do new allocation before deleting old stuff so that if it does not works, your object will still be in a stable state. Using RAII idiom is a good pattern to follows.
name = new char[strlen(onoma) + 1];
strcpy(name, onoma);
This code is repetitive. Why not add a static member function for that purpose?
int main(void)
{
}
The
main
function has some memory leaks (
Person
in the array are not deleted).
Also, there are still a bunch of hard-coded constants. For example if
MAX
is modified then neither the message nor the range check are adjusted.
As a side note, it would be better to use
std::vector
and
std::string
in your program to avoid a lot of explicit memory management and have code that it is exception-safe.