Foundations
Hello C++
Start with a tiny C++ program that stores a name and prints a greeting.
cout
`std::cout` writes text so you can see what the program produced.
Hello C++
hello.cpp
Replay: real traced execution (multi-file project)
#include <iostream>
#include <string>
int main() {
std::string name = "C++";
std::string message = "Hello, " + name + "!";
std::cout << message << std::endl;
return 0;
}
#include <iostream>
#include <string>
int main() {
std::string name = "Ada";
std::string message = "Hello, " + name + "!";
std::cout << message << std::endl;
return 0;
}
#include <iostream>
#include <string>
int main() {
std::string name = "Bjarne";
std::string message = "Hello, " + name + "!";
std::cout << message << std::endl;
return 0;
}
name ← C++, message ← Hello, C++!
4int main() {5 std::string name→ C++ = "C++"; //@name="Ada", "Bjarne"6 std::string message→ Hello, C++! = "Hello, " + nameC++ + "!";78 std::cout << messageHello, C++! << std::endl;9 return 0;10}outputHello, C++!
name ← Ada, message ← Hello, Ada!
4int main() {5 std::string name→ Ada = "Ada";6 std::string message→ Hello, Ada! = "Hello, " + nameAda + "!";78 std::cout << messageHello, Ada! << std::endl;9 return 0;10}outputHello, Ada!
name ← Bjarne, message ← Hello, Bjarne!
4int main() {5 std::string name→ Bjarne = "Bjarne";6 std::string message→ Hello, Bjarne! = "Hello, " + nameBjarne + "!";78 std::cout << messageHello, Bjarne! << std::endl;9 return 0;10}outputHello, Bjarne!
Follow the Greeting
namestarts as"C++".- The program joins
"Hello, ",name, and"!". messagebecomesHello, C++!.std::coutprintsHello, C++!. | name | message | | --- | --- | | C++ | Hello, C++! | | Ada | Hello, Ada! | | Bjarne | Hello, Bjarne! |
Exercise: hello.cpp
Reproduce Hello, C++!, then try Ada and Bjarne and predict each greeting before running it.