Basics of C++

Structure of a program

hello.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
/* my first program in c++ */
/* g++ -Wall -o hello hello.cpp */

#include <iostream>

using namespace std;

int main(void){
  
  cout << "Hello C++ World\n"; // prints Hello World!
  cout << "I am a C++ program\n"; // prints I am a C++ program
    
  return 0;
}

hello.cpp の実行結果は:

[cactus:~/code_c++/cpp_tuts]% ./hello
Hello C++ World
I am a C++ program

Variables. Data Types.

Fundamental data types:

_images/fund.png

Declaration of variables:

var.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// operating with variables

#include <iostream>
using namespace std;

int main(void){
  
  // declaring variables
  int a, b;
  int result;

  // process
  a = 5;
  b = 2;
  result = a - b;

  // print out the result
  cout << result;
  cout << "\n";

  // terminate the program
  return 0;
}

var.cpp の実行結果は:

[cactus:~/code_c++/cpp_tuts]% ./var
3

Scope of variables:

_images/scope_of_variables.gif

Initialization of variables:

initvar.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
// initialization of variables

#include <iostream>
using namespace std;

int main(void){
  int a = 5; // initial value = 5
  int b(2); // initial value = 2
  int result; // initial value undertermined

  a = a + 3;
  result = a - b;

  cout << result;
  cout << endl;

  return 0;
}

initvar.cpp の実行結果は:

[cactus:~/code_c++/cpp_tuts]% g++ -Wall -o initvar initvar.cpp
[cactus:~/code_c++/cpp_tuts]% ./initvar
6

Introduction to strings:

str.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
// my first string

#include <iostream>
#include <string>
using namespace std;

int main(void){

  string mystring = "This is a string.";
    
  cout << mystring << endl;

  return 0;
}

str.cpp の実行結果は:

[cactus:~/code_c++/cpp_tuts]% ./str
This is a string.

str2.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
// my second string

#include <iostream>
#include <string>
using namespace std;

int main(void){

  string mystring;

  mystring = "This is the initial string content";
  cout << mystring << endl;

  mystring = "This is a different string content";
  cout << mystring << endl;

  return 0;
}

str2.cpp の実行結果は:

[cactus:~/code_c++/cpp_tuts]% ./str2
This is the initial string content
This is a different string content

Constants

Literals:

a = 5;

Integer Numerals:

75    // decimal
0113  // octal
0x4b  // hexadecimal
75    // int
75u   // unsigned int
75l   // long
75ul  // unsigned long

Floating Point Numbers:

3.14159  // 3.14159
6.02e23  // 6.02 x 10^23
1.6e-19  // 1.6 x 10^(-19)
3.0      // 3.0
3.14159L // long double
6.02e23f // float

Character and string literals:

'z'
'p'
"Hello world"
"How do you do"
_images/newline.png

For example:

'\n'
'\t'
"Left \t Right"
"ong\ntow\nthree"

Boolean Literals:

true
false

Defined constants (#define):

#define PI 3.14159
#define NEWLINE '\n'

define.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
// defined constants: calculate circumference

#include <iostream>
using namespace std;

#define PI 3.14159
#define NEWLINE '\n'

int main(void){

  double r = 5.0; // radius
  double circle;

  circle = 2 * PI * r;

  cout << circle;
  cout << NEWLINE;

  return 0;
}

define.cpp の実行結果は:

[cactus:~/code_c++/cpp_tuts]% ./define
31.4159

Declared constants (const):

const int pathwidth = 100;
const char tabulator = '\t';

Operators

Assignment (=):

a = 5;
a = b;

op.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
// assignment operator

#include <iostream>
using namespace std;

int main(void){

  int a, b;
  a = 10;
  b = 100;

  cout << "a = ";
  cout << a << endl;

  cout << "b = ";
  cout << b << endl;

  return 0;
}

op.cpp の実行結果は:

[cactus:~/code_c++/cpp_tuts]% ./op
a = 10
b = 100

Arithmetci operators

_images/op-art.png

Compound assignment

_images/comp.png

comp.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
// compound assignment operators

#include <iostream>
using namespace std;

int main(void){

  int a, b = 3;
  a = b;
  a += 2; // equivalent to a = a + 2
  cout << a << endl;

  return 0;
}

comp.cpp の実行結果は:

[cactus:~/code_c++/cpp_tuts]% ./comp
5

Increase and decrease (++, –):

c++;
c += 1;
c = c + 1;
_images/comp.png

Relational and equality operators (==, !=, >, <, >=, <=)

_images/equa.png

Here there are some examples:

(7 == 5) // evaluates to false
(5 > 4)  // evaluates to true
(3 != 2) // evaluates to true
(6 >= 6) // evaluates to true
(5 < 5)  // evaluates to false

Logical operators (!, && ||):

!(5 == 5) // false
!(6 <= 4) // true
!true     // false
!false    // true
_images/op-and.png _images/op-or.png

For example:

(5 == 5) && (3 > 6) // false
(5 == 5) || (3 > 6) // true

Conditional operator:

7 == 5 ? 4 : 3   // returns 3, since 7 is not equal to 5.
7 == 5+2 ? 4 : 3 // returns 4, sing 7 is equal to 5+2.
5 > 3 ? a : b    // returns the value of a, since 5 is greater than 3.
a > b ? a : b    // returns whichever is greater, a or b.

cond.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
// conditional operator

#include <iostream>
using namespace std;

int main(void){

  int a, b, c;
  a = 2;
  b = 7;
  c = (a > b) ? a : b;

  cout << c << endl;

  return 0;
}

cond.cpp の実行結果は:

[cactus:~/code_c++/cpp_tuts]% ./cond
7

Comma operator:

a = (b = 3, b + 2);

Would fisrt assing the value 3 to b, and then assign b + 2 to variable a. So, at the end, variable a would contain the value 5 while variable b would contain value 3.

Bitwise Operator

_images/bitwise.png

Explicit type casting operator:

int i;
float f = 3.14;
i = (int)f;

sizeof():

a = sizeof(char);

Precedence of operators:

a = 5 + 7 % 2

a = 5 + (7 % 2) // result is 6.
a = (5 + 7) % 2 // result is 0.
_images/preop.png

Basic Input/Output

stdin.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
// i/o example

#include <iostream>
using namespace std;

int main(void){

  int i;
  
  cout << "Please enter an integer value: ";
  cin >> i;

  cout << "The value you entered is " << i;

  cout << " and its double is " << i*2 << ".\n";

  return 0;
}

stdin.cpp の実行結果は:

[cactus:~/code_c++/cpp_tuts]% ./stdin
Please enter an integer value: 100
The value you entered is 100 and its double is 200.

cinstr.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
// cin with strings

#include <iostream>
#include <string>

using namespace std;

int main(void){

  string mystr;

  cout << "What's your name? ";
  getline(cin, mystr);
  cout << "Hello " << mystr << ".\n";

  cout << "What's your favorite team? ";
  getline(cin, mystr);
  cout << "I like " << mystr << " too!\n";

  return 0;
}

cinstr.cpp の実行結果は:

[cactus:~/code_c++/cpp_tuts]% ./cinstr
What's your name? wtopia
Hello wtopia.
What's your favorite team? A-Team
I like A-Team too!

stringstream:

The standard header file <sstream> defines a class called stringstream that allows a string-based object to be treated as a stream. This way we can perform extraction or insertion operations from / to strings. which is especially useful to convert strings to numerical values and vice versa. For example, if we want to extract an integer from a string we can write:

string mystr("1024");
int myint;
stringstream(mystr) >> myint;

strstream.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// stringstreams

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main(void){

  string mystr;
  float price = 0;
  int quantity = 0;

  cout << "Enter price: ";
  getline(cin, mystr);
  stringstream(mystr) >> price;

  cout << "Enter quantity: ";
  getline(cin, mystr);
  stringstream(mystr) >> quantity;

  cout << "Total price: " << price*quantity << endl;

  return 0;
}

strstream.cpp の実行結果は:

[cactus:~/code_c++/cpp_tuts]% ./strstream
Enter price: 99.99
Enter quantity: 10
Total price: 999.9

Table Of Contents

Previous topic

Introduction

Next topic

Control Structures