By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,694 Members | 1,270 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,694 IT Pros & Developers. It's quick & easy.

Please, help me. It's destructor problem

P: n/a
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;
}

Oct 13 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a

kw**********@gmail.com wrote:
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!):
std::string name; // you'll have to include <string>
Date birthday;
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

Oct 13 '06 #2

P: n/a
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;
}

Oct 13 '06 #3

P: n/a


On Oct 13, 10:18 am, "kwangbok....@gmail.com" <kwangbok....@gmail.com>
wrote:
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.

Oct 13 '06 #4

P: n/a

tragomaskhalos wrote:
On Oct 13, 10:18 am, "kwangbok....@gmail.com" <kwangbok....@gmail.com>
wrote:
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.
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

Oct 13 '06 #5

P: n/a
tragomaskhalos wrote:
> 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.
The birthday of the programmer, maybe? X-)

--
Salu2
Oct 13 '06 #6

P: n/a

kw**********@gmail.com wrote:
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>
#include <iostream>
#include <string>
>
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;
}

Oct 13 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.