Change 2 classes into 1, possible?

This is a discussion on Change 2 classes into 1, possible? within the C++ Programming forums, part of the General Programming Boards category; I have 2 .h files which contain 2 classes, List and a ListNode. I want to change both classes into ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    17

    Change 2 classes into 1, possible?

    I have 2 .h files which contain 2 classes, List and a ListNode. I want to change both classes into 1 class, List. Is this possible?

    Code:
    #ifndef LIST_H
    #define LIST_H
    
    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;
    
    #include "ListNode.h"
    
    template< typename T >
    class List
    {
    public:
    	List()
    		: m_pFirst( 0 ), m_pLast( 0 ) 
    	{
    	}
    
    	~List();
    
    	void insertAtFront( const T& data );
    	void insertAtBack( const T& data );
    	bool removeFromFront( T& data );
    	bool removeFromBack( T& data );
    
    	bool isEmpty() const
    	{
    		return m_pFirst == 0;
    	}
    
    	void print() const;
    
    private:
    	ListNode< T >* m_pFirst;
    	ListNode< T >* m_pLast;
    
    	ListNode< T >* getNewNode( const T& data )
    	{
    		ListNode< T >* pNewNode = new ListNode< T >( data );
    		return pNewNode;
    	}
    
    };
    
    //DECLARE INLINE FUNCTIONS HERE
    template< typename T >
    List< T >::~List()
    {
    	if ( !isEmpty() )
    	{
    		cout << "Destroying nodes ...\n";
    
    		ListNode< T > *currentPtr = m_pFirst;
    		ListNode< T > *tempPtr;
    
    		while ( currentPtr != 0 )
    		{
    			tempPtr = currentPtr;
    			cout << tempPtr->m_Data << '\n';
    			currentPtr = currentPtr->m_pNext;
    			delete tempPtr;
    		}
    	}
    
    	cout << "All nodes destroyed\n\n";
    	getch();
    }
    template< typename T >
    void List< T >::insertAtFront( const T& data )
    {
    	ListNode< T > *pNewNode = getNewNode( data );
    
    	if ( isEmpty() )
    			m_pFirst = m_pLast = pNewNode;
    	else
    	{
    		pNewNode->m_pNext = m_pFirst;
    		m_pFirst = pNewNode;
    	}
    }
    template< typename T >
    void List< T >::insertAtBack( const T& data )
    {
    	ListNode< T > *pNewNode = getNewNode( data );
    
    	if( isEmpty() )
    		m_pFirst = m_pLast = pNewNode;
    	else
    	{
    		m_pLast->m_pNext = pNewNode;
    		m_pLast = pNewNode;
    	}
    }
    template< typename T >
    bool List< T >::removeFromFront( T& data )
    {
    	if ( isEmpty() )
    		return false;
    	else
    	{
    		ListNode< T > *tempPtr = m_pFirst;
    
    		if ( m_pFirst == m_pLast )
    			m_pFirst = m_pLast = 0;
    		else
    			m_pFirst = m_pFirst->m_pNext;
    
    		data = tempPtr->m_Data;
    		delete tempPtr;
    		return true;
    	}
    }
    template< typename T >
    bool List< T >::removeFromBack( T& data )
    {
    	if ( isEmpty() )
    		return false;
    	else
    	{
    		ListNode< T > *tempPtr = m_pLast;
    
    		if ( m_pFirst == m_pLast )
    			m_pFirst = m_pLast = 0;
    		else
    		{
    			ListNode< T > *currentPtr = m_pFirst;
    
    			while ( currentPtr->m_pNext != m_pLast )
    				currentPtr = currentPtr->m_pNext;
    
    			m_pLast = currentPtr;
    			currentPtr->m_pNext = 0;
    		}
    
    		data = tempPtr->m_Data;
    		delete tempPtr;
    		return true;
    	}
    }
    template< typename T >
    void List< T >::print() const
    {
    	if( isEmpty() )
    	{
    		cout << "The list is empty\n\n";
    			return;
    	}
    
    	ListNode< T > *currentPtr = m_pFirst;
    
    	cout << "The list is: ";
    
    		while ( currentPtr != 0 )
    		{
    			cout << currentPtr->m_Data << ' ';
    			currentPtr = currentPtr->m_pNext;
    		}
    
    		cout << "\n\n";
    }
    
    #endif  //LIST_H

    ListNode

    Code:
    #include <iostream>
    #include <conio.h>
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    #ifndef LISTNODE_H
    #define LISTNODE_H
    
    template< typename T> class List;
    
    template< typename T >
    class ListNode
    {
    	friend class List< T >;
    
    public:
    	ListNode( const T& data )
    		: m_Data( data ), m_pNext( 0 )
    	{
    	}
    
    	T getData() const
    	{
    		return m_Data;
    	}
    
    private:
    	T m_Data;
    	ListNode< T >* m_pNext;
    };
    
    
    #endif // LISTNODE_H

  2. #2
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Why would you want to? You only need to access the List in order to access the Node's, so its not like it makes anything simpler.

    You could possibly make List's functions static and put Node's functions in List, but it wouldn't be very good looking or clear, and could be a problem actually.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    17
    I want to do this b/c I want to use all the functions in List in another program but I don't want to bring both files over.

  4. #4
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Put them in the same file?
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    17
    Ok, I can actually put them both in the same file but then I get another error.

    I have a class deck which is derived from ideck which now I need to also use all of the list functions.

    I changed all of the "data" to "card" in both of the above files.
    My class header looks like this:

    class Deck : public IDeck, private List< card >

    The error is telling me card in undefined.

    How can I make my Deck class use both IDeck and List (which uses ListNode)?

  6. #6
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Try not using multiple inheritance and just hold an instance of List in the class?
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  7. #7
    Registered User
    Join Date
    Feb 2005
    Posts
    17
    I can't. I have to inherit IDeck.

    Also, Deck is not a template, not sure if that matters. I should still be able to use multiple. Anyone?????

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reference parameters and calculating change
    By Cstudent2121 in forum C Programming
    Replies: 6
    Last Post: 11-04-2005, 03:19 PM
  2. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 08:26 PM
  3. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 04:49 AM
  4. Replies: 2
    Last Post: 11-08-2002, 03:22 AM
  5. Replies: 2
    Last Post: 09-04-2001, 03:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21