Thursday, July 30, 2009

Can you include C++ source files in other ones?

C++ is really confusing me. I'm working on a project with a lot of files: source codes, header files, libraries. It is a jungle with tons of files, classes and structures that must be defined somewhere. I'm new to C++





What type of comunication can exist between C++ files? Can you declare classes or objects in one file and use it in another one? Or is it best to use functions? And can you call a function in one file from another?

Can you include C++ source files in other ones?
You define your function prototypes and/or class definitions in the header files and then implement them in the source files. The compiler is run against each source file and the included headers for each source file are copied into the source file. Then the compiler creates object/compiled code for the source file and spits out an object file. If another source file also includes the header file with functions implemented in another object file, the linker is provided with this information so that the external symbols can be resolved during linking. When you use an IDE such as Visual C++ this tedious stuff normally seen in a make file is all handled for you.





Basically just remember to put the definitions in the headers and implementation in source.





Example:





---------------------Stack.h----------...


#ifndef __STACK__


#define __STACK__





struct StackData


{


char Data;


StackData *Node;


};





class Stack


{


private:


StackData* m_Head;





public:


Stack();


virtual ~Stack();


void Push(char Data);


char Pop( void );


bool IsEmpty( void );


};





#endif





---------------------Stack.cpp--------...





#include "Stack.h"





#ifndef NULL


#define NULL 0


#endif








Stack::Stack(): m_Head(NULL) { /* Constructor */ }





// Destructor


Stack::~Stack()


{


while(m_Head != NULL)


{


StackData *OldNode = m_Head;


m_Head = m_Head-%26gt;Node;





delete OldNode;


}


}





void Stack::Push(char Data)


{


StackData *NewData = new StackData();





NewData-%26gt;Data = Data;


NewData-%26gt;Node = NULL;





if (m_Head == NULL)


{


m_Head = NewData;


}


else


{


NewData-%26gt;Node = m_Head;


m_Head = NewData;


}


}





char Stack::Pop( void )


{


char Result = '\0';





if(m_Head != NULL)


{


Result = m_Head-%26gt;Data;





StackData *OldData = m_Head;


m_Head = m_Head-%26gt;Node;


delete OldData;


}





return Result;





}





bool Stack::IsEmpty( void )


{


return (m_Head == NULL);


}





---------------------main.cpp---------...





#include %26lt;iostream%26gt;


#include %26lt;string%26gt;


#include %26lt;string.h%26gt;


#include "Stack.h"





using namespace std;





void main( void )


{


string sInput;


string sOutput;


Stack oStack;





cout %26lt;%26lt; "Enter a string: " %26lt;%26lt; endl;


cin %26gt;%26gt; sInput;





for( size_t i = 0; i %26lt; sInput.length(); i++)


oStack.Push(sInput[i]);





while(!oStack.IsEmpty())


sOutput += oStack.Pop();





if (sInput.compare(sOutput) == 0)


cout %26lt;%26lt; "It is a palindrome." %26lt;%26lt; endl;


else


cout %26lt;%26lt; "It is NOT a palindrome." %26lt;%26lt; endl;


}
Reply:You can #include "filename.cpp" to obtain a class(es)/functions, but I consider it poor programming. Typically you want to include only the header and link additional source file.
Reply:#include %26lt;file path%26gt;

quince

No comments:

Post a Comment