Solved

Destructors...!!

Posted on 2004-04-08
5
274 Views
Last Modified: 2010-04-01
Where should I put my delete ??
It doesn't work if I put it in ma base class destructor!

class Personne
{
public:
      Personne(char *s)
      {
            name = new char[strlen(s+1)];
            strcpy(name,s);
      }
      void print()
      {
            cout << "Je m'appelle " << name << endl;
      }
      ~Personne()
      {
                               //PROBLEM
                     delete  []name;
            name = NULL;      
      }

protected:
      char *name;
};

class Etudiant : public Personne
{
public:
      Etudiant(char* s, float m):Personne(s),moyenne(m)
      {
      }
      void print()
      {
            cout << "Je m'appelle " << name <<" et j'ai une moyenne de "
                  << moyenne << endl;
      }
      ~Etudiant()
      {}

private:
      float moyenne;
};

class Prof:public Personne
{
public:
      Prof(char *s,int n):Personne(s),publs(n)
      {
      }
      void print()
      {
            cout << "Je m'appelle " << name << " et j'ai publie "
                  << publs << " ouvrages" << endl;
      }
      
      ~Prof()
      {}
private:
      int publs;

};
0
Comment
Question by:Prog_
5 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 65 total points
ID: 10782769
Make destructor virtual:

virtual ~Personne()
0
 
LVL 14

Assisted Solution

by:wayside
wayside earned 60 total points
ID: 10782779
You have to make the destructor virtual in the base class.
0
 

Author Comment

by:Prog_
ID: 10782780
Sorry...
the problem was strlen(s)+1 and not strlen(s+1)....
it was a typo error...
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10782853
AlexFM,
waysid,

Why does he need to make the base class destructor virtual
This is only required if he has a virtual function in his class

Amit
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 10782914
It was just guess (not successful). You need virtual destructor in this case:

Personne* p = new Prof("abc", 1);

delete p;       // Prof destructor is called only if it is virtual
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now