Please, help me. It's destructor problem

K

kwangbok.kim

Hi.
I'm major in computer science.
While studying with the what kind of document, the problem got.
Please help me.
The lower part is the program...
Programming with visual c++ 6.0 at windows xp
--------------------------------------------------------------------------------------------
#include <iostream.h>
#include <string.h>

class Date
{
int d, m, y;
public:
Date(int dd=5, int mm=5, int yy=2005) : d(dd), m(mm), y(yy)
{
cout << "Date Object Created .. " << endl;
}
~Date()
{
cout << "Date : " << y << "/" << m << "/" << d << endl;
}
void print();
};

void Date::print()
{ cout << "Date : " << y << "/" << m << "/" << d << endl; }

class Person
{
char *name;
Date *birthday;
public:
Person(char *n, Date *d);
~Person()
{
delete[] name;
cout << "Person Object Destructed .. " << endl;
}
void print();
};

Person::person(char *n, Date *d) : birthday(d)
{
name = new char[strlen(n) +1];
strcpy(name,n);
cout << "Person Object Created .. " << endl;
}

void Person::print()
{
cout << "Name : " << name << endl;
cout << "Birthday : ";
birthday->print();
cout << endl;
}

int main(void)
{
Date day(1,1,1973);
Person hong("Hong, Gin-Dong", &day);
hong.print();

Person jang = hong;
jang.print();

return 0;
}
 
P

peter koch

Hi.
I'm major in computer science.
While studying with the what kind of document, the problem got.
Please help me.
The lower part is the program...
Programming with visual c++ 6.0 at windows xp
--------------------------------------------------------------------------------------------
#include <iostream.h>
#include <string.h>
[snip]
class Person
{
char *name;
Date *birthday;

Why use pointers? Pointers are difficult to use and quite often the
wrong solution. Use the standard library (as your teacher should have
told you to!):
public:
Person(char *n, Date *d);
~Person()
{
delete[] name; This line is not needed.
cout << "Person Object Destructed .. " << endl;
}
void print();
};

Person::person(char *n, Date *d) : birthday(d)
Person::person(std::string const& s, Date& d) : name(s),birthday(d)
{
name = new char[strlen(n) +1];
strcpy(name,n); Two lines above now not needed
cout << "Person Object Created .. " << endl;
}
[snip]

So in short: learn to use the standard library and avoid using pointers
until you get a problem that you can't solve without them.

/Peter
 
L

Leo jay

Hi.
I'm major in computer science.
While studying with the what kind of document, the problem got.
Please help me.
The lower part is the program...
Programming with visual c++ 6.0 at windows xp
--------------------------------------------------------------------------------------------
#include <iostream.h>
#include <string.h>

class Date
{
int d, m, y;
public:
Date(int dd=5, int mm=5, int yy=2005) : d(dd), m(mm), y(yy)
{
cout << "Date Object Created .. " << endl;
}
~Date()
{
cout << "Date : " << y << "/" << m << "/" << d << endl;
}
void print();
};

void Date::print()
{ cout << "Date : " << y << "/" << m << "/" << d << endl; }

class Person
{
char *name;
Date *birthday;
public:
Person(char *n, Date *d);
~Person()
{
delete[] name;
cout << "Person Object Destructed .. " << endl;
}
void print();
};

Person::person(char *n, Date *d) : birthday(d)
{
name = new char[strlen(n) +1];
strcpy(name,n);
cout << "Person Object Created .. " << endl;
}

void Person::print()
{
cout << "Name : " << name << endl;
cout << "Birthday : ";
birthday->print();
cout << endl;
}

int main(void)
{
Date day(1,1,1973);
Person hong("Hong, Gin-Dong", &day);
hong.print();

Person jang = hong;
jang.print();

return 0;
}


since there are pointers in the class Person, and you want to assign
values from one Person to another, you should write both copy
construction and copy assignment to
prevent multiple pointers point to the same area.

so, add following code(copy construction) to class Person, the program
will be ok.
Person(const Person& p) {
name = new char[strlen(p.name)+1];
strcpy(name, p.name);

birthday = p.birthday;
}
 
T

tragomaskhalos

Hi.

class Date
{
int d, m, y;
public:
Date(int dd=5, int mm=5, int yy=2005) : d(dd), m(mm), y(yy)
{
cout << "Date Object Created .. " << endl;
}

How in the name of god's green earth is 5th May 2005 a sensible default
value for a date ? Enquiring minds want to know.
 
G

Gavin Deane

tragomaskhalos said:
How in the name of god's green earth is 5th May 2005 a sensible default
value for a date ? Enquiring minds want to know.

Not to mention the fact that this defines _four_ different constructors
_and_ an implicit conversion from int to Date, which might well be not
what is wanted at all.

Gavin Deane
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

tragomaskhalos said:
How in the name of god's green earth is 5th May 2005 a sensible default
value for a date ? Enquiring minds want to know.

The birthday of the programmer, maybe? X-)
 
S

Salt_Peter

Hi.
I'm major in computer science.
While studying with the what kind of document, the problem got.
Please help me.
The lower part is the program...
Programming with visual c++ 6.0 at windows xp

#include <iostream>
#include said:
class Date
{
int d, m, y;
public:
Date(int dd=5, int mm=5, int yy=2005) : d(dd), m(mm), y(yy)
{
cout << "Date Object Created .. " << endl;
}
~Date()
{
cout << "Date : " << y << "/" << m << "/" << d << endl;
}
void print();
};

void Date::print()
{ cout << "Date : " << y << "/" << m << "/" << d << endl; }

class Person
{
char *name;
Date *birthday;
public:
Person(char *n, Date *d);
~Person()
{
delete[] name;
cout << "Person Object Destructed .. " << endl;
}
void print();
};

Person::person(char *n, Date *d) : birthday(d)
{
name = new char[strlen(n) +1];
strcpy(name,n);
cout << "Person Object Created .. " << endl;
}

void Person::print()
{
cout << "Name : " << name << endl;
cout << "Birthday : ";
birthday->print();
cout << endl;
}

int main(void)
{
Date day(1,1,1973);
Person hong("Hong, Gin-Dong", &day);
hong.print();

Person jang = hong; // its actually a copy
jang.print();

return 0;
}

Note how much easier and safe it is to code without pointers. Specially
when you use pointers to one character to store an array of characters.
Lets face it, does char* name point to a character or an array or
characters? Answer: the compiler does not know. Why deal with the
amibiguity?
pointers == bugs <- remember that
Since the code above is using copy construction, you are in need of an
appropriate copy constructor.

class Person
{
std::string name;
Date birthday;
public:
Person(std::string s, int d, int m, int y);
Person(const Person& r_copy);
~Person();
void print() const; // is not modifying Person, make it const
};

Person::person(std::string s, int d, int m, int y)
: name(s), Date(d, m, y)
{
std::cout << "Person()\n";
}

Person::person(const Person& r_copy)
{
std::cout << "Person(const Person& copy)\n";
name = r_copy.name;
birthday = r_copy.birthday;
}

Person::~Person()
{
std::cout << "~Person()\n";
}

void Person::print() const
{
std::cout << "Name : " << name;
std::cout << "\nBirthday : ";
birthday.print();
std::cout << std::endl;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,416
Messages
2,571,562
Members
48,797
Latest member
shadowoftheunknown

Latest Threads

Top