Thread: Inheritance and Dynamic Memory Program Problem

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    73

    Inheritance and Dynamic Memory Program Problem

    Hi everyone,

    My program which involves Inheritance is crashing and I think it has to do with the destructors which are making it crash. Me and a few other people are having the same problem with this program.

    Here are the program requirements which MUST be followed:

    1. I need to write the definitions for the declared multi-argument constructor, copy constructor, destructor, and Display functions for a base class called OfficeSupply. The class has four private data members which must be intialized by the specified constructor when the object is created. Note that the descrip data member is of type char*. When I initialize this member, I must use dynamic memory allocation to reserve blocks in memory just large enough to hold the argument (I use strlen() and strcpy() functions to accomplish this). Similarly, the destructor must release this memory. The Display function should then print out the data members.

    2. I then need to derive a Pen class from the OfficeSupply base class. The Pen class adds two private data members of type char*: pColor to indicate pen colour, and iColor to indicate ink color. I need to use dynamic memory management to allocate and release memory for these members (same way I did for the base class). I need to include overloaded constructors (multi-argument and copy) for member initialization. I must override the Display function from OfficeSupply to print the new class members in addition to those inherited from the parent (OfficeSupply) class.

    According to my code, the logic and how I go about accomplishing the desired results looks OK but the program is consistently crashing and I am not sure of why that is happening. I think it has something to do with the destructors that is causing the instability but that is just a guess on my part.

    If anyone can help me out with dissecting this problem, that would be great.

    Here is my code for this program:

    Code:
    #include <iostream.h>
    #include <conio.h>
    #include <iomanip.h>
    #include <string.h>
    
    class OfficeSupply {
    
       private:
          char* descrip;
          char* ptr;
          long stockNo;
          int  quant;
          float price;
    
       public:
          OfficeSupply(char*, long , int, float);
          OfficeSupply();
          ~OfficeSupply();
          char* getdescrip();
          long getstock();
          int getquant();
          float getprice();
          void Display();
          void Increase(int);
          void Decrease(int);
          void Reprice(float);
    };
    
    OfficeSupply::OfficeSupply() {}
    
    OfficeSupply::OfficeSupply(char* description, long stockNumber, int initQuantity, float initPrice) {
       int len = strlen(descrip);
       ptr = new char[len+1];
       strcpy(ptr, descrip);
       ptr = description;
       stockNo = stockNumber;
       quant = initQuantity;
       price = initPrice;
    }
    
    OfficeSupply::~OfficeSupply() {
       delete[] ptr;
    }
    
    char* OfficeSupply::getdescrip() {
       return ptr;
    }
    
    long OfficeSupply::getstock() {
       return stockNo;
    }
    
    int OfficeSupply::getquant() {
       return quant;
    }
    
    float OfficeSupply::getprice() {
       return price;
    }
    
    void OfficeSupply::Display() {
       cout << ptr << endl;
       cout << stockNo << endl;
       cout << quant << endl;
       cout << price << endl;
    }
    
    void OfficeSupply::Increase(int amt) {
       quant += amt;
    }
    
    void OfficeSupply::Decrease(int amt) {
       quant -= amt;
    }
    
    void OfficeSupply::Reprice(float newPrice) {
       price = newPrice;
    }
    
    class Pen : public OfficeSupply {
    
       private:
          char* pColor;
          char* iColor;
          char* ptr2;
          char* ptr3;
          char* ptr4;
    
       public:
          Pen(char*, char*, char*, long, int, float);
          ~Pen();
          char* getpColor();
          char* getiColor();
    };
    
    Pen::Pen(char* penColor, char* inkColor, char* descrip, long stockNo, int quant, float price) : OfficeSupply(descrip, stockNo, quant, price) {
       ptr2 = new char[strlen(pColor)+1];
       strcpy(ptr2, pColor);
       ptr2 = penColor;
       ptr3 = new char[strlen(iColor)+1];
       strcpy(ptr3, iColor);
       ptr3 = inkColor;
    }
    
    Pen::~Pen() {
       delete[] ptr2;
       delete[] ptr3;
    }
    
    char* Pen::getpColor() {
       return ptr2;
    }
    
    char* Pen::getiColor() {
       return ptr3;
    }
    
    int main() {
       OfficeSupply supp1("Hello", 20, 45, 5.60);  // base object
       Pen supp2("BLUE", "BLACK", "BIC", 25, 50, 7.64); // derived object
    
       cout << "Supply #1" << endl << endl;
       cout << "Description: " << supp1.getdescrip() << endl;
       cout << "Stock Number: " << supp1.getstock() << endl;
       cout << "Quantity: " << supp1.getquant() << endl;
       cout << "Price: $" << supp1.getprice() << endl << endl;
    
       cout << "Supply #2" << endl << endl;
       cout << "Pen Color: " << supp2.getpColor() << endl;
       cout << "Ink Color: " << supp2.getiColor() << endl;
       cout << "Description: " << supp2.getdescrip() << endl;
       cout << "Stock Number: " << supp2.getstock() << endl;
       cout << "Quantity: " << supp2.getquant() << endl;
       cout << "Price: $" << supp2.getprice() << endl << endl;
       getche();
       return 0;
    }
    The problem is that the program is crashing and my guess is that it has something to do with how the memory is allocated for the char* types and then released within the destructors.

    Any help would be greatly appreciated. Thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    It's your constructor which is the problem

    > ptr = description;
    You've already allocated memory, then copied it.
    This just trashes that with a pointer to some memory you didn't allocate, so BOOM happens when you try and free it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic LL && inheritance, help?
    By Else in forum C++ Programming
    Replies: 7
    Last Post: 07-28-2009, 02:46 AM
  2. Problem with pointers to dynamic objects
    By mike_g in forum C++ Programming
    Replies: 20
    Last Post: 04-25-2007, 01:16 PM