Question

This is c++ programming and here is my code. I am getting an exception thrown on...

This is c++ programming and here is my code. I am getting an exception thrown on the destructor for permanentworker. Can you tell me why I am getting this exception?

#include <iostream>
#pragma warning(disable:4996)

class PermanentWorker
{
private:
   char *name;
   int salary;
public:
   PermanentWorker(const char* nam, int money) : salary(money)
   {
       name = new char[strlen(nam) + 1];
       strcpy(name, nam);
   }
   int getPay() const
   {
       return salary;
   }
   void showSalaryInfo()
   {
       std::cout << "name: " << name << std::endl;
       std::cout << "salary: " << getPay() << std::endl << std::endl;
   }
   ~PermanentWorker()
   {
       delete[] name;
       std::cout << "called PermanentWorker destructor" << std::endl;
   }
};

class EmployeeHandler
{
private:
   PermanentWorker* employeeList[50];
   int employeeNumber;
public:
   EmployeeHandler() : employeeNumber(0)
   {

   }
   void addEmployee(PermanentWorker* employee)
   {
       employeeList[employeeNumber++] = employee;
   }
   void showAllSalaryInfo() const
   {
       for (int i = 0; i < employeeNumber; i++)
       {
           employeeList[i]->showSalaryInfo();
       }
   }
   void showTotalSalary() const
   {
       int sum = 0;
       for (int i = 0; i < employeeNumber; i++)
       {
           sum += employeeList[i]->getPay();
       }
       std::cout << "salary sum: " << sum << std::endl;
   }
   ~EmployeeHandler()
   {
       for (int i = 0; i < employeeNumber; i++)
       {
           delete[] employeeList[i];
       }
   }
};

int main()
{
   EmployeeHandler handler;

   handler.addEmployee(new PermanentWorker("Dean", 3000));
   handler.addEmployee(new PermanentWorker("Joyce", 4000));
   handler.addEmployee(new PermanentWorker("Sunny", 2000));

   handler.showAllSalaryInfo();

   handler.showTotalSalary();
   return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Done

Mistake is you first deleted whole pointer of PermanentWorker and again calling destructor which access freed memory so error raises

Now its clear see the output

#include <iostream>
#include<cstring>
#pragma warning(disable:4996)
class PermanentWorker
{
private:
   char *name;
   int salary;
public:
   PermanentWorker(const char* nam, int money) : salary(money)
   {
       name = new char[strlen(nam) + 1];
       strcpy(name, nam);
   }
   char * getname(){
       return name;
   }
   int getPay() const
   {
       return salary;
   }
   void showSalaryInfo()
   {
       std::cout << "name: " << name << std::endl;
       std::cout << "salary: " << getPay() << std::endl << std::endl;
   }
   ~PermanentWorker()
   {
       delete []name;
     
       std::cout << "called PermanentWorker destructor" << std::endl;
   }
};

class EmployeeHandler
{
private:
   PermanentWorker* employeeList[50];
   int employeeNumber;
public:
   EmployeeHandler() : employeeNumber(0)
   {

   }
   void addEmployee(PermanentWorker* employee)
   {
       employeeList[employeeNumber++] = employee;
   }
   void showAllSalaryInfo() const
   {
       for (int i = 0; i < employeeNumber; i++)
       {
           employeeList[i]->showSalaryInfo();
       }
   }
   void showTotalSalary() const
   {
       int sum = 0;
       for (int i = 0; i < employeeNumber; i++)
       {
           sum += employeeList[i]->getPay();
       }
       std::cout << "salary sum: " << sum << std::endl;
   }
   ~EmployeeHandler()
   {
           for(int i=0;i<employeeNumber;i++){
               delete employeeList[i];
        }
   }
};

int main()
{
   EmployeeHandler handler;

   handler.addEmployee(new PermanentWorker("Dean", 3000));
   handler.addEmployee(new PermanentWorker("Joyce", 4000));
   handler.addEmployee(new PermanentWorker("Sunny", 2000));

   handler.showAllSalaryInfo();

   handler.showTotalSalary();
   return 0;

}

name: Dean salary: 3000 name: Joyce salary: 4000 name: Sunny salary: 2000 salary sum: 9000 called PermanentWorker destructor

Add a comment
Know the answer?
Add Answer to:
This is c++ programming and here is my code. I am getting an exception thrown on...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • When running the program at the destructor  an exception is being thrown. Can someone help me out?...

    When running the program at the destructor  an exception is being thrown. Can someone help me out? vararray.h: #ifndef VARARRAY_H_ #define VARARRAY_H_ class varArray { public:    varArray(); // void constructor    int arraySize() const { return size; } // returns the size of the array    int check(double number); // returns index of element containg "number" or -1 if none    void addNumber(double); // adds number to the array    void removeNumber(double); // deletes the number from the array   ...

  • Hi there, I am working on a binary search tree code in c++. The program must...

    Hi there, I am working on a binary search tree code in c++. The program must store and update students' academic records, each node includes the student name, credits attempted, credits earned and GPA. I have made some progress with the code and written most of the functions in the .cpp file (already did the .h file) but i am struggling with what to put in the main and how to put an update part in the insert function. I...

  • I am getting a seg fault with my huffman tree. I'm not sure if it's my...

    I am getting a seg fault with my huffman tree. I'm not sure if it's my deconstructors but also getting some memory leaks. Can some one help me find my seg fault? It's in c++, thanks! //#ifndef HUFFMAN_HPP //#define HUFFMAN_HPP #include<queue> #include<vector> #include<algorithm> #include<iostream> #include <string> #include <iostream> using namespace std; class Node { public:     // constructor with left and right NULL nodes     Node(char charTemp, int c) {       ch = charTemp;       count = c;       left...

  • "Function does not take 0 arguments". I keep getting this error for my last line of...

    "Function does not take 0 arguments". I keep getting this error for my last line of code: cout << "First Name: " << employee1.getfirstName() << endl; I do not understand why. I am trying to put the name "Mike" into the firstName string. Why is my constructor not working? In main it should be set to string firstName, string lastName, int salary. Yet nothing is being put into the string. #include<iostream> #include<string> using namespace std; class Employee {    int...

  • Need to implement Account.cpp and AccountManager.cpp code //Account.hpp #ifndef _ACCOUNT_HPP_ #define _ACCOUNT_HPP_ #include <string> using std::string;...

    Need to implement Account.cpp and AccountManager.cpp code //Account.hpp #ifndef _ACCOUNT_HPP_ #define _ACCOUNT_HPP_ #include <string> using std::string; class Account { public: Account(); Account(string, double); void deposit(double); bool withdraw(double); string getName() const; double getBalance() const; private: string name; double balance; }; #endif ////////////////////////////////////////////// //AccountManager.hpp #ifndef _ACCOUNT_MANAGER_HPP_ #define _ACCOUNT_MANAGER_HPP_ #include "Account.hpp" #include <string> using std::string; class AccountManager { public: AccountManager(); AccountManager(const AccountManager&); //copy constructor void open(string); void close(string); void depositByName(string,double); bool withdrawByName(string,double); double getBalanceByName(string); Account getAccountByName(string); void openSuperVipAccount(Account&); void closeSuperVipAccount(); bool getBalanceOfSuperVipAccount(double&) const;...

  • c++, I am having trouble getting my program to compile, any help would be appreciated. #include...

    c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...

  • Hello, I'm looking to modify my fraction program to include constructors and destructors. Here are all...

    Hello, I'm looking to modify my fraction program to include constructors and destructors. Here are all the instructions: Create constructors default two argument three argument copy constructor Create a destructor. The destructor should set whole and numerator to zero and denominator to one. Add cout statements to the constructors and the destructor so you know when it's getting executed. Add system ("pause") to the destructor so you know when it executes. Only positive values allowed If denominator is 0 set...

  • Am I getting this error because i declared 'n' as an int, and then asking it...

    Am I getting this error because i declared 'n' as an int, and then asking it to make it a double? This is the coude: #include "stdafx.h" #include <iostream> #include <fstream> #include <string> #include <algorithm> #include <vector> using namespace std; void sort(double grades[], int size); char calGrade(double); int main() {    int n;    double avg, sum = 0;;    string in_file, out_file;    cout << "Please enter the name of the input file: ";    cin >> in_file;   ...

  • I need help with the code below. It is a C program, NOT C++. It can...

    I need help with the code below. It is a C program, NOT C++. It can only include '.h' libraries. I believe the program is in C++, but it must be a C program. Please help. // // main.c // float_stack_class_c_9_29 // // /* Given the API for a (fixed size), floating point stack class, write the code to create a stack class (in C). */ #include #include #include #include header file to read and print the output on console...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT