Here is an example that uses the standard string class: #include <iostream> #include <string> <-- 1 using namespace std; void f(const string& firstName, const string& lastName) { string fullName = firstName + " " + lastName; <-- 2 cout << "Your full name is " << fullName << "\n"; <-- 3 } int main() { f("Charlie", "Brown"); f("Fred", "Flintstone"); }
The f() function takes two string objects that will remain unchanged (const string&; see FAQ 2.09). Line 1 concatenates the first name, a space, and then the last name. This uses the overloaded + operator associated with class string. Line 2 prints the resulting full name. This uses the overloaded << operator associated with class string. |