7

In Java, there is what is called package. Does library in C++ represent the same meaning, especially in terms for example of containg relative classes and the use of protected members?

Thanks.

Simplicity
  • 47,404
  • 98
  • 256
  • 385

6 Answers6

11

There are different dimensions of what a package means in Java. As a container that differentiates the names of the classes inside from the names of classes in other packages, its equivalent would be c++ namespaces.

As a unit that guarantees access to non-private members to classes in the same block, there is no equivalent in C++. The access level granted to a class is independent of the namespace where the class is defined.

As a way of ordering your sources in the disk, there is no equivalent, the C++ language has no requirements on how the code is stored in files.

Regarding c++ libraries, that is closer to jar files in Java. They bundle different classes that share some relation. A jar can contain more than one package, and more than one jar can contain classes from the same package. Similarly with libraries, they can contain classes from different namespaces and/or different libraries can contain classes from the same namespace.

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
8

The closest to Java packages are namespaces in C++.

They can be nested into one another, and you need to specifically declare that you are using them or a part of their contents. However, they do not enforce any physical file hierarchy like Java packages do.

Péter Török
  • 114,404
  • 31
  • 268
  • 329
3

Strictly speaking I think that namespaces in C++ provide the same semantics.

Mihai Toader
  • 12,041
  • 1
  • 29
  • 33
1

I guess it is more related to namespaces in C++. Java and C++ both use libraries. Library can be any independent set of classes[probably a framework] which can be accessed in our code.

aNish
  • 1,079
  • 6
  • 11
0

External Libraries are there in both Java and C++. Just the formats vary, .jar in Java and .dll/.so in C++.

Purpose of Packages and Namespaces are different from Libraries. They avoid running out of names by allowing user to logically group the source.

Sulla
  • 7,631
  • 9
  • 45
  • 71
0

A package in Java is a namespace for classes, interfaces and enums. Package name, a dot and the classname form the fully qualified classname of a class:

com.example.hello.HelloWorldApplication
^--packagename--^ ^-----classname-----^

The dots in the package name have a different meaning then the dot between the names: the first two dots of this example are part of the package name, the last one is a separator.

This should be kept in mind, because there's a common misunderstanding regarding package names: just because the names can be mapped to a hierarchical folder structure, some people think, package names have a hierarchy too - which is not the case: hello is not a "subpackage" of example!

But, to create a simple mapping to folders and files, a classloader can simply take the fully qualified class name, replace all dots with a slash and append .class to get a relative path to a class file.

But note again, that a folder/file mapping is not required t load classes - we can invent a class loader that gets classes from a database or a remote service - a folder/file mapping wouldn't make any sense in that case.

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268