3

How to include file 2 in file 1. What changes I need to make in file 2.

file 1

 #include <iostream>

 using namespace std;

int main()
{
cout<<"Hello World";

return 0;
}

file 2

 int otheFun()
 {
   cout<<"Demo Program";
   return 0;
 }
2
  • 1
    Question makes no sense. You can't have two programs in one. What would the functionality be? Which main would execute? Commented Jan 17, 2012 at 11:14
  • 1
    You need to explain what effect you're trying to get. What do you want to happen? Commented Jan 17, 2012 at 11:15

2 Answers 2

11

You do not include cpp files in to another cpp files.
Also, a c++ program can have only one main() function.
If you are trying to play around with a program which has multiple files, You will need to have something like this:

file2.cpp

#include <iostream>
#include "file2.h"


int printHelloWorld()
{
    std::cout<<"Hello World";

    return 0;
}

file2.h

 #ifndef FILE2_H    <----Lookup Inclusion Guards on google, this is important concept to learn.
 #define FILE2_H

 int printHelloWorld();

 #endif //FILE2_H

file1.cpp

#include <iostream>
#include "file2.h"


 int main()
 {
     std::cout<<"Demo Program";
     printHelloWorld();
     return 0;
 }
Sign up to request clarification or add additional context in comments.

3 Comments

How about creating header and then including??
@sandbox: Including what? What are you trying to accomplish?
learing to create header file...eg: file2.h
1

What changes I need to make in file 2?

#include <iostream>

using namespace std;

int main()
{
   cout << "Hello world";
   cout << "Demo Program";
}

3 Comments

Should do what the OP wants, although you forgot using sarcasm;.
@peachykeen: I knew I'd forgotten something...!
can I propose another change ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.