C++ Notes TO USE
C++ Notes TO USE
Basics of C++
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World!";
return 0;
}
//
#
#include
<iostream>
main
main ()
{}
The C++ Language Tutorial
cout
Hello World
//
#
cout
{}
int main ()
{
cout << " Hello World!";
return 0;
}
;
The C++ Language Tutorial
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World! ";
cout << "I'm a C++ program";
return 0;
}
main
int main () { cout << " Hello World! "; cout << " I'm a C++ program "; return 0; }
int main ()
{
cout <<
"Hello World!";
cout
<< "I'm a C++ program";
return 0;
}
// line comment
/* block comment */
//
/*
*/
The C++ Language Tutorial
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World! "; // prints Hello
World!
cout << "I'm a C++ program"; // prints I'm a
C++ program
return 0;
}
// /* */
The C++ Language Tutorial
a = 5;
b = 2;
a = a + 1;
result = a b;
a b result
asm, auto, bool, break, case, catch, char, class, const, const_cast, continue, default, delete,
do, double, dynamic_cast, else, enum, explicit, export, extern, false, float, for, friend, goto,
if, inline, int, long, mutable, namespace, new, operator, private, protected, public, register,
reinterpret_cast, return, short, signed, sizeof, static, static_cast, struct, switch, template,
this, throw, true, try, typedef, typeid, typename, union, unsigned, using, virtual, void,
volatile, wchar_t, while
and, and_eq, bitand, bitor, compl, not, not_eq, or, or_eq, xor, xor_eq
The C++ Language Tutorial
char
short int
short
int
bool
float
double
long double
wchar_t
int
char short
int long char
float double long double
The C++ Language Tutorial
int a;
float mynumber;
int a
float mynumber a
mynumber
int a, b, c;
a b c int
int a;
int b;
int c;
signed unsigned
signed unsigned
int MyAccountBalance;
signed
char
signed char unsigned char signed
unsigned char
short long
short short int long long int
short Year;
short int Year;
unsigned NextYear;
unsigned int NextYear;
The C++ Language Tutorial
#include <iostream>
using namespace std;
int main ()
{
// declaring variables:
int a, b;
int result;
// process:
a = 5;
b = 2;
a = a + 1;
result = a b;
a b result int
The C++ Language Tutorial
main
main main
int a = 0;
()
int a (0);
// initialization of variables 6
#include <iostream>
using namespace std;
int main ()
{
int a=5; // initial value = 5
int b(2); // initial value = 2
int result; // initial value
undetermined
a = a + 3;
result = a b;
cout << result;
return 0;
}
string
The C++ Language Tutorial
<string> std
using namespace
int main ()
{
string mystring = "This is a string";
cout << mystring;
return 0;
}
int main ()
{
string mystring;
mystring = "This is the initial string content";
cout << mystring << endl;
mystring = "This is a different string content";
cout << mystring << endl;
return 0;
}
The C++ Language Tutorial
a = 5;
1776
707
273
"
1776
0
0x
75 // decimal
0113 // octal
0x4b // hexadecimal
int u l
75 // int
75u // unsigned int
75l // long
75ul // unsigned long
e
e
e
The C++ Language Tutorial
3.14159 // 3.14159
6.02e23 // 6.02 x 10^23
1.6e 19 // 1.6 x 10^ 19
3.0 // 3.0
e f l
'z'
'p'
"Hello world"
"How do you do?"
'
"
x
'x'
x x 'x'
'x'
\n \t
\
The C++ Language Tutorial
\n
\r
\t
\v
\b
\f
\a
\' '
\" "
\? ?
\\ \
'\n'
'\t'
"Left \t Right"
"one\ntwo\nthree"
\23 \40
x \x20 \x4A
"string expressed in \
two lines"
wchar_t
char
bool
true false
#define
The C++ Language Tutorial
#define PI 3.14159
#define NEWLINE '\n'
PI NEWLINE
#include <iostream>
using namespace std;
#define PI 3.14159
#define NEWLINE '\n'
int main ()
{
double r=5.0; // radius
double circle;
circle = 2 * PI * r;
cout << circle;
cout << NEWLINE;
return 0;
}
#define
PI NEWLINE
3.14159 '\n'
#define
; ;
const
pathwidth tabulator
The C++ Language Tutorial
a = 5;
a =
a = b;
a b
a
b a
b a
#include <iostream>
using namespace std;
int main ()
{
int a, b; // a:?, b:?
a = 10; // a:10, b:?
b = 4; // a:10, b:4
a = b; // a:4, b:4
b = 7; // a:4, b:7
return 0;
}
a 4 b 7 a
b a = b
The C++ Language Tutorial
a = 2 + (b = 5);
b = 5;
a = 2 + b;
5 b a 2
b a 7
a = b = c = 5;
5 a b c
a = 11 % 3;
a 2 2 11 3
#include <iostream>
using namespace std;
int main ()
{
int a, b=3;
a = b;
a+=2; // equivalent to a=a+2
cout << a;
return 0;
}
++
+=1 =1
c++;
c+=1;
c=c+1;
a++
B=3; B=3;
A=++B; A=B++;
// A contains 4, B contains 4 // A contains 3, B contains 4
B A B A
B
The C++ Language Tutorial
(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.
= ==
==
((b=2) == a) 2 b a
2
&& ||
&&
&& a &&
b
||
a
|| b
The C++ Language Tutorial
#include <iostream>
using namespace std;
int main ()
{
int a,b,c;
a=2;
b=7;
c = (a>b) ? a : b;
cout << c;
return 0;
}
a 2 b 7 a>b
b
7
a = (b=3, b+2);
The C++ Language Tutorial
3 b b+2 a a
5 b 3
&
|
^
~
<<
>>
()
int i;
float f = 3.14;
i = (int) f;
3.14 3
(int)
i = int ( f );
a = sizeof (char);
1 char
sizeof
a = 5 + 7 % 2
The C++ Language Tutorial
a = 5 + (7 % 2) // with a result of 6, or
a = (5 + 7) % 2 // with a result of 0
::
* &
(type)
.* >*
* / %
<< >>
== !=
&
&&
||
?:
( )
a = 5 + 7 % 2;
The C++ Language Tutorial
a = 5 + (7 % 2);
a = (5 + 7) % 2;
The C++ Language Tutorial
iostream
cout
cout <<
<<
Output sentence 120 x cout
"
"
<<
cout << "Hello, " << "I am " << "a C++ statement";
cout << "Hello, I am " << age << " years old and my zipcode is " << zipcode;
cout
The C++ Language Tutorial
cout
cout \n
First sentence.
Second sentence.
Third sentence.
endl
First sentence.
Second sentence.
endl '\n'
cout
\n endl
>> cin
int age;
cin >> age;
cin RETURN
cin RETURN
cin
The C++ Language Tutorial
int main ()
{
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;
}
cin
cin
stringstream
cin >> a;
cin >> b;
a b
cin >>
cin >> mystring;
cin
getline
cin
The C++ Language Tutorial
int main ()
{
string mystr;
cout << "What's your name? ";
getline (cin, mystr);
cout << "Hello " << mystr << ".\n";
cout << "What is your favorite team? ";
getline (cin, mystr);
cout << "I like " << mystr << " too!\n";
return 0;
}
getline mystr
<sstream> stringstream
myint 1204
int main ()
{
string mystr;
float price=0;
int quantity=0;
cin mystr
quantity
The C++ Language Tutorial
The C++ Language Tutorial
Control Structures
{ }
{}
{}
if
if (condition) statement
condition statement
statement
x is 100 x 100
if (x == 100)
cout << "x is 100";
{ }
if (x == 100)
{
cout << "x is ";
cout << x;
}
else
if
if (x == 100)
cout << "x is 100";
else
cout << "x is not 100";
x is 100 x 100 x is
not 100
if + else
x
if (x > 0)
cout << "x is positive";
else if (x < 0)
cout << "x is negative";
else
cout << "x is 0";
{ }
int main ()
{
int n;
cout << "Enter the starting number > ";
cin >> n;
while (n>0) {
cout << n << ", ";
n;
}
while
n>0 n
n>0
The C++ Language Tutorial
n
n>0
n;
n n>0
n 0
condition
statement condition
0
statement condition
for initialization increase
initialization
condition statement
statement { }
increase
initialization increase
for (;n<10;)
for (;n<10;n++)
,
for initialization ,
n i
n 0 i 100 n!=i n i n
i n i
50
The C++ Language Tutorial
break
#include <iostream>
using namespace std;
int main ()
{
int n;
for (n=10; n>0; n )
{
cout << n << ", ";
if (n==3)
{
cout << "countdown aborted!";
break;
}
}
return 0;
}
continue
int main ()
{
for (int n=10; n>0; n ) {
if (n==5) continue;
cout << n << ", ";
}
cout << "FIRE!\n";
return 0;
}
goto
goto
The C++ Language Tutorial
#include <iostream>
using namespace std;
int main ()
{
int n=10;
loop:
cout << n << ", ";
n ;
if (n>0) goto loop;
cout << "FIRE!\n";
return 0;
}
exit cstdlib
exit
exitcode
0
if
else if
switch (expression)
{
case constant1:
group of statements 1;
break;
case constant2:
group of statements 2;
break;
.
.
.
default:
default group of statements
}
expression constant1
group of statements 1 break break
switch
constant1 constant2
group of statements 2 switch
expression
case
default:
The C++ Language Tutorial
switch (x) {
if (x == 1) {
case 1:
cout << "x is 1";
cout << "x is 1";
}
break;
else if (x == 2) {
case 2:
cout << "x is 2";
cout << "x is 2";
}
break;
else {
default:
cout << "value of x unknown";
cout << "value of x unknown";
}
}
switch
break
switch break
break
switch
break switch
{ }
switch (x) {
case 1:
case 2:
case 3:
cout << "x is 1, 2 or 3";
break;
default:
cout << "x is not 1, 2 nor 3";
}
if else if
The C++ Language Tutorial
type
name
parameters
int x
statements { }
int main ()
{
int z;
z = addition (5,3);
cout << "The result is " << z;
return 0;
}
main
main z int
addition
main addition
5 3 int a int b
The C++ Language Tutorial
main main
addition 5 3 int a int
b
return (r);
addition
main
addition return addition
r return (r); 8
z addition (5, 3) 8
addition (5,3) 8
a b r main addition
z addition
main
The C++ Language Tutorial
int main ()
{
int x=5, y=3, z;
z = subtraction (7,2);
cout << "The first result is " << z << '\n';
cout << "The second result is " << subtraction (7,2) << '\n';
cout << "The third result is " << subtraction (x,y) << '\n';
z= 4 + subtraction (x,y);
cout << "The fourth result is " << z << '\n';
return 0;
}
subtraction
main subtraction
z = subtraction (7,2);
cout << "The first result is " << z;
z = 5;
cout << "The first result is " << z;
subtraction
cout
5 subtraction (7,2)
subtraction
subtraction x y 5
3 2
z = 4 + subtraction (x,y);
z = subtraction (x,y) + 4;
z = 4 + 2;
z = 2 + 4;
type
void
void printmessage ()
{
cout << "I'm a function!";
}
int main ()
{
printmessage ();
return 0;
}
void
printmessage
void
The C++ Language Tutorial
printmessage
printmessage ();
printmessage;
The C++ Language Tutorial
addition
x y 5 3
x y
a b 5 3
a b x y
x y
int main ()
{
int x=1, y=3, z=7;
duplicate (x, y, z);
cout << "x=" << x << ", y=" << y << ", z=" << z;
return 0;
}
duplicate
&
The C++ Language Tutorial
a b c x y z
a x b
y c z
x y z duplicate
main
&
x y z
int main ()
{
int x=100, y, z;
prevnext (x, y, z);
cout << "Previous=" << y << ", Next=" << z;
return 0;
}
The C++ Language Tutorial
int main ()
{
cout << divide (12);
cout << endl;
cout << divide (20,4);
return 0;
}
divide
divide (12)
divide divide
2
int b=2 int b
6 12/2
divide (20,4)
b int b=2 b
4 5 20/4
The C++ Language Tutorial
// overloaded function 10
#include <iostream> 2.5
using namespace std;
int main ()
{
int x=5,y=2;
float n=5.0,m=2.0;
cout << operate (x,y);
cout << "\n";
cout << operate (n,m);
cout << "\n";
return 0;
}
operate
int float
int
float
operate int
float
operate
inline
inline
The C++ Language Tutorial
n! = n * (n 1) * (n 2) * (n 3) ... * 1
5! = 5 * 4 * 3 * 2 * 1 = 120
int main ()
{
long number;
cout << "Please type a number: ";
cin >> number;
cout << number << "! = " << factorial (number);
return 0;
}
factorial
0
long
main
main
The C++ Language Tutorial
{ }
;
protofunction int
odd even
main
odd even
even odd
The C++ Language Tutorial
int
int
int billy
int
0 4 0
billy
elements []
{ }
{ }
[ ] billy
{ }
[ ]
{ }
billy
name[index]
billy int
75 billy
billy[2] = 75;
billy a
a = billy[2];
billy[2] int
[ ]
[
]
The C++ Language Tutorial
billy[0] = a;
billy[a] = 75;
b = billy [a+2];
billy[billy[a]] = billy[2] + 5;
// arrays example 12206
#include <iostream>
using namespace std;
int main ()
{
for ( n=0 ; n<5 ; n++ )
{
result += billy[n];
}
cout << result;
return 0;
}
jimmy int
jimmy[1][3]
The C++ Language Tutorial
char
#define
#define HEIGHT 3
#define HEIGHT 4
[]
int arg
procedure (myarray);
// arrays as parameters 5 10 15
#include <iostream> 2 4 6 8 10
using namespace std;
int main ()
{
int firstarray[] = {5, 10, 15};
int secondarray[] = {2, 4, 6, 8, 10};
printarray (firstarray,3);
printarray (secondarray,5);
return 0;
}
for
base_type[][depth][depth]
[]