C++

Object-oriented extension of C with modern features

Overview

C++ extends C with object-oriented programming, templates, and modern features. It's used for game development, high-performance applications, and systems programming.

Language Aliases

Supported Aliases
run cpp "#include <iostream>
int main() { std::cout << "Hello" << std::endl; return 0; }"
run c++ "#include <iostream>
int main() { std::cout << "Hello" << std::endl; return 0; }"
run g++ "#include <iostream>
int main() { std::cout << "Hello" << std::endl; return 0; }"
Output
Hello
Hello
Hello

Basic Usage

Hello World
run cpp "#include <iostream>
int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}"
Output
Hello, World!
Using Variables
run cpp "#include <iostream>
int main() {
    int x = 42;
    std::cout << "Value: " << x << std::endl;
    return 0;
}"
Output
Value: 42

Piping Code to run

C++ works well with both echo and cat for piping code. Use whichever is more convenient:

Terminal
echo '#include <iostream>
int main() { std::cout << "Hello" << std::endl; return 0; }' | run cpp
Terminal
cat <<'EOF' | run cpp
#include <iostream>
int main() {
    std::cout << "Hello " << 42 << std::endl;
    return 0;
}
EOF

Step-by-step in REPL Mode

For step-by-step execution with persistent state, enter REPL mode once, then type commands interactively:

Terminal
$ run cpp
run universal REPL. Type :help for commands.
cpp>>> #include <iostream>
cpp>>> int square(int x) { return x * x; }
cpp>>> std::cout << square(7) << std::endl;
49
cpp>>>

REPL Behavior - Stateful

C++'s REPL is STATEFUL within a single interactive session:

• Start REPL with 'run cpp'

• Includes, functions, and variables persist at the cpp>>> prompt

• Each command builds on previous definitions in that session

• Separate 'run cpp "code"' invocations are independent compilations

Common Issues & Solutions

Here are solutions to common problems when working with C++ in run:

Issue: Missing Headers
int square(int x){ return x*x; }
std::cout << square(7) << std::endl;
Output
error: use of undeclared identifier 'std'
Solution: Add Required Includes
#include <iostream>
int square(int x) { return x * x; }
std::cout << square(7) << std::endl;
Output
49
Full Program with main()
cat <<'EOF' | run cpp
#include <iostream>
int square(int x) { return x * x; }
int main() {
    std::cout << square(7) << std::endl;
    return 0;
}
EOF
Output
49