PDA

View Full Version : [SOLVED] Multiple Source Files in C++



nebu
December 29th, 2008, 12:04 PM
If I have multiple source files for a program in c++....

how do i use a class defined in one of the files in another??

do i have to include something in the header files??? function prototyping in the header files lets me use them in multiple source files..... how do i achieve this for classes??

dempl_dempl
December 29th, 2008, 12:31 PM
Declaration in MyClass.h file :




#ifndef __MY_CLASS_H__ ///Header file protection begin
#define __MY_CLASS_H__

class MyClass
{
public:
MyClass();
~MyClass();

void someFunction(); //
};

#endif ///Header protection end



In MyClass.cpp :



#include "MyClass.h" //You need this :)


#include <iostream> //I've put this here just for example
using namespace std;

MyClass::MyClass()
{
//Initialization stuff
}

void MyClass::someFunction()
{
cout << "Hello, world" << endl;
}


MyClass::~MyClass()
{
//Destructor stuff
}

Emill
December 29th, 2008, 02:37 PM
Then you can include MyClass.h in another file and use MyClass in that file.

nebu
December 29th, 2008, 04:43 PM
thnx.... that solved it