Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing #include and using namespace std in C++ with import in Python

Tags:

c++

python

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?

like image 454
ruthless Avatar asked Jul 09 '26 21:07

ruthless


1 Answers

#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());
    }
}
like image 92
leewz Avatar answered Jul 13 '26 15:07

leewz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!