Beruflich Dokumente
Kultur Dokumente
OF
C++
OBJECTIVES
To be able to use C++
C++ standard
Outcome
Know how to write your first
Hello C++!
// This is a C++ program. It prints the sentence:
// Welcome to C++ Programming.
#include <iostream>
using namespace std;
int main()
{
cout << "Welcome to C++ Programming!" <<endl;
system("Pause");
return 0;
}
system("Pause");
return 0;
Comments
Comments can be written in two styles:
Single line:
double can_volume = 0.355; // Liters in a 12-ounce
can
C++ keyword
Range
Integer
int
-32768 to 32767
Long integer
long
-4294967296 to 4294967295
Short integer
short
-128 to 127
Unsigned integer
unsigned
0 to 65535
Character
char
0 to 255
Floating point
float
6 digits of precision
double
12 digits of precision
char ch;
C++ operators
Several classes of operators
Arithmetic
Relational
Logical
Assignment
Arithmetic operators
Seven arithmetic operators in C++
( E.g. int a = 7, b =
2; )
Operators
Action
Expression
Value
Substraction
a-b
Addition
a+b
Multiplication
a*b
14
Division
a/b
Modulus division
a%b
--
Decrement
a--
++
Increment
b++
int main()
{
// Integers Expression
return 0;
int main()
{
// Floating-point expression
return 0;
int main()
{
// Mix expression
cout<<"3 / 2 + 5.5 = "<<3 / 2 + 5.5<<endl;
return 0;
Relational operators
Six relational operators in C++
The results is either TRUE
E.g.
Meaning
if a = 7 and Operator
b = 5, then a < b yields
0 and a != b yields 1
<
Less than
<=
>
Greater than
>=
==
Equal
!=
Not equal
Logical Operators
&& (logical AND)
Expression
true && false
true || false
!false
Result
false
true
true
Assignment Operators
Most commonly used assignment operator is =
(e.g. int Q = 5; )
Assignment expression abbreviations
c = c + 3; can be abbreviated as c += 3; using the addition
assignment operator
Examples of other assignment operators include:
d
e
f
g
-=
*=
/=
%=
4
5
3
9
(d
(e
(f
(g
=
=
=
=
d
e
f
g
*
/
%
4)
5)
3)
9)
int main()
{
cout<<"7.9 (int) = "<<static_cast<int>(7.9)<<endl;
cout<<"25 (double) =
"<<static_cast<double>(25)<<endl;
cout<<"5 + 3 (double) = "
<<static_cast<double>(5 + 3)<<endl;
cout<<"(15) / 2 (double) = "
<<static_cast<double>(15) / 2<<endl<<endl;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
cout<<str;
return 0; }
int main()
{
int feet, inches;
cout<<Enter two integers separated by spaces: ;
cin>> feet >> inches;
cout<<endl;
cout<< Feet = << feet << endl;
cout<< Inches = << inches << endl;
system(pause");
return 0;
}
int main()
{
int num = 0, num2 = 0, tot = 5;
num++;
num2 = num2 + 1;
cout<< num << << ++num <<endl;
tot--;
cout<< tot;
system("Pause");
return 0;
}
Preprocessor Directives
#include <iostream>
#include <string>
using namespace std;
int main()
{
int num;
string name;
cout<<Enter an integer: ;
cin>>num;
cout<<Enter name: ;
cin>>name;
return 0; }
Lets Try!
Write a program that prompts the user to enter two
Lets Try!
Write a program that converts Fahrenheit degrees to