§2024-12-02
¶namespace
In C++, a namespace is used to organize and manage code to avoid naming conflicts. A namespace allows you to group related classes, functions, variables, and other entities under a common name. It helps prevent name clashes, particularly when combining code from different libraries or modules that might use the same names for variables or functions.
For example, the standard C++ library functions and types are defined inside the std namespace, so to use them, you either need to explicitly specify std:: or use a using directive.
#include <iostream>
namespace myNamespace {
int x = 10;
void display() {
std::cout << "Hello from myNamespace!" << std::endl;
}
}
int main() {
// Accessing the variable and function from the namespace
std::cout << myNamespace::x << std::endl; // Outputs: 10
myNamespace::display(); // Outputs: Hello from myNamespace!
return 0;
}
Libraries in C++ are collections of precompiled code that you can link to your program. A namespace is a way to organize the names of the entities in the code, which can come from any part of the program, including libraries.
In summary, a namespace in C++ is a mechanism for avoiding name collisions and organizing code, not a library.
In C++, the statement using namespace std; is a directive that tells the compiler to use the standard namespace (std) without requiring you to prefix every standard library component with std::
. This helps avoid repeatedly writing std:: before common standard library types, functions, and objects.
For example:
- Without using namespace std;
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
- With using namespace std;
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}
In the second example, the using namespace std; line allows you to write cout and endl directly without the std:: prefix. Advantages:
- Less Typing: You don't need to type std:: every time you use something from the standard library (e.g., cout, cin, vector, etc.).
- Simpler Code: It makes the code cleaner and more concise, especially for smaller programs.
Disadvantages:
Name Conflicts: If you use multiple libraries that have the same names for functions or classes, using using namespace std; could lead to ambiguities or conflicts.
For example, if you include another library that also has a class or function called sort, you may run into issues because the compiler won't know whether you mean std::sort or the other sort.
Larger Projects: In larger projects or when writing code that will be part of a library, it's better to avoid using using namespace std; to prevent naming conflicts and maintain clarity.
¶Best Practices:
- In small, simple programs or examples, it's fine to use using namespace std; because the risk of conflicts is minimal.
- In larger, more complex programs, it's often recommended to avoid using namespace std; and instead explicitly use the std:: prefix to maintain clarity and avoid potential conflicts.
¶ C++ User Input
#include <iostream>
using namespace std;
int main() {
int x;
cout << "Type a number: "; // Type a number and press enter
cin >> x; // Get user input from the keyboard
cout << "Your number is: " << x; // Display the input value
return 0;
}
¶Basic Data Types
The data type specifies the size and type of information the variable will store:
Data Type Size Description
- boolean 1 byte Stores true or false values
- char 1 byte Stores a single character/letter/number, or ASCII values
- int 2 or 4 bytes Stores whole numbers, without decimals
- float 4 bytes Stores fractional numbers, containing one or more decimals. Sufficient for storing 6-7 decimal digits
- double 8 bytes Stores fractional numbers, containing one or more decimals. Sufficient for storing 15 decimal digits
int myNum = 1000;
double myNum = 19.99;
float f1 = 35e3;
double d1 = 12E4;
bool isCodingFun = true;
// Online C++ compiler to run C++ program online
#include <iostream>
#include <string> // g++ might have implicit declaration
using namespace std;
int main() {
bool isCodingFun = true;
cout << isCodingFun << "\n" ; // 1
int myNum = 1000;
cout << myNum << "\n";
double myDoubleNum = 19.99;
cout << myDoubleNum << "\n";
float f1 = 35e3;
cout << f1 << "\n";
double d1 = 12E4;
cout << d1 << "\n";
string greeting = "Hello";
cout << greeting << "\n";
return 0;
}
/* output 1 1000 19.99 35000 120000 Hello /*
¶User Input Strings
string firstName;
cout << "Type your first name: ";
cin >> firstName; // get user input from the keyboard
cout << "Your name is: " << firstName;
// Type your first name: John
// Your name is: John
However, cin considers a space (whitespace, tabs, etc) as a terminating character, which means that it can only store a single word (even if you type many words):
¶C-style strings are created with the char type instead of string.
#include <stdio.h>
int main() {
// Declare a C-style string (character array)
char str[] = "Hello, World!";
// Print the string
printf("%s\n", str);
return 0;
}