Professional Documents
Culture Documents
Chapter 2
Chapter 2
Chapter 2
C++ can be found in today's operating systems, Graphical User Interfaces, and
embedded systems.
C++ is an object oriented language which gives a clear structure to programs and
allows code to be reused, lowering development costs.
C++ is portable and can be used to develop applications that can be adapted to
multiple platforms.
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!";
return 0;
}
C++ Syntax
int main() {
cout << "Hello World!";
return 0;
}
Example explained
Line 1: #include <iostream> is a header file library that lets us work with
input and output objects, such as cout (used in line 5). Header files add
functionality to C++ programs.
Line 2: using namespace std means that we can use names for objects and
variables from the standard library.
Line 3: A blank line. C++ ignores white space.
Line 4: Another thing that always appear in a C++ program, is int main().
This is called a function. Any code inside its curly brackets {} will be
executed.
Line 5: cout (pronounced "see-out") is an object used to output/print text. In
our example it will output "Hello World".
Example explained
You might see some C++ programs that runs without the standard
namespace library. The using namespace std line can be omitted
and replaced with the std keyword, followed by the :: operator for
some objects:
Example
#include <iostream>
int main() {
std::cout << "Hello World!";
return 0;
}
C++ Data Types
The data type specifies the size and type of information the
variable will store:
INT
#include <iostream>
using namespace std;
int main () {
float myNum = 5.75;
cout << myNum;
return 0;
}
double
#include <iostream>
using namespace std;
int main () {
double myNum = 19.9999;
cout << myNum;
return 0;
}
float vs. double
#include <iostream>
using namespace std;
int main () {
float f1 = 35e3;
double d1 = 12E4;
cout << f1 << "\n";
cout << d1;
return 0;
}
Booleans
Example
bool isCodingFun = true;
bool isFishTasty = false;
cout << isCodingFun; // Outputs 1 (true)
cout << isFishTasty; // Outputs 0 (false)
Characters
The char data type is used to store a single character. The character must be
surrounded by single quotes, like 'A' or 'c':
#include <iostream>
using namespace std;
int main () {
char myGrade = 'B';
cout << myGrade;
return 0;
}
Example
Alternatively, you can use ASCII values to display certain characters:
#include <iostream>
using namespace std;
int main () {
char a = 65, b = 66, c = 67;
cout << a;
cout << b;
cout << c;
return 0;
}
Strings
Example
string greeting = "Hello";
cout << greeting;
Example
To use strings, you must include an additional header file in the source code, the
<string> library:
#include <iostream>
#include <string>
using namespace std;
int main() {
string greeting = "Hello";
cout << greeting;
return 0;
}
Operators Precedence in C++
x = x+1;
is the same as
x++;
C++ Increment and Decrement Operators
And similarly
x = x-1;
is the same as
x--;
C++ Increment and Decrement Operators
Both the increment and decrement operators can either precede (prefix) or
follow (postfix) the operand. For example:
x = x+1;
can be written as
or as :
main() {
int a = 21;
int c ;
a = b;
x = 10;
Assignment Operator
In the above statements, the value of b is assigned to a, and the value 10 is assigned to x. Here is an example
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a, b=10, x;
a = b;
x = 10;
cout<<"a = "<<a<<endl;
cout<<"b = "<<b<<endl;
cout<<"x = "<<x<<endl;
}
Assignment Statement
As you know, an expression is composed of one or more operations. An expression
terminated ; (semicolon) becomes a statement.
Statements from the smallest executable unit within a C++ program. Statements are
terminated with a semicolon.
where a is a variable to whom the value is being assigned and cve can either be a constant
or variable or an expression. Following are some examples of assignment statements :
x = 16;
y = 3.6;
z = x + y;
z = z × x;
a = b + c - d;
Assignments can be chained together.
x = y = z = 13
C++ Operators
#include <iostream>
using namespace std;
int main() {
int x = 100 + 50;
cout << x;
return 0;
}
C++ Operators
Although the + operator is often used to add together two values, like in the example above,
it can also be used to add together a variable and a value, or a variable and another variable:
#include <iostream>
using namespace std;
int main() {
int sum1 = 100 + 50; // 150 (100 + 50)
int sum2 = sum1 + 250; // 400 (150 + 250)
int sum3 = sum2 + sum2; // 800 (400 + 400)
cout << sum1 << "\n";
cout << sum2 << "\n";
cout << sum3;
return 0;
}
C++ Operators
C++ divides the operators into the following
groups:
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Bitwise operators
Arithmetic Operators
Arithmetic operators are used to perform
common mathematical operations.
C++ Assignment Operators
Assignment operators are used to assign values to variables.
In the example below, we use the assignment operator (=) to assign the value 10 to
a variable call x:
#include <iostream>
using namespace std;
int main() {
int x = 10;
cout << x;
return 0;
}
C++ Assignment Operators
#include <iostream>
using namespace std;
int main() {
int x = 10;
x += 5;
cout << x;
return 0;
}
C++ Assignment Operators
A list of all assignment operators:
C++ Comparison Operators