*I don't really know what to title this...sorry

Hello everyone,
I am taking a class to learn c++. But throughout the class all we did were make simple program using visual studio 08 with one source file. We are using C++ Programming :Program design Including Data structures by D.S. Malik. We are at chapter 7 right now, which is basically reference parameters and void functions. But he really never explained the basic structure of a project. I was looking around visual studio and I have a few questions.

In all the programs that we have made, all the headers(#include <iostream>...etc) are
in the top of the cpp file in the source folder. Why is there a folder that is named header files then?

What are the resource files?

Can there be more than one cpp file in the source file? If so, why?

All the project types that we make are win32 console application. What is the purpose of this type of application and what are some other application used? What are their benefits and what makes them different?

Are the header files(#include <iostream>...etc) always the same in any situation? How can you change the name? Does it make a difference?

Are there any other using namespace kind of things? Is there a specific name for these lines?

When I was playing around with the different project types(win32 console application...etc), I put in a code from a cpp file into a different project type and it doesn't run. Any special reason?

The return function always confuses me for some reason. My teacher told me that the return 0; basically says to the program that it will return 0 errors. I dont really understand what that mean. Can anyone explain?
Also in function the return function returns the value that you want. Does the return function act differently in different areas of the code? What excatly does the return do?And what is the meaning of the number after it?

Are all compilers pretty much the same? What are some good other kinds? What are the differences?

Are global variables the same thing as constant variables? What is the difference?

Also when calling for an input from a file, using the fstream, why do you have to declare the instream and ofstream? Are they not already part of the library? Can you delcare them to be any name or does it have to be infile and outfile?

When using the fstream do you have to declare your variables globally for the variables to work in a separate function or is it fine declaring them in the main function?

Finally anyone got any tips or interesting about microsoft visual studio 2008?


Sorry for writing an essay for this. I hope this is the right place to post this. I just really want to learn this. I feel that another question is going to pop into my head after I post this...Thanks.

Edited 6 Years Ago by emcyroyale: n/a

Header files usually .h are used to outline classes, and other files which need those classes include the .h file. In other words something like student.cpp will include student.h but there will be a main that also needs to include student.h.

I'm not sure what you mean by resource files, but I assume you mean iostream and stdlib, these files are in the c++ library and are basically .h files that the c++ library has already created. You can find the code in these files easily by just searching on google if you're interested.

Like I already said you can include .cpp files but its best to create files with .h and .cpp... then you will only be including .h or header files.

I have always worked in a linux/ unix environment but those are basically the same thing as a win32 console application. This just means you will run your files on the windows console... instead of creating programs which will run in their own window, they run on the console.

Header files are not always the same... they change based on what you need. You will find out what headers you need based on what c++ library definitions you use. If you use cin// cout you will need <iostream> if you use strings(array of characters) you will have to include <string>. there are a ton of different libraries and you will only use the ones you need

using namespace std just makes it so you don't have to type std before things such as cin and cout. It is just standard(std) c++ library terms. Later on in your programming career you will create your own header files and at your own free will you can create namespaces to better fit your programs.

I would have to see exactly what you did with the .cpp file you added and in what context to tell you why it didnt work. Probably because you were missing its .h file so it was muscles trying to work without bones.

return means what the function will return to the program which calls it. You will create functions that return an int, return a char, return a bool and so on. If one function calls another the function being called can have two purposes. It can return something useful, where you can either use the thing it returns or print it to console. or it can just manipulate the variables you send to that function, so it changes them but does not return anything because you don't need to use the changes right away.

I'm not sure about different compilers because I have always used the same one, maybe someone else can help you there.

constant variables and global variables are not the same thing. Don't use global variables... but you will become more accustomed to using constant variables... but constant variables are extremely complex and get very hard to explain.
check out this site to further read up on constant variables
http://duramecho.com/ComputerInformation/WhyHowCppConst.html

In all the programs that we have made, all the headers(#include <iostream>...etc) are
in the top of the cpp file in the source folder. Why is there a folder that is named header files then?

Header files define prototypes of functions that are implemented either in a library (like the standard C++ library) or your own functions, in a different .cpp file.
By putting '#include <iostream>' at the top of your cpp file, you are making the functions that are defined in the iostream header available to that specific .cpp file.
iostream defines functions/classes like 'cout' and 'cin' that you have probably used. Without #include <iostream>, the cout and cin would be undefined (try to remove the include and compile your source).

The 'header' folder is there for your own header files, if you create one in VisualStudio(VS) it will automatically place it in this 'virtual' folder, to keep the project clean.

Can there be more than one cpp file in the source file? If so, why?

I don't understand this question.. the cpp IS a source file, I suppose you mean if a project can have multiple .cpp(source) files.. and the answer is yes, imagine a huuuge project being written in just 1 cpp file.

For example:
Create a 'printFunctions.h'

#ifndef PRINTFUNCTIONS_H_INCLUDED
#define PRINTFUNCTIONS_H_INCLUDED

void printInt( int myInt );

#endif

And 'printFunctions.cpp'

#include "printFunctions.h"

//Implement printInt:
void printInt( int myInt )
{
    cout << "Printing an integer: " << myInt << "\n";
}

And finally, in your main.cpp.. or any other .cpp file:

#include "printFunctions.h"

int main()
{
    printInt( 5 ); // Output: Printing an integer: 5
}

All the project types that we make are win32 console application. What is the purpose of this type of application and what are some other application used? What are their benefits and what makes them different?

A win32 console application creates a console application for windows(the compiled binary won't run on Linux of course), beside that there are projects to create DLLs(Dynamic Link Library) and projects for applications that provide a graphical user interface.
Whilst it is not absolutely necessary to have these different types of projects, it is just convenient as a GUI project will already set up a lot of stuff for you, so that you can faster start creating/designing your application.

Are the header files(#include <iostream>...etc) always the same in any situation? How can you change the name? Does it make a difference?

I hope I answered this above.. you can't change the name(unless of course you wrote the header file)... and the header files obviously are not the same in any situation, if you don't need cout for example, you do not #include <iostream>

P.S. header files from the std library are included with #include <header_file_name> and your own header files are included with #include "my_header_file_name.h"
For std libraries you shouldn't put '.h' behind it.

Are there any other using namespace kind of things? Is there a specific name for these lines?

A namespace is basically a way to group together declarations, to avoid 'name clashing' with other library (for instance.. what if you have a class that is named 'cout' and you are also including <iostream> (which defines cout aswell) then you would be in trouble, but in this case you are not.. because <iostream> really is defining std::cout).
Im sure the use of namespaces is explained in your book(else it is bad) or you can google it.

When I was playing around with the different project types(win32 console application...etc), I put in a code from a cpp file into a different project type and it doesn't run. Any special reason?

Well, a DLL project for instance needs a different 'entry point' function, it does not know what 'int main()' is.. so there obviously are differences.

The return function always confuses me for some reason. My teacher told me that the return 0; basically says to the program that it will return 0 errors. I dont really understand what that mean. Can anyone explain?
Also in function the return function returns the value that you want. Does the return function act differently in different areas of the code? What excatly does the return do?And what is the meaning of the number after it?

First of all, return is not a function. It is pretty bad that your teacher is not explaing this correctly, or maybe you missunderstood him. Take this for example:

int giveMeANumber()
{
    return 5;
}

int main()
{
  cout << "The number given is: " << giveMeANumber() << "\n";
}

The function giveMeANumber is returning '5' to the caller.. this '5' is something that you as programmer decide. So really the possibilities are endless, you could use this '5' as an indication that the function returned an error, with error_code 5..
And of course you can return anything other than an int.. floats, double, string etc.etc.
E.G.:

#include <string>
std::string giveMeAString()
{
    if( something_went_wrong == true )
    {
        return "Oh shice, something just went horribly wrong!";
    }

    return "Here is your string, everything went A-OK";
}

int main()
{
  cout << "The string given is: " << giveMeAString() << "\n";
}

Are all compilers pretty much the same? What are some good
other kinds? What are the differences?

There are many different compilers, they are not all the same.. each has their own advantages and disadvantages. But that is definitely something that you don't have to worry about yet.. stick with the microsoft C++ compiler and you will be fine for now.

Are global variables the same thing as constant variables? What is the difference?

No, the names have a completely different meaning, so what makes you think they are the same?
A variable defined as 'const' (e.g. const int myConstant = 5) is... well, constant. Meaning that you can not change it's value, so typing 'myConstant = 6' will not compile.
There are ways to by-pass this 'constness'.. but you should not need to worry about that.

A global variable is a variable that is defined in 'global' space, meaning it is accessible by any function:

int myGlobal = 5;
const int myConstGlobal = 10;

int main()
{
   int myLocal = 1;
   myGlobal = 10; // Valid.. myGlobal is global

   cout << myConstGlobal << "\n"; // Valid.. read-only access
   myConstGlobal = 40;  // ERROR
}

void myFunction()
{
   myGlobal = 10; // Valid.. myGlobal is global
   myLocal = 100; // ERROR!, myLocal is undefined in the scope of myFunction

}

phew that was a lot of typing... but a lot of this you could figure out by exerimenting with code: Just define a const variable and see what you can do with it, etc.

This article has been dead for over six months. Start a new discussion instead.