how and where to include files.

I have been trying to split an app I am writing, it dos the
same kind of thing as the one below. However I can not
get my head around where to include the different parts.
Can some one give a list of what need to be included where
considering I am calling the files like this. And are there any
good tutorals that do not just fly over this kind of thing.

main.cc
t_res.cc
t_interface.cc
t_add.cc
t_sub.cc


#include <iostream>

class t_res{
public:
  int res;
};


class t_interface{
public:
  virtual int doIt(int i,int j) = 0;
  virtual t_res doIt_r(int i, int j) = 0;
};


class t_add:public t_interface{
public:
  virtual int doIt(int i,int j){
    return i + j;
  }
  virtual t_res doIt_r(int i,int j){
    t_res t;
    t.res = i + j;
    return t;
  }
};


class t_sub:public t_interface{
public:
  virtual int doIt(int i,int j);
  virtual t_res doIt_r(int i,int j);
};


int t_sub::doIt(int i, int j){
  return i - j;
}

t_res t_sub::doIt_r(int i, int j){
  t_res t;
  t.res = i - j;
  return t;
}


int main(int args,char *argv[]){
 
  t_interface *add = (t_interface *)new t_add;
  t_interface *sub = (t_interface *)new t_sub;
 
  cout << add->doIt(10, 11) << endl;
  cout << sub->doIt(10, 11) << endl;

  cout << add->doIt_r(10, 11).res << endl;
  cout << sub->doIt_r(10, 11).res << endl;
 
  delete add;
  delete sub;
  return 1;
}
thedogeaterAsked:
Who is Participating?
 
nietodConnect With a Mentor Commented:
Very few books and articles deal with program organization.  More should!!!    "Large-Scale Program Design" by Lakos is the only one  I know of and it deals with much much more than just this.
0
 
thedogeaterAuthor Commented:
Oh I should say I also have
main.h
t_res.h
t_interface.h
t_add.h
t_sub.h
0
 
scientist060700Commented:
since you are dealing with file you may need to write  #include <fstream> command and may  need to
write #include <cctype> also virtual function may require #include <algorithm>  
Try above see which one works and leave out the one without which prog. works
--hope it helps
--scientist
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
thedogeaterAuthor Commented:
Thanks.
I know I need to include <fstream> and <list> and the like
but what I am asking is how I should include the different
file with each other.
0
 
nietodCommented:
>>  However I can not
>> get my head around where to include the different parts.
Are you talking about splitting the app into different parts and then #including them all into one file when you compile?  that is usually not a good idea.

Instead when you break your app into seperate parts each part should be palced in its own source coude file (.cpp or .cp) and each part should be compiled seperately and then linked together.    Thus these parts do not include each other.  They are seperate peices that are compiled seperately and then linked to form your program.

You make need header/include 9.h) files that have decalrations for things defines in these source files that will be shared with other parts of the program.  Note the .h file will contain only the declarations for these things, not their full definitions.   then other parts of the program can include these header files to get the decalrations they need.
0
 
scientist060700Commented:
I would agree with nietod on that one create header files for each class and before writing .cpp in which you would write you main program be sure to  #include "whatever.h" all of them so they all will be included and just those header file I don't think then you would have to #include <fstream> since you included them in your header files.

You also asked for some tutorial consider following site:
http://www.intap.net/~drw/cpp/
--scientist
0
 
thedogeaterAuthor Commented:
Should I still have the class declaration in a .h file
and there implemention is a .cc file?
are there any examples I can folow?
0
 
scientist060700Commented:
It would have been easier and better structured if you put the class declarations in .h files and just one .cpp where you should include all of you class declaration/defination and all of you function declaration/defination.
--scientist
0
 
thedogeaterAuthor Commented:
I am still confused. Taking my example what would
the various includes look like for it?
Or do I compile them separately and then link them, in
this case what needs to be added by way of #include's
and what are the g++ (gnu c++ compiler) commands
to do it?
0
 
nietodCommented:
>> Should I still have the class declaration in a .h file
Yes.

>> and there implemention is a .cc file?
Yes.


// Your new include file
// Interface.h

#ifndef INTERFACE_H
#define INTERFACE_H

class t_interface{
   public:
   virtual int doIt(int i,int j) = 0;
   virtual t_res doIt_r(int i, int j) = 0;
};

class t_add:public t_interface{
            public:
   virtual int doIt(int i,int j);  // Note only declarations, not definitions.  
                                      // You often don't want definitions for member functions
                                      // in the class definition.  This make sthe functions
                                      // inline and with internal linkage.  It can lead to code
                                      // bloat.  Do this only when speed is an issue and almost never
                                      // do it for virtual fucntions.
   virtual t_res doIt_r(int i, int j);
};

class t_sub:public t_interface{
            public:
   virtual int doIt(int i,int j);
   virtual t_res doIt_r(int i, int j);
};


#endif

// Your new source code file
// Interface.cc

#include "interface.h"

int t_add::doIt(int i,int j){
               return i + j;
}

t_res  t_add::doIt_r(int i,int j){
               t_res t;
               t.res = i + j;
               return t;
}

t_res t_sub::doIt_r(int i, int j){
             t_res t;
             t.res = i - j;
             return t;
}


// Your main source code file

#include "interface.h"

int main(int args,char *argv[]){
             
             t_interface *add = (t_interface *)new t_add;
             t_interface *sub = (t_interface *)new t_sub;
             
             cout << add->doIt(10, 11) << endl;
             cout << sub->doIt(10, 11) << endl;

             cout << add->doIt_r(10, 11).res << endl;
             cout << sub->doIt_r(10, 11).res << endl;
             
             delete add;
             delete sub;
             return 1;
}
0
 
garbouaCommented:
yes all advices are good for this particular app.  usually applications and classes are much bigger and have lots of attributes and behaviors.  You can always use the forwared declaration and split your files without code being mangeld.
If your classes are exactly the same as you posted,then nietod method is very good and i would take his advice.  If there are more to your calsses than a constructor, and have more behaviors, then you can use this.
take your app.

file t_res.h
#idndef T_RES_HEADER
#define T_RES_HEADER

class t_res{
public:
 int res;
};

#endif

file interface.h
#ifndef YOU KNOW the drill
// here comes your forward declaration
class t_res;
// this will tell the linker Not to worry about  class t_res until linking time, because it is there

class t_interface{
public:
 virtual int doIt(int i,int j) = 0;
 virtual t_res doIt_r(int i, int j) = 0;
};

#endif

#ifndef T_ADD
#define  T_ADD
//forward declaration

class t_interface;
class t_res;

class t_add:public t_interface{
public:
 virtual int doIt(int i,int j){
   return i + j;
 }
 virtual t_res doIt_r(int i,int j){
   t_res t;
   t.res = i + j;
   return t;
 }
};

#endif

//foward dec
class t_interface;
class t_res;
class t_add;
class t_sub:public t_interface{
public:
 virtual int doIt(int i,int j);
 virtual t_res doIt_r(int i,int j);
};

then each ".h" file has a ".cc" or a ".cpp"
on your main application main.cpp
you have to include all the header files
interface, res, t_add, etc etc etc

now you might say WHY garboua, BECAUSE this is how you develop application according to software engineering.
what do you benifit?
well, code reusability,
you can add t_res in another application without the rest.  if you have one header file then you will have to add all these NON NEEDED object file, or even library to your code and make size un realisticly large.  

ON your next app, you might just need the interface class, to build on it, then you forwared declare t_res and interface, and only link with the two object files.


0
 
thedogeaterAuthor Commented:
Ok thanks but why dos this not work?

________t.cc_________________________
#include "test.h"


int main(){

  test *t = new test(10);
  cout << t->getAInt() << endl;
 
}
________test.cc______________________
test::test(int _a){
  a = _a;
}

int test::getAInt(){
  return a;
}


________test.h_______________________
#ifndef _test_h_
#define _test_h_

#include "test.cc"

class test{
 public:
  test(int a);
  int getAInt();
 private:
  int a;
};

#endif


And I am talking about the context a much bigger app
as nietod surmised.
0
 
nietodCommented:
your test.h include file includes test.cc.  that is wrong.  it should not contain the definitions for the class, only the declarations.  Since it contains the declarations directly it does not need to include test.cc.  Test.cc contains the definitions.  test.cc should not be included by anything ever.   However it needs to see the declarations.  So it needs to include test.h.
0
 
thedogeaterAuthor Commented:
Ok I think I understand I would still like to find a
good tutoral on this and not one that just gives it a
small write up
but anyhow nietod is the best in the end i think.
garboua was good

Thanks to everone.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.