Does the following program architecture make sense?

M

mikelinyoho

Does the following program architecture make sense?


#include <stdio.h>
#include "test1.c"
#include "sample.cpp"


int main(void){
output();
output();


testmain();


getchar();
return 0;



}
-------------------------------------

#include"test1.c"
#include"sample.cpp"

include *.c and *.cpp at the same time?


Thank You
Best Regards to you all
 
C

Chris Theis

mikelinyoho said:
Does the following program architecture make sense?


#include <stdio.h>
#include "test1.c"
#include "sample.cpp"


int main(void){
output();
output();


testmain();


getchar();
return 0;



}
-------------------------------------

#include"test1.c"
#include"sample.cpp"

include *.c and *.cpp at the same time?

Yikes. With this construct you force the preprocessor to include the actual
code of test1.c and sample.cpp into your current file. This will be accepted
by the compiler, although it is not what #include is actually meant for. The
idea is to have the interface in a header file and the actual implementation
in a .cpp (or for example .cxx) file. In order to have the interface
available you include it with the include statement, but you do not do this
for the actual implementation.

C++ supports different compilation units, which basically means that you can
split your program into different files where you maintain the actual
implementation. The compiler will create object files for each of these and
the linker will combine them into an executable. If you split up your
program (and this is actually good practice), you will have to use a
procedure to compile all of the files in your project separately (via a
makefile or project-file) and link them afterwards. With the inclusion of
the .c and the .cpp file you would have to compile one file only, but I
would very much recommend to stay away from this.

Best regards
Chris
 
J

Jonathan Mcdougall

mikelinyoho said:
Does the following program architecture make sense?

Depends. It sometimes does, but probably not in this case.
#include <stdio.h>
#include "test1.c"
#include "sample.cpp"


int main(void){

Don't use void in an argument list to specify that there is no
argument. It is considered bad style and is redundant.
output();
output();
testmain();

All these statements won't compile.
getchar();
return 0;
}
-------------------------------------

#include"test1.c"
#include"sample.cpp"

include *.c and *.cpp at the same time?

Usually, you won't do that. Ending a file name with .c or .cpp usually
denotes that this file is meant to be compiled alone. Including it in
another .c or .cpp file is not common practice (except sometimes for
templates).

Although the compiler does not care about the file it includes (as long
as it exists), "header" files (ending in .h or .hpp) or standard C++
headers (ending with nothing) are usually the only kind of file which
gets included.

If this is not only a "just curious" question, saying more about the
design or the problem would allow us to help you a bit more.


Jonathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top