Values in C++
C++ has various value types including strings, integers, floats, booleans, etc. Here are a few basic examples.
#include <iostream>
#include <string>
int main() {
// Strings, which can be added together with +.
std::cout << "cpp" + "lang" << std::endl;
// Integers and floats.
std::cout << "1+1 = " << 1+1 << std::endl;
std::cout << "7.0/3.0 = " << 7.0/3.0 << std::endl;
// Booleans, with boolean operators as you'd expect.
std::cout << std::boolalpha;
std::cout << (true && false) << std::endl;
std::cout << (true || false) << std::endl;
std::cout << (!true) << std::endl;
return 0;
}
To run the program, save it as values.cpp
, compile it, and then execute the resulting binary:
$ g++ -std=c++11 values.cpp -o values
$ ./values
cpplang
1+1 = 2
7.0/3.0 = 2.33333
false
true
false
In this C++ version:
We include the necessary headers:
<iostream>
for input/output operations and<string>
for string manipulation.The
main()
function is the entry point of the program.We use
std::cout
for output instead offmt.Println
.String concatenation is done with the
+
operator, similar to the original.For boolean output, we use
std::boolalpha
to printtrue
andfalse
instead of1
and0
.The program returns 0 at the end to indicate successful execution.
Note that C++ uses <<
for stream insertion and std::endl
for line breaks. The syntax for arithmetic operations and boolean logic remains the same as in the original example.