what's wrong with this function

November 17, 2010 at 18:14:40
Specs: Macintosh
it's an example from my c++ book but it doesn't work with me PLZ...thank u

#include <iostream.h>
#include <string.h>

class person {
protected:
char* ptr_name;
public:
person(char* np) {
int length = strlen(np);
ptr_name = new char[length+1];
strcpy(ptr_name, np);
}
virtual ~person() = 0
{
cout << "\nPerson Destructor";
if(ptr_name != NULL)
delete[] ptr_name;
}
virtual void put_data() {
cout << "\n Name = " << ptr_name;
}
};
//////////////////////////////////////////////////////
class grad_student : public person {
private:
char* ptr_topic;
public:
grad_student(char* n, char* t) :
person(n), ptr_topic(NULL) {
int length = strlen(t);
ptr_topic = new char[length+1];
strcpy(ptr_topic, t);
}
~ grad_student() {
if ( ptr_topic != NULL)
delete[] ptr_topic;
}
virtual void put_data() {
person::put_data();
cout << "\nThe Topic Is : " << ptr_topic;
}
};
//////////////////////////////////////////////////////

int main() {
int j;
const int total=3;
person* ptr_prs[3];

char name[40];
char topic[80];

for (j=0; j<total; j++) {
cout << "\nenter name : ";
cin >> name;
cout << "\nenter thesis topic : ";
cin >> topic;
ptr_prs[j] = new grad_student(name, topic);
}

for (j=0; j<total; j++) {
ptr_prs[j]->put_data();
}
for (j=0; j<total; j++) {
delete ptr_prs[j];
}
}


See More: whats wrong with this function

Report •

#1
November 18, 2010 at 06:15:45
virtual ~person() = 0
{

. . . Is not technically valid C++.

You have two options:
1) Remove the = 0 from the destructor.

virtual ~person()
{

2) Separate the definition from the deceleration.
	virtual ~person() = 0;
	virtual void put_data() { cout << "\n Name = " << ptr_name; }
};

person::~person() {
	cout << "\nPerson Destructor";
	if(ptr_name != NULL)
	delete[] ptr_name;
}

How To Ask Questions The Smart Way


Report •

#2
November 18, 2010 at 16:26:43
ok thank you --- maybe i have an old book with old codes ..

Report •
Related Solutions


Ask Question