C++ intro

C++ intro. Variables. Types. Arithmetic operations.

A simple C++ program

#include <iostream>

int main() {
  std::cout << "Hello World!\n";
  return 0;
}
Hello World!

How to compile a program

You need g++ compiler from GNU Compiler Collection. If you have no compiler on you computer, install it. Alternatively, you may try this online development environment.

$ g++ hello.cpp 

$ ./a.out 
Hello World!

By default, the executable file is called a.out, to give it a different name, you can run the compiler with the option -o myname, for example:

$ g++ -o hello hello.cpp 

$ ./hello 
Hello World!

Variables and types

#include <iostream>
using namespace std;

int main() {
  int x = 10;
  int y = 2 * x;
  int z;

  y = y * 5;
  z = y - x - 20;

  double pi = 3.14159265;
  double proxima_centauri = 40100000000000000.0; // meters
  double proxima_centauri2 = 4.01e+16; // 4.01 x 10^16 
  char c = 'A';

  cout << x << endl;
  cout << y << endl;
  cout << z << endl;
  cout << proxima_centauri << endl;
  cout << c << " " << static_cast<int>(c) << endl;
  return 0;
}
10
100
70
4.01e+16
A 65

Arithmetic operations

#include <iostream>
using namespace std;

int main() {

  // Integer operations
  int x1 = 27 + 5; // addition
  int x2 = 27 - 5; // subtraction
  int x3 = 27 * 5; // multiplication
  int x4 = 27 / 5; // quotient (integer division)
  int x5 = 27 % 5; // remainder

  int r = (x1 - x2) * x3 / (x4 + x5);
    
  cout 
    << x1 << "  " << x2 << "  " << x3 << "  "
    << x4 << "  " << x5 << "  " << r << endl;
  
  return 0;
}
32  22  135  5  2  192
#include <iostream>
using namespace std;

int main() {

  // Floating-point operations
  double y1 = 27.2 + 5.2; // addition
  double y2 = 27.2 - 5.2; // subtraction
  double y3 = 27.2 * 5.2; // multiplication
  double y4 = 27.2 / 5.2; // division
  
  double r = (y1 - y2) * y3 / (y4 + 1.0);

  cout 
    << y1 << "  " << y2 << "  " << y3 << "  "
    << y4 << "  " << r << endl;
  
  return 0;
}
32.4  22  141.44  5.23077  236.083

Automatic type conversion

#include <iostream>

using namespace std;
int main(){
  
  // Conversion at assignment:

  int i1 = 1;
  int i2 = 2.5; // fractional decimal places are truncated
  cout << i1 << "  " << i2 << endl;

  double d1 = i1; // auto conversion of int to double
  cout << d1 << endl;
 
  // Conversion before applying binary operators:
   
  double d2 = 3 + 1.5; // int becomes double, then 3.0 + 1.5
  double d3 = 1.5 + 3; // int becomes double, then 1.5 + 3.0

  int i3 = 3 + 1.5; // 3 + 1.5 = 4.5, and then truncated to 4

  cout << d2 << "  " << d3 << "  " << i3 << endl;

  return 0;
}
1  2
1
4.5  4.5  4

Integer and floating-point division

#include <iostream>

using namespace std;
int main() {

  int v = 25, w = 29, x = 30, y = 31, z = -31; 

  int d = 10;
  double dd = 10.0;

  cout 
    << v/d << "  " << w/d << "  " << x/d << "  " 
    << y/d << "  " << z/d << endl;
  
  cout 
    << v/dd << "  " << w/dd << "  " << x/dd << "  " 
    << y/dd << "  " << z/dd << endl;

  return 0;
}
2  2  3  3  -3
2.5  2.9  3  3.1  -3.1

Typecasts

#include <iostream>

using namespace std;

int main() {
 
  int a = 40, b = 13;

  int x = a / b;     // integer division
  double y = a / b;  // integer division, then conversion
  
  cout 
    << x << "  " << y << "  " << endl;

  // we can do explicit type conversion before division:
  double z = a / static_cast<double>(b);
  // or
  double u = static_cast<double>(a) / b; 
  // or
  double v = static_cast<double>(a) / static_cast<double>(b); 

  cout 
    << z << "  " << u << "  " << v << endl; 
}
3  3  
3.07692  3.07692  3.07692

Increment and decrement operators

To increment a variable x, you can write x = x + 1. However, there is a shortcut increment operator for that: you can write ++x or x++ achieving the same effect. Similarly, you can use --x or x-- to decrement x. Consider an example:

#include <iostream>
using namespace std;
int main() {
  int x = 0;
  cout << x << endl;
  
  // to increment x
  x++; 
  cout << x << endl;
  // or
  ++x; 
  cout << x << endl;

  // decrement x
  x--;
  cout << x << endl;
  // or
  --x;
  cout << x << endl;

  return 0;
}
0
1
2
1
0

However, notice the difference between the post-increment (x++) and pre-increment (++x). They would return different values if used as an expression (not as a standalone statement). Consider:

#include <iostream>
using namespace std;

int main() {
  // The difference between x++ and ++x 
  
  // (x++) as an expression returns the initial value of x
  // i.e. it returns x, then increments it
  int x = 10;
  cout << 2 * (x++) << endl; 
  cout << x << endl; 
  
  // (++x) as an expression returns the incremented value 
  // i.e. it increments x, then returns it
  int y = 10;
  cout << 2 * (++y) << endl; 
  cout << y << endl; 

  return 0;
}
20
11
22
11

Standard input and output

#include <iostream>
using namespace std;
int main() {
  int num = 0;
  cout << "Enter a number: ";
  cin >> num;
 
  if (num > 0) { 
    cout << "The number is positive." << endl; 
  }
  else if (num == 0) {
    cout << "The number is a zero." << endl; 
  }
  else {
    cout << "The number is negative." << endl; 
  }

  return 0;
}
Enter a number: 10
The number is positive.