Solved

Destructors...!!

Posted on 2004-04-08
5
277 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_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

710 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