CMS
Introduction to Basic C++ for Programmers — Part 1
string
and vector
basic_structure.cpp
/** * Most basic program in C++ */ #include <iostream> // allows using std::cout and std::endl int main() { // main function - C++ programs start their execution here std::cout << "Hello World!" << std::endl; // writing to stdout return 0; // indicate successful termination to environment } // blocks are delimited with curly braces
a.out
g++ basic_structure.cpp ./a.out
clang++ basic_structure.cpp ./a.out
-o
g++ basic_structure.cpp -o basic_structure ./basic_structure
g++ -Wall -Wextra -Wconversion
data_types.cpp
// Using a static type system #include <iostream> #include <string> #include <vector> using std::cout; int main() { int a_number(3); // an integral value double value = 13.7404; // double precision floating point value char sep = '\t'; // single character (escaped tab key) std::string name("Amelie"); // C++ string std::vector<double> numbers(3); // a managed array of doubles cout << a_number << sep << value << std::endl; cout << name << std::endl; for (auto& number : numbers) { cout << number << sep; // dereference the iterator } cout << std::endl; return 0; }
block_scope.cpp
#include <iostream> int main() { { int number = 3; } // std::cout << number; // error: number not declared in this scope std::cout << std::endl; return 0; }
if (expression) { block } [else if ...] [else (expression) ...]
if.cpp
#include <iostream> int main() { int number; std::cout << "Enter an integer value: "; std::cin >> number; // read from stdin if (number > 0) { // execute block if expression evaluates to `true` std::cout << "number is greater than 0"; std::cout << std::endl; // a block can have many statements } else if (number < 0) { std::cout << "number is less than 0" << std::endl; } else { std::cout << "number is 0" << std::endl; } return 0; }
switch.cpp
#include <iostream> int main(int argc, char* argv[]) { switch (argc) { // switch evaluates an expression: (argc) case 1: // if the result of the expression resolves to 1, jump here std::cout << "Only the command was entered."; break; // break - jump out of the 'switch' block to avoid falltrough case 2: std::cout << "Command plus one argument"; break; case 3: std::cout << "Command plus two arguments"; break; default: // any other value of the expression jumps here std::cout << "Command plus " << argc-1 << " arguments"; break; } std::cout << std::endl; return 0; }
for (auto& elem : container) { block }
c++11_for.cpp
#include <iostream> #include <string> #include <vector> using std::cout; int main() { std::vector<std::string> names = {"Pat", "Chris", "Sue", "Steve", "Anne"}; for (auto& name : names) { cout << name << " "; } cout << "\n"; }
for (initialization; condition; iteration-statement) { block }
iterator_for.cpp
#include <iostream> #include <string> #include <vector> int main() { std::vector<std::string> names = {"Pat", "Chris", "Sue", "Steve", "Anne"}; // use reverse iterators for (auto it = names.rbegin(); it != names.rend(); it++) { std::cout << *it << std::endl; // dereference the iterator } std::cout << std::endl; return 0; }
for (initialization; condition; iteration-statement) { block }
traditional_for.cpp
#include <iostream> #include <string> #include <vector> int main() { std::vector<std::string> names = {"Pat", "Chris", "Sue", "Steve", "Anne"}; // use reverse iterators for (std::vector<std::string>::size_type i(0); i < names.size(); ++i) { std::cout << names[i] << std::endl; // dereference the iterator } std::cout << std::endl; return 0; }
while (expression) { block }
do { block } while (expression);
while.cpp
#include <iostream> int main() { int n; std::cout << "Enter the starting number: "; std::cin >> n; while (n > 0) { // execute block while expression evaluates to `true` std::cout << n << ", "; --n; // avoid side effects in statement above } std::cout << "FIRE!\n"; return 0; }
return_type function_name(parameter1, parameter2, ...);
return_type function_name(parameter1, parameter2, ...) { block }
function.cpp
#include <cmath> #include <iostream> double area(double radius); // function declaration int main() { std::cout << area(1.0) << std::endl; std::cout << area(2.75) << std::endl; return 0; } double area(double radius) { return radius * radius * M_PI; }
overloading.cpp
#include <iostream> #include <vector> using std::vector; // saves typing but pollutes namespace void print_vector(vector<std::string> v); // `void` means nothing is returned void print_vector(vector<int> v); // overload function int main() { vector<std::string> names = {"Pat", "Chris", "Sue", "Steve", "Anne"}; vector<int> numbers = {3, 9, 1, 12}; print_vector(names); print_vector(numbers); return 0; } void print_vector(vector<std::string> v) { for (auto it = v.begin(); it != v.end(); ++it) { std::cout << *it << std::endl; } } void print_vector(vector<int> v) { for (auto it = v.begin(); it != v.end(); ++it) { std::cout << *it << std::endl; } }
template.cpp
#include <iostream> #include <vector> template <class Type> // compiler will generate a function for required types void print_vector(std::vector<Type>& v); // pass by object reference int main() { std::vector<std::string> names = {"Pat", "Chris", "Sue", "Steve", "Anne"}; std::vector<int> numbers = {3, 9, 1, 12}; std::vector<int> bools = {true, false, true}; print_vector(names); print_vector(numbers); print_vector(bools); return 0; } template <class Type> void print_vector(std::vector<Type>& v) { for (auto it = v.begin(); it != v.end(); ++it) { std::cout << *it << std::endl; } std::cout << std::endl; }