Introduction To C++
Introduction To C++
Dwi Ary
Department of Statistics
Faculty of Science and Data Analytics
Institut Teknologi Sepuluh Nopember
2023
Programming Languages
1950s FORTRAN (FORmula TRANslator)
1960s BASIC language (Beginner’s All-purpose
Symbolic Instruction Code)
1970s UCSD Pascal
1970s C language
1980s C++ language
2
How Computer Executes a Task
Statement: A = A + B
Meaning: add the number B to the number A
3
Inventor
Bjarne Stroustrup
4
Characteristics
C++ contains all the features available in C:
• Universally usable modular programs
• Efficient, close to the machine programming
• Portable programs for various platforms
5
Object-Oriented Programming
6
Error and Warning
Error
Indicating issues in your syntax.
Be sure to start with the first error shown
when troubleshooting the errors.
Warning
To draw your attention to a possible error
in the program’s logic
7
Get Started to C++
Supporting applications:
Dev-C++ Codeblocks
8
Try This Code!
#include <iostream>
using namespace std;
int main()
{
int year;
year = 1821;
cout << year << "\n";
return 0;
}
9
Try This Code!
End of function }
10
Standard Namespace
Immediately after the include statements, we state:
using namespace std;
11
The Main Function
Each C++ application has a main function.
When execution has been concluded, the main
function returns the integer 0.
12
Grammar and Syntax
Name of a Variable
In C++, the name of a variable must start with a letter and contain only
letters, numbers, and the underscore (_).
#include <iostream>
using namespace std;
int main()
{
int year;
year = 1821;
cout << year << "\n";
return 0;
}
13
Grammar and Syntax
Name of a Variable
More detail rules:
- A name contains a series of letters, numbers, or underscore
characters
- The first character must be a letter or underscore
- There are no restrictions on the length of a name and all the
characters in the name are significant
- C++ keywords are reserved and cannot be used as names
14
Keywords in C++
15
Grammar and Syntax
C++ is case sensitive
#include <iostream>
using namespace std;
int main()
Year ≠ year
{ echidna ≠ echiDna
int year; return ≠ Return
year = 1821;
cout << Year << "\n";
return 0;
}
16
Grammar and Syntax
A semicolon (;) to end a statement
If we do not include the semicolon, the compiler will assume that the
statement in the next line is a continuation of the statement in the
present line.
#include <iostream>
using namespace std;
int main()
{
int year;
year = 1821;
cout << year << "\n";
return 0;
}
17
Grammar and Syntax
Multiple command in a line
Two or more statements can be placed in the same line provided they are
separated with semicolons.
#include <iostream>
using namespace std;
int main()
{
int year; year = 1821;
cout << year << "\n";
return 0;
}
18
Grammar and Syntax
In-line comments
In-line comments may be inserted following the double slash “//”.
#include <iostream>
using namespace std;
int main()
{
int year;
year = 1821; // to return year in 1821
cout << year << "\n";
return 0;
}
19
Grammar and Syntax
Commentary
All text enclosed between a slash-asterisk pair (/*) and the converse
asterisk-slash pair (*/) is commentary and ignored by the compiler.
/*-----Running the first code in C++----*/
#include <iostream>
using namespace std;
int main()
{
int year;
year = 1821;
cout << year << "\n";
return 0;
}
20
Question 1
How many commands are executed in the following
line?
a = 3.0; // b = 4.0;
21
Classification
The fundamental types in C++ are:
- Integer types
Arithmetic types
- Floating-point types
- Void type For expressions that do not represent a value
Data types:
• Numerical
• Boolean
• Characters
• Strings, etc.
22
Numerical Variable Declaration
Every numerical variable must be declared either as
an integer or as a real number registered in single or
double precision.
Example:
int a; Integer int a;
float b; a = 875;
Real
double c; int a = 875;
float b = -9.30;
int i; float c = 10.45e-3;
int j;
int i, j; double pi=3.141592653589793238;
Initialized numerical variables
23
Numerical Variable Declaration
24
Boolean Variables
The result of a comparison or a logical association
using AND or OR.
A Boolean variables can be either false or true.
Internal value for true will be represented as the
numerical value 1 and false by a zero.
bool hot;
hot = true;
25
Characters
A single character is encoded according to the ASCII
protocol.
char a;
a = ‘B’;
char a = ‘B’;
char a = 66;
char a = ‘B’;
int c = a;
26
Escape Sequences
27
Strings
A string is an array of characters.
It consists of a sequence of characters enclosed in
double quotes.
string name;
name = “Today is a beautiful day”;
28
Other Data Types
29
Question 2
What are the values of the integers c and d evaluated
by the following statements?
char a = ‘=‘; int c = a;
char b = ‘1’; int d = b;
30
Question 3
Which of the variable definitions shown below is
invalid or does not make sense?
int a(2.5); const long large;
int b = '?’; char c('\'');
char z(500); unsigned char ch = '\201';
int big = 40000; unsigned size(40000);
double he's(1.2E+5); float val = 3/2;
31
Example
#include <iostream>
using namespace std;
int main()
{
double a = 4;
double b = 2;
double c;
c = a+b;
cout << c << "\n";
string message;
message = "peace on earth";
cout << message << "\n";
return 0;
}
32
Example
#include <iostream>
using namespace std;
int gVar1; // Global variables,
int gVar2 = 2; // explicit initialization
int main()
{
char ch = 'A';
cout << "Value of gVar1: " << gVar1 << endl;
cout << "Value of gVar2: " << gVar2 << endl;
cout << "Character in ch: " << ch << endl;
int sum, number = 3; // Local variables
sum = number + 5;
cout << "Value of sum: " << sum << endl;
return 0;
}
33
Constant Objects
The const keyword is used to create a “read only’
object.
As an object of this type is constant it cannot be
modified at a later stage and must be initialized
during its definition.
const double pi = 3.1415947;
34
Volatile Objects
The keyword volatile creates variables that can be
modified not only by the program but also by other
programs and external events.
volatile unsigned long clock_ticks;
35
Example
// Circumference and area of a circle with radius 2.5
#include <iostream>
using namespace std;
const double pi = 3.141593;
int main()
{
double area, circuit, radius = 1.5;
area = pi * radius * radius;
circuit = 2 * pi * radius;
cout << "\nTo Evaluate a Circle\n" << endl;
cout << "Radius: " << radius << endl
<< "Circumference: " << circuit << endl
<< "Area: " << area << endl;
return 0;
}
36
Exercise 1
Write a C++ program that outputs the following text
on screen:
Today is the best day of my life!
Oh yes,
What a happy day!
37
Exercise 2
The following program contains several errors:
*/ Now you should not forget your glasses //
#include <stream>
int main
{
cout << "If this text",
cout >> " appears on your display, ";
cout << " endl;"
cout << 'you should have understood '
<< " the basic of "C++." << endl.
return 0
)
39