Overloading classes problem...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lolcheelol
    New Member
    • Feb 2010
    • 25

    Overloading classes problem...

    I'm trying to run this program and it works, but I would like to clean it up and make it more acceptable. One of the problems I am having is that when the program runs when I put p1 to 100 to print the random numbers it will print them, but then the program crashes. When I put p1 = 1300, it runs because that is the number of actual integers in the file I'm reading from. Here is the source code.

    Code:
    class Data1{
    protected:
    int *p1;
    int size1;
    public:
    Data1();
    Data1(int);
    ~Data1();
    int randomData();
    int printData();
    }; // Data1
    
    Data1::Data1(){
    p1 = new int[1300];
    size1 = 1300;
    }
    Data1::Data1(int x1){
    p1 = new int[x1];
    size1 = x1;
    }
    
    Data1::~Data1(){
    delete [] p1;
    }
    
    Data1::randomData(){ 
    int i;
    for(i=0; i<size1; i++){
    *(p1+i) = random(899) + 100;
    }
    return 0;
    }
    
    
    Data1::printData(){
    int i;
    cout << "\n";
    for(i=0; i<size1; i++){
    cout << "\t" << *(p1+i);
    }
    return 0;
    }
    class Data2a: public Data1{
    protected:
    int forever;
    int ct;
    public:
    Data2a();
    Data2a(int);
    ~Data2a();
    int fileInput1();
    int fileOutput1();
    }; // data2
    Data2a::Data2a(){
    forever = 1;
    ct = 0;
    }
    Data2a::Data2a(int n1){
    p1 = new int[n1];
    size1 = n1;
    }
    
    Data2a::~Data2a(){
    delete[]p1;
    }
    
    Data2a::fileInput1(){
    ifstream in1("nums.dat");
    while(forever){ 
    in1 >> *(p1+ct); 
    if(*(p1+ct) == -1) break;
    ct++; 
    } 
    in1.close();
    cout << "\n\n\tFile size = " << ct;
    return ct;
    }
    
    Data2a::fileOutput1(){
    for(int i=0; i<ct; i++){
    cout << " " << *(p1+i);
    if((i%15==0)&&(i>0)) cout << "\n";
    if((i%150==0)&&(i>0)){
    cout << "\n\n\tEnter to continue...\n";
    getch();
    } // if
    } // FOR LOOP
    return 0;
    }
    
    int main(){
    clrscr();
    Data1 *d1;
    Data2a *d2;
    d1 = new Data1;
    d2 = new Data2a;
    
    d1->randomData();
    d1->printData();
    cout << "\n\n";
    d2->fileInput1();
    cout << "\n\n";
    d2->fileOutput1();
    
    
    delete d1;
    delete d2;
    cout << "\n\n\n\tComplete";
    return 0;
    } //MAIN
  • RedSon
    Recognized Expert Expert
    • Jan 2007
    • 4980

    #2
    First off I see hard coded values and magic numbers, which I don't like. What does your code look like in your 100 number example?

    Comment

    • lolcheelol
      New Member
      • Feb 2010
      • 25

      #3
      if i'm understanding you correctly, i would just change


      Code:
      Data1::Data1(){
       p1 = new int[1300];
       size1 = 1300;
      }
      to

      Code:
      Data1::Data1(){
       p1 = new int[100];
       size1 = 100;
      }

      Comment

      • RedSon
        Recognized Expert Expert
        • Jan 2007
        • 4980

        #4
        That's it? Okay, are you still having problems if you make those changes?

        Comment

        • lolcheelol
          New Member
          • Feb 2010
          • 25

          #5
          correct, when I do that it crashes after printing the 100 random numbers.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Probably it'a crashing in your destructors. Set a breakpoint and start debugging from that point. A code step-through should flush the problem out.

            Comment

            • lolcheelol
              New Member
              • Feb 2010
              • 25

              #7
              okay thanks for your help!

              Comment

              Working...