Uni Graz logo

CMS

Introduction to Basic C++ for Programmers — Part 1

Gerald Senarclens de Grancy <cms@senarclens.eu>

Terminology

Structure of a Program

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
  

Variables and Data Types

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;
}
  

Control Structures

Functions

STL Container Data Types

Summary

Homework and Required Reading

  1. Read the 'Compound data types' section of the C++ Language Tutorial
  2. Do the 10th homework

Questions
and feedback...

Further Reading

cplusplus.com C++ Language Tutorial http://www.cplusplus.com/doc/tutorial/ (2014)
Bjarne Stroustrup Programming – Principles and Practice Using C++ Addison Wesley (December 15, 2008)
Mark Allen Weiss Data Structures and Algorithm Analysis in C++ Pearson (2013)