Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

how and where to include files.

Posted on 2001-07-28
14
Medium Priority
?
189 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 

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 300 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

578 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