The University of Iowa
The College of Liberal Arts and Sciences
Department of Computer Science
Programming Languages and Tools:
CS:3210:0001
Lecture/Lab #2
Programming with C++
Namespaces, functions, std::string
Warm-up
main()
{
return 0;
}
-
Is this a valid C++ program?
-
If so, what does it do?
-
Can it be simplified?
-
What happens if we try to return a string instead of an integer?
Basics recap
-
Program execution starts with a function called main; we don't need to call main explicitly, the language runtime will do it for us when OS runs our program
-
The function body is enclosed in curly braces ({})
-
Individual statements within the function body are separated by semicolon (;)
-
Function declaration takes a general form of
<return-type> <function-name>( [<parameter list>] )
-
main's return type is int (short for integer), a standard built-in type representing integer numbers
int main()
{
return 0;
}
Basics recap (cont.)
#include <iostream>
int main() {
std::cout << "Hi there!\n";
}
-
To bring in symbol definitions from a standard (or a third-party) library, we need to #include a corresponding header
-
The standard header for console input/output is called iostream
-
To write something to the standard output, we use << operator on the standard output object, std::cout
-
main is special-cased to implicitly return 0, so we can omit the return statement
-
String literals are enclosed in double quotes (")
-
Similarly to Python, backslash character (\) inside of a string literal has a special meaning
Terminology
Python, etc.
C++
module / package
library, library header, header
import
include
import os
print( "Current directory:",
os.getcwd() )
#include <iostream>
#include <filesystem>
int main() {
std::cout
<< "Current directory: "
<< std::filesystem::current_path()
<< "\n";
}
What about namespaces?
-
Because there is no explicit construct for declaring a namespace in Python or Java, the term might be unfamiliar to you...
-
... but the concept should not be.
-
A namespace is collection of names that exist independently of any other names in the program
-
In Python and Java this concept is tied to the notion of module/package: an entity declared in a module/package can be assigned any name you want, because when you import the module/package, its entities will be scoped within the module/package's namespace:
import os
print( os.getcwd() )
Namespaces in C++
-
In C++, namespaces exist independently of the physical structure of the program
-
Specifically, there is no one-to-one correspondence between a namespace and a library header (C++ equivalent of a module/package):
#include <iostream>
#include <filesystem>
int main() {
std::cout
<< std::filesystem::current_path()
<< "\n";
}
-
Instead, C++ gives us construct to explicitly define and manage namespaces independently of what file we put our code into
Exercise 1
Write a program that prints the following two lines of text to the standard output:
-
Open the exercise template
-
Write your code, press Run to test
-
When you're done, grab your Repl's link and send it as a direct message to me (agurtovoy)
-
Click on the corresponding option in the "Lab2 exercises" poll in #general
Hello, Bob! Nice to meet you
Instructions
Functions
#include <iostream>
int main()
{
hello_world(); // print "Hello from a function!"
return 0;
}
-
void return type is used to designate that a function doesn't return a value
-
Function call syntax in C++ is the same as in many other mainstream programming languages:
<function-name>( [<comma-separated argument list>] )
-
A function (and, more generally, any other program entity) needs to be declared before we can use it in the program
Exercise 2
Refactor your Exercise 1 solution by moving the console output code inside of the following two functions:
-
Open the exercise template
-
Write your code, press Run to test
-
When you're done, grab your Repl's link and send it as a direct message to me (agurtovoy)
-
Click on the corresponding option in the "Lab2 exercises" poll in #general
Instructions
int main() {
hello_bob();
nice_to_meet_you();
}
Passing function arguments
int main()
{
hello("Bob"); // print "Hello, Bob!"
}
-
A function can be declared to have one or more parameters
-
We have to specify a type for each of the parameters
-
Mismatch in type/number of function arguments is detected at compile time
-
String literals are best passed as std::strings
-
Parameters at the end of function parameter list can be supplied default arguments
Programming with C++, Spring 2020, Lecture #2
By Aleksey Gurtovoy
Programming with C++, Spring 2020, Lecture #2
Namespaces, functions, std::string
- 653