3

I read through many pages on the internet about the #include statement and using namespace std phrase in C++ and I need some clarification. Since I already know Python I will use it as an analogy. First of all, in C++

#include library

and in Python

import library

are the same. Then, in C++

#include library
using namespace std;

and in Python

from library import *

are the same.

For example, if we compare the first analogy, we know that in C++

#include <iostream>

int main()
{
std::cout << "hello" << std::endl;
return 0;
}

is similar to the code below in Python (similar in using std and #include):

import math

def main():
    print math.sqrt(12)

If we were to compare the second analogy, we have that in C++

include <iostream>
using namespace std;

int main()
{
cout << "hello world" << endl;
}

and in Python

from math import *
def main():
    print sqrt(12)

are similar.

Can you correct me if I am wrong?

10
  • 6
    #include and import are quite different, as are namespaces in Python and namespaces 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. Commented Dec 25, 2013 at 16:47
  • 1
    using namespace std is considered bad form and should be avoided. Commented Dec 25, 2013 at 16:49
  • using namespace std; can be considered very poor form in C++, as can from X import * in python. But the meaning of each is quite different. I think your analogy causes more confusion than is worth. Commented Dec 25, 2013 at 16:52
  • Is it possible if you guys can provide an analogy with perhaps java to help understand this concept, if python is a bad example? Commented Dec 25, 2013 at 16:58
  • I don't think an analogy with Java would be useful either. Basically, C++ does not have a concept of module. An include statement 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. Commented Dec 25, 2013 at 17:20

1 Answer 1

6

#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());
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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.