C++
Beginner
1 min read
Compilation, Namespaces, and the C++ Toolchain
Example
// hello.cpp — classic first C++ program
#include <iostream> // standard I/O declarations
#include <string> // std::string
// Declare a custom namespace to avoid collisions
namespace myapp {
std::string greet(const std::string& name) {
return "Hello, " + name + "!";
}
} // namespace myapp
int main(int argc, char* argv[]) {
// Fully-qualified standard library usage
std::cout << myapp::greet("World") << std::endl;
// argc holds the argument count; argv holds the values
if (argc > 1) {
std::cout << "First arg: " << argv[1] << "\n";
}
// Demonstrate a local namespace alias
namespace ma = myapp;
std::cout << ma::greet("C++") << "\n";
return 0; // 0 == success
}
// Compile with: g++ -std=c++17 -Wall -Wextra -o hello hello.cpp
// Run with: ./hello MyName