0

consider a fun.cpp file :

class fun

{

  public:

  void sum();

  void dispaly();

};


Class fun2

{

    public: 

     void subtract();


};

Now consider another c++ file execute.cpp where i want to access only subtract method of fun.cpp file..

i dont want to include "fun.cpp" file into my execute.cpp as it will increase the size(in larger projects)..

so, how can i access any particular method wihtod including the file????

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
Balwinder SIngh
  • 1,831
  • 2
  • 24
  • 28
  • Do you want to add an interface that describes what parameters the function takes and what the function finally returns or are you referring to something else like serialization? – RetroCoder Sep 10 '12 at 07:33
  • yeah i want to addd interface – Balwinder SIngh Sep 10 '12 at 07:34
  • Similiar question on inheritance or virtual funcitons try this [article][1]. [1]: http://stackoverflow.com/questions/318064/how-do-you-declare-an-interface-in-c – RetroCoder Sep 10 '12 at 08:44

4 Answers4

2

i dont want to include "fun.cpp" file into my execute.cpp

Nor should you, as it would break the one definition rule (assuming the implementations are also in the cpp file).

The usual way to do this is to have the class definition in a header, and include only the header file.

To answer your question, you can, but it's fugly.

C++ allows us to define a class multiple times, as long as the definition is identical. So, in execute.cpp you can simply write:

//execute.cpp
class fun2  //note lower-case 'c'
{

    public: 
     void subtract();
};
// use fun2 here

before you use it. But, again, the usual way is to break the class definition and implementation in a .h file and a .cpp file.

Mandatory advice: read a good introductory C++ book.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
0

Even though you include the file, the linker will only include code that is actually used. This is why using static libraries is sometimes preferable to a dynamic library that has to include everything.

GazTheDestroyer
  • 20,722
  • 9
  • 70
  • 103
0

You can't. You need to actually include the file that links to the code that defines the function if you want to include the function. If you just need the interface and aren't going to use the function, you could simply declare the class in execute.cpp as follows

class fun
{
    public:
        void subtract();
};

But you couldn't use subtract there.

Publius
  • 1,184
  • 2
  • 10
  • 27
0

You need to include the header file which defines the class fun(and has the declaration of subtract()) in the cpp file where you want to use the function subtract().
Note that the function must be defined though.

fun2.h

#ifndef FUN2_H
#define FUN2_H

Class fun2  
{      
    public:        
       void subtract();   
};

#endif // FUN2_H

fun2.cpp

#include "fun2.h"
void func2::subtract()
{

}

execute.cpp

#include "fun2.h"

//use subtract() through object of `fun2`

Note that, to use a member function in a particular source file, the definition of the class which declares that function should be visible to the compiler, the actual job of linking to the particular definition is done at the linking stage and as long as you follow the above format the linker shall link the appropriate function for you.

Alok Save
  • 202,538
  • 53
  • 430
  • 533