Solved

Destructors...!!

Posted on 2004-04-08
5
275 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

776 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