7 Replies - 796 Views - Last Post: 24 October 2012 - 08:44 AM Rate Topic: -----

#1 prototype4  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 64
  • Joined: 22-October 12

How to call other .cpp files in Visual Studios 2010

Posted 22 October 2012 - 07:21 AM

ok, i am fairly new at C++ but i am trying hard to grasp it. My dad told me that instead of trying to put 200 lines of code into one project, that i can make one .cpp file that has code on it to call other .cpp files if certain things happen.

For example, say that i have to .cpp files that have what happens on level one and the other has the code for level two. I dont want to put all this code on one page beacuse that long of code confuses me easily. so what i looked for was something that read like this:

if( go to level one)
{ call .cpp file for level 1}
if( go to level two)
{ call .cpp file to level 2}
else()
{ stay at main menu}

something along those lines.
please either help me or direct me to where i ned to go.

Is This A Good Question/Topic? 0
  • +

Replies To: How to call other .cpp files in Visual Studios 2010

#2 jimblumberg  Icon User is offline

  • member icon

Reputation: 5344
  • View blog
  • Posts: 16,679
  • Joined: 25-December 09

Re: How to call other .cpp files in Visual Studios 2010

Posted 22 October 2012 - 07:41 AM

You don't call a .cpp file. You call functions contained in .cpp files. Here is a tutorial on using multiple files in a project: Multiple source files for one program (C++ example).

Jim
Was This Post Helpful? 0
  • +
  • -

#3 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 456
  • View blog
  • Posts: 1,179
  • Joined: 31-May 11

Re: How to call other .cpp files in Visual Studios 2010

Posted 22 October 2012 - 08:12 AM

#include preprocessor directive is what you're looking for, however I'm not sure you'd use it "right".

What you usually want to have is breaking specific things up into separate files, like declarations (of functions, classes) to .h/.hpp (some people like to name C++ header files .hpp, some like .h) files, and definitions to .c/.cpp files. These extensions are not fixed (and there are more), at this point though what you need to know is that you should refrain from including .c/.cpp files, and rather only include header files.

Example:
//@filename: foo.h
#ifndef SOME_UNIQUE_ENOUGH_HEADER_GUARD_NAME_20121022165149
#define SOME_UNIQUE_ENOUGH_HEADER_GUARD_NAME_20121022165149
namespace my {
    class Foo {
      public:
        long int square(int n); // declaration
    };
}
#endif


//@filename: foo.cpp
#include "foo.h"
namespace my {
    long int Foo::square(int n) { //implementation
        return n * n;
    }
}


//@filename: main.cpp
#include <iostream>
#include "foo.h"

int main() {
    my::Foo f;
    std::cout << f.square(4) << "\n";
    return 0;
}


Compilation: g++ --pedantic -Wall main.cpp foo.cpp.

So, when you start building a little bit bigger programs, you probably will have quite a few classes and you will want/need to place these implementations in separate files, like above. You can include (guarded) header files wherever you need them, but you don't "call .cpp files", rather you use the declared classes, functions (or variables).

This post has been edited by Xupicor: 22 October 2012 - 09:09 AM

Was This Post Helpful? 1
  • +
  • -

#4 prototype4  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 64
  • Joined: 22-October 12

Re: How to call other .cpp files in Visual Studios 2010

Posted 22 October 2012 - 08:43 AM

thank you guys. i appreciate the help.
Was This Post Helpful? 0
  • +
  • -

#5 prototype4  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 64
  • Joined: 22-October 12

Re: How to call other .cpp files in Visual Studios 2010

Posted 23 October 2012 - 07:15 AM

ok so i looked back through and i am confused, which isnt new for me. so here is what i am working with:

/* 
This page is where i want to "call" the other .cpp files to 
*/

#include <iostream>
#include <difficulty_easy.cpp> // i think this is what i need
#include <difficulty_hard.cpp> // but please correct it if it isnt


int main()
{
//call difficulties here...

return 0;
}



*
This is where i write to call the levels
*/

#include <iostream>
#include <Level_One.cpp> // again, if i am not right, please fix
#include <Level_Two.cpp>
#include <Level_Three.cpp>
#include <Level_Four.cpp>
#include <Level_Five.cpp>

int main()
{
//call levels here

return 0:
} 



so, in a nut shell this is what i am working with. also, i have no idea what header files are for, even though i have read and reread about them over and over. i am below a noivce level at this point but i hope to get better.

thanks for the help!
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon

Reputation: 5344
  • View blog
  • Posts: 16,679
  • Joined: 25-December 09

Re: How to call other .cpp files in Visual Studios 2010

Posted 23 October 2012 - 07:49 AM

#include <iostream>
#include <difficulty_easy.cpp> // i think this is what i need
#include <difficulty_hard.cpp> // but please correct it if it isnt


The above is incorrect. You don't include source files in other source files.

I think you really need to understand how functions work before you try to use multiple files. So lets start with a very simple program that contains multiple functions. One function I will call sum will sum two numbers, the second function will be main() which every program must have.

// Include the iostream header to allow using cin/cout.
#include <iostream> 

using namespace std;

// Function prototype for the sum function.
int sum(int first, int second);

int main()
{
   int number1 = 10;
   int number2 = 5;

   cout << "Number 1: " << number1 << " Number 2: " << number2 << " before function call" << endl;

   // Call the function sum.
   int number3 = sum(number1, number2);

   cout << "Sum of number1 and number2 = " << number3 << endl;


   return(0);
}

// Implement the sum function.

int sum(int first, int second)
{
   int returnValue = first + second;

   return returnValue;
}




Do you understand what the above program is actually doing? Once you understand how to use functions properly in a single file you will be ready to use multiple files. But until you have more experience with functions I recommend sticking to a single file.

Jim
Was This Post Helpful? 2
  • +
  • -

#7 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 456
  • View blog
  • Posts: 1,179
  • Joined: 31-May 11

Re: How to call other .cpp files in Visual Studios 2010

Posted 23 October 2012 - 08:59 AM

Just in case you're confused about the terminology, "prototype" is also called a "declaration", and "implementation" is also called a "definition".

So .h (header) file is a place where declarations, or prototypes go, and a corresponding .cpp is a place where definitions, or implementations go.
Was This Post Helpful? 1
  • +
  • -

#8 prototype4  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 64
  • Joined: 22-October 12

Re: How to call other .cpp files in Visual Studios 2010

Posted 24 October 2012 - 08:44 AM

ok thank you, you guys are great. i understand how this works now. :bananaman:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1