Thread: Dynamic LL && inheritance, help?

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    33

    Dynamic LL && inheritance, help?

    I havent got time atm to up load my code,
    but basically I am trying to create a dynamic linked list using derived classes
    ( 3 of them ).

    I have a base-class pointer that I am using to start my list by reference (&).
    But using references means my '*first' pointer is over-written.

    WTS for my code-upload...

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    So, you are using a std::list<Base*>, where Base is the base class of this hierarchy?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    33
    HEADER FILE :
    Code:
    # include <iostream.h>
    # include <process.h>
    # include <stdlib.h>
    # include <string.h>
    # include <ctype.h>
    
    class  library
    {
    private:
             int ID;
    
    public:
             library();
            ~library();
    
             void input_ID( int id );
             void output_ID();
    
             virtual void enter( int id ) { input_ID( id ); };
             virtual void display()       { output_ID();    };
    
             class library *nxLb;
    };
    
    library::library()
    {
            ID       =   0; 
            nxLb   =   0;
    };
    
    library::~library()
    {
    };
    
    void library::input_ID( int id )
    {
           ID   =  id;
    
           cout << "\n\n ID number is : "
                   << ID;
           return;
    };
    
    void library::output_ID()
    {
           cout << "\n "
                   << ID;
           return;
    };
    
    // ---------------------------------------------------------
    
    class book : public library
    {
    private:
           char name[25];
    
    public:
           book();
          ~book();
    
           void input_name();
           void output_name();
    
           void enter( int id ) { input_ID( id );  input_name(); };
           void display()       { output_ID(); output_name();    };
    
           class book *nxBk;
    };
    
    book::book()
    {
           memset( name, '\0', 25 );
           nxBk  =   0;
    };
    
    book::~book()
    {
    };
    
    void book::input_name()
    {
           cout << "\n\n Please enter NAME : ";
           cin    >> name;
           return;
    }
    
    void book::output_name()
    {
          cout << "\n " 
                  << name;
          return;
    };
    
    // ---------------------------------------------------------
    SOURCE to follow, WTS please...

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    iostream.h is deprecated iostream should be used

    why do you use C-strings and not C++ strings (std::string class)

    not very good idea to encapsulate input output inside the class and bind it to iostreams, what if the user of the class whats to export/import data to/from the file or even DB connection?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Just a thought on the layout. From what I know about "books" and "libraries", books are not a type of library, libraries contain books. Of course I don't know the intent of the whole project, just making an observation.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  6. #6
    Registered User
    Join Date
    Aug 2008
    Posts
    33
    Right,
    In my defence: The whole point of this (naff) program was to create a dynamic linked list composed of derived classes.
    Its a bit of fun/research on my own part, as im still learning...

    Here is whats left of the program, the source file:

    Code:
    # include "library.h"
    
    // ---------------------------------------------------------
    // main() FUNCTIONS
    int   menu();
    bool  newOB( int id );
    bool  newBK( int id );
    //bool  newDVD( int id );
    //bool  newCD( int id );
    void  show_all();
    //bool  show_type();
    
    // ---------------------------------------------------------
    // GLOBAL VARIABLES
    int   total  =  0;
    
    // ---------------------------------------------------------
    // CLASS INSTANCE
    library  *first;
    library  *current;
    book      BKtemp;
    
    
    // ---------------------------------------------------------
    int   main()
    {
      int   choice  =  0;
      bool  EXIT    =  false;
    
      do
      {
        choice  =  menu();
    
        switch( choice )
        {
        case 1 : if( newOB( total+1 ) )
                 {  total++; }
                 else
                 {}
                 break;
        case 4 : show_all();
                 system("pause");
                 break;
        default: EXIT  = true;
                 break;
        }
    
      } while( !EXIT );
    
    
      return  0;
    };
    
    // ---------------------------------------------------------
    int   menu()
    {  
      char   choice  =  'X';
      int    option  =   0;
      bool   repeat  =   true;
    
      do
      {
        system("cls");
        cout << "\n"  
                << "\n =================================="
                << "\n   |                AESW DESIGN SYSTEMS                | "
                << "\n   |             Library Rental System  V1.0             | "
                << "\n ================================== "
                << endl << endl;
    
         cout << "        Please choose one of the following options: "
                 << "\n =================================="
                 << "\n             1       :      CREATE NEW OBJECT.       "
    //          << "\n             2       :      DELETE EXISTING ITEM.  "
    //          << "\n             3       :      FIND EXISTING ITEM.    "
                 << "\n             4       :      DISPLAY CONTENTS       ( " << total << " )"
    //           << "\n             7       :      SAVE CHANGES & EXIT.   "
                 << "\n            'X'      :      QUIT.                    "
                 << "\n =================================="
                 << endl << endl << "             ";
           cin  >> choice;
    
           if( isdigit( choice ) )
           {
              option  =  atoi( &choice );
              if( 0 < option && option < 8 )
              {
                 repeat  =  false;
              }
    	    else
          {
    		    repeat  =  true;
            cerr << "\n\tERROR:Invalid MENU Input:  " << choice << "\n\t";
            system("pause");
          }
    	  }
    	  else if( choice == 'X' || choice == 'x' )
    	  {
    	    // EXIT PROCEDURE
    		  repeat  =  false;
    		  option  =  0;
    	  }
    	  else
    	  {
          repeat  =  true;
          cerr << "\n\tERROR:Invalid MENU Input:  " << choice << "\n\t";
          system("pause");
        }
    
    	}while( repeat );
    
    	return  option;
    };
    
    // ---------------------------------------------------------
    
    bool  newOB( int id )
    {
      char   choice  =  'X';
    	int    option  =   0;
    	bool   repeat  =   true;
    
      do
      {
        system("cls");
    
        cout << "\n        Please choose the type of Object: "
             << "\n   ====================================================="
             << "\n             1       :      BOOK / MAGAZINE. "
    	       << "\n             2       :      DVD  / VHS. "
    	       << "\n             3       :      CD   / CASSETTE TAPE. "
             << "\n   ====================================================="
             << endl << endl << "             ";
        cin  >> choice;
    
        if( isdigit( choice ) )
    	  {
    	    option  =  atoi( &choice );
    	    if( 0 < option && option < 8 )
    	    {
    	      repeat  =  false;
    	    }
    	    else
          {
    		    repeat  =  true;
            cerr << "\n\tERROR:Invalid MENU Input:  " << choice << "\n\t";
            system("pause");
          }
    	  }
    	  else if( choice == 'X' || choice == 'x' )
    	  {
    	    // EXIT PROCEDURE
    		  repeat  =  false;
    		  option  =  0;
    	  }
    	  else
    	  {
          repeat  =  true;
          cerr << "\n\tERROR:Invalid MENU Input:  " << choice << "\n\t";
          system("pause");
        }
    
    	}while( repeat );
    
      switch( option )
      {
      case 1 : repeat  =  newBK( id );
               break;
      default: break;
      }       
    
      return  repeat;
    };
    
    // ---------------------------------------------------------
    bool  newBK( int id )
    {
      bool  ok  =  true;
    
    // !! FIX ME !! FIX ME !! FIX ME !! FIX ME !! FIX ME !! FIX ME !! FIX ME 
      BKtemp.enter( id );
    // FIX => references to pointers are over-written!!
      if( first  ==  NULL )
      {
        first    =   new book();
        first    =&  BKtemp;
        current  =   new book();
        current  =&  BKtemp;
        ok       =   true;
      }
    // FIX => references to pointers are over-written!!
      else if( current && (current->nxLb  ==  NULL) )
      {
        current->nxLb  =   new book();
        current        =   current->nxLb;
        current        =&  BKtemp;
        ok             =   true;
      }
    // FIX => references to pointers are over-written!!
      else
      {
        ok  =  false;
      }
    // !! FIX ME !! FIX ME !! FIX ME !! FIX ME !! FIX ME !! FIX ME !! FIX ME 
    
      if( ok && (current->nxLb  ==  NULL) )
      {
        return  true;
      }
      else
      {
        return  false;
      }
    };
    
    // ---------------------------------------------------------
    void  show_all()
    {
      library     *show;
      show     =   new library();
      show     =   first;
    
      while( show )
      {
        cout << "\n";
        show->display();
        show   =   show->nxLb;
      }
    
      return;
    };
    Bugger it. I give up trying to make this look neater...
    Well, here it is. Im ready for your critisim now...
    But please remember that im looking for a way to save my '*first' pointer from being over-written...

  7. #7
    Registered User
    Join Date
    Aug 2008
    Posts
    33
    Quote Originally Posted by vart View Post
    iostream.h is deprecated iostream should be used

    why do you use C-strings and not C++ strings (std::string class)

    not very good idea to encapsulate input output inside the class and bind it to iostreams, what if the user of the class whats to export/import data to/from the file or even DB connection?
    So you think I should use over-loaded functions to perform input/out on my classes...?
    ...Is that what your saying?

    Also, Im using VB v.6 so <iostream.h> is what im stuck with, I think thats what you mean. If not, pray tell...
    Last edited by Else; 03-11-2009 at 05:40 PM.

  8. #8
    Registered User
    Join Date
    Aug 2008
    Posts
    33
    I owe everybody who replyed to this thread an apology.
    I was being dense and writing hurried and messy code...
    I have since corrected the issue and can create a Dynamic Linked List in my sleep
    (wow, I hear you say).

    Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 03:03 PM
  2. Pointer Mayhem
    By thomas41546 in forum C Programming
    Replies: 3
    Last Post: 05-11-2008, 10:25 AM
  3. Inheritance and Dynamic Memory Program Problem
    By goron350 in forum C++ Programming
    Replies: 1
    Last Post: 07-02-2005, 02:38 PM
  4. inheritance and performance
    By kuhnmi in forum C++ Programming
    Replies: 5
    Last Post: 08-04-2004, 12:46 PM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM