Solved

how and where to include files.

Posted on 2001-07-28
14
175 Views
Last Modified: 2013-11-18
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;
}
0
Comment
Question by:thedogeater
  • 6
  • 4
  • 3
  • +1
14 Comments
 

Author Comment

by:thedogeater
ID: 6329734
Oh I should say I also have
main.h
t_res.h
t_interface.h
t_add.h
t_sub.h
0
 
LVL 1

Expert Comment

by:scientist060700
ID: 6329888
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
 

Author Comment

by:thedogeater
ID: 6329893
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
 
LVL 22

Expert Comment

by:nietod
ID: 6329909
>>  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
 
LVL 1

Expert Comment

by:scientist060700
ID: 6329920
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
 

Author Comment

by:thedogeater
ID: 6329925
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
 
LVL 1

Expert Comment

by:scientist060700
ID: 6329948
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:thedogeater
ID: 6329952
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
 
LVL 22

Expert Comment

by:nietod
ID: 6330028
>> 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
 
LVL 5

Expert Comment

by:garboua
ID: 6330208
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
 

Author Comment

by:thedogeater
ID: 6331644
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
 
LVL 22

Expert Comment

by:nietod
ID: 6332002
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
 

Author Comment

by:thedogeater
ID: 6334761
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
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
ID: 6334892
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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Most of the sites are being standardized with W3C Web Standards. W3C provides lot of web standard services to the web. They have the web specification, process and documentation for all the web standards. You can apply HTML, CSS and Accessibility st…
There are two main kinds of selectors in CSS: One is base selector like h1, h2, body, table or any existing HTML tags.  For instance, the following rule sets all paragraphs (<p> elements) to red: (CODE) CSS also allows us to define our own custom …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now