samedi 27 décembre 2014

Virtual Destructor Not called in Base as well as Derived Class

The following code involves 4 classes.




  1. The bases class is the Person Class and there are two Derived classes Student and Lecturer. Each person supports two functions: toString() and type(). Type() returns the name of that class whereas toString() prints the information of the instance (Student or Lecturer).




  2. Person is an abstract class but both Student and Lecturer are concrete classes.




I've implemented the above two functionalities.



  1. Many lecturers will share the pointer to the same SalaryTable, that there could be other SalaryTables (supporting the same functionality) and that the lecturers do not own the SalaryTable. Also, each one of them has some extra functions.


Can anyone explain me the 3rd point alone? As far as I know, I made use of the SalaryTable Pointer given in the Lecturer constructor and assigned it to the SalaryTable pointer (salaryTable_) which I've added in Lecturer.h. Then I return the salary using salaryTable_->annualSalary(grade_). And in the destructor of ~Lectuere(), I do a delete salaryTable_.


Is it the right way to do so? When I do it, only the ~Salary() destructor is getting called and both the Base class destructor (~Person()) and the derived class destructors (~Student() & ~Lecturer()) are not called. Can anyone explain me where am I wrong please?


SalaryTable.h



#ifndef SALARYTABLE_H_
#define SALARYTABLE_H_

class SalaryTable {
public:
SalaryTable();
~SalaryTable();

unsigned int annualSalary(unsigned int grade) const;
};

#endif /* SALARYTABLE_H_ */


Person.h



#ifndef PERSON_H_
#define PERSON_H_

#include <string>
#include <iosfwd>
#include <vector>

#include "SalaryTable.h"

using std::vector;
using std::string;

class Person {
public:
Person() = delete;
Person(const Person&) = delete;
Person(Person&&) = delete;

Person(const char* name);
Person(const std::string& name);
virtual ~Person();

// Return the name of the Person
// Should be supported by all Persons.
std::string name() const;

virtual std::string toString() const=0;

virtual std::string type() const=0;

friend std::ostream& operator<<(std::ostream&, const Person&);
private:
std::string name_;
};


Student.h



class Student: public Person {
public:
Student() = delete;
Student(const Student&) = delete;
Student(Student&&) = delete;

Student(const char* name, unsigned int studentId);
Student(const std::string& name, unsigned int studentId);
virtual ~Student();

void addMCF(const std::string&);
std::string MCF(unsigned int);

unsigned int id() const;

std::string toString() const;
std::string type() const;

private:
unsigned int studentId_;
vector<string> vec_;
};


Lecturer.h



class Lecturer: public Person {
public:
Lecturer() = delete;
Lecturer(const Lecturer&) = delete;
Lecturer(Lecturer&&) = delete;

Lecturer(const char* name, const char* teaches, unsigned int grade,
SalaryTable*);
Lecturer(const std::string& name, const std::string& teaches,
unsigned int grade, SalaryTable*);
virtual ~Lecturer();

void increaseGrade();
unsigned int salary() const;

void changeModule(const std::string& newModule);
std::string teaches() const;

std::string toString() const;
std::string type() const;

private:
string teaches_;
string module_;
unsigned int grade_;
SalaryTable* salaryTable_;

};

#endif /* PERSON_H_ */

Aucun commentaire:

Enregistrer un commentaire