#include is "copy-paste this file". For example, you can #include a file into itself, and get this ridiculousness. You can also have a snippet of code in a file and #include it everywhere you want.
Don't do this:
//forloop.txt
for(int i=0;i<SIZE;i++){
ARRAY[i] = VALUE;
}
//prime.txt
2147483647
//main.cpp
#include<iostream>
using std::cout;
using std::string;
int main(){
int prime =
#include "prime.txt"
;
int arr[10];
#define ARRAY arr
#define SIZE 10
#define VALUE prime
#include "forloop.txt"
#undef VALUE
#undef SIZE
#undef ARRAY
for(int i=10;i-- > 0;){
cout<<arr[i]<<'\n';
}
}
The using directive is not strictly needed. C++ has an idea of "namespaces" that prevents, for example, your max function to be considered different from the math function in cmath. It looks something like this:
namespace std{
int max(int i, int j){
if (i<j)
return j;
return i;
}
//some variable declaration and initialization
ostream cout(MAKE_BUFFER(stdout)); //std::ostream, if used outside
}
int max = 5;
int main(){
std::cout<<std::max(3,::max)<<'\n'; //::max refers to global name "max"
}
using namespace std; practically means "If you can't find a name globally, try sticking std:: in front and see if that's a name." People say it's bad practice to say using namespace std partly because if you #include "F.h", and F.h has using namespace std, then your code also uses namespace std (due to copy-paste #include).
Python is not a compiled language. When you say import X, you're giving a command to the interpreter to do something (run some code and make some name). #include is more like telling the compiler what to do to continue compiling at this point.
import in Python kind of means, "attach the names in this module". So import blah as x means, "Every variable (including functions) in blah can be accessed as x.THING". It also calls the module's initialization stuff, which makes sense, because the variables need to be initialized before you can use them.
Java is also a compiled language. In Java, the import statement doesn't play with files, it plays with classes. Every piece of code in Java has to belong to a class.
But unlike the other two languages, import is not strictly necessary. import is actually closer to C++'s using. import simply adds names for classes you can already use, except only to the current class.
Here's some code using imports.
import java.util.Scanner;
public class Example{
public static void main(String blargs[]){
Scanner cin = new Scanner(System.in);
System.out.println("Type in your name and press Enter: ");
System.out.println("Hello "+cin.next());
}
}
Here's the same program, using no imports.
public class Example{
public static void main(String blargs[]){
java.util.Scanner cin = new java.util.Scanner(System.in);
System.out.println("Type in your name and press Enter: ");
System.out.println("Hello "+cin.next());
}
}
Here's the same program, using all long names (import java.lang.*; is implicit in every Java source file).
public class Example{
public static void main(java.lang.String blargs[]){
java.util.Scanner cin = new java.util.Scanner(java.lang.System.in);
java.lang.System.out.println("Type in your name and press Enter: ");
java.lang.System.out.println("Hello "+cin.next());
}
}
Here's the same program, using all the imports.
import java.util.Scanner;
import static java.util.System.out;
import static java.util.System.in;
public class Example{
public static void main(String blargs[]){
Scanner cin = new Scanner(in);
out.println("Type in your name and press Enter: ");
out.println("Hello "+cin.next());
}
}
#includeandimportare quite different, as are namespaces in Python andnamespaces in C++. Knowing both Python and C++, I can tell you that the two are sufficiently different that an analogy with Python isn't a good way of approaching these C++ concepts.using namespace stdis considered bad form and should be avoided.using namespace std;can be considered very poor form in C++, as canfrom X import *in python. But the meaning of each is quite different. I think your analogy causes more confusion than is worth.includestatement is a pre-processor directive, essentially a pre-compilation copy-and-paste. You still need to link in the corresponding compiled libraries, if any, into your executable.