Chapter 2 - Basics of C++
Chapter 2 - Basics of C++
Preprocess
Compile
Link
Load
Execute
1.2 Origin and Basics of C++
1.2.3 C++ basic concepts
- C++ is object-oriented programming language.
- It is case-sensitive language, and the file extension of C++ is
.cpp.
- Examples of C++ IDE text editor: Code blocks, Dev C++,
Turbo C++, Microsoft Visual C++ 2010.
#include <iostream>
using namespace std;
int main() {
cout << “Hello World”;
return 0;
}
1.2 Origin and Basics of C++
- #include <iostream> : Directive pre-processor for
input-output stream library.
- using namespace std; : Elements of the C++ standard
library.
- int main() : Beginning and starting execution of the
main function.
- cout << “Hello World”; : Printing the statement on
the screen by adding the symbol (<<) or insertion stream
operator.
- return 0; : The program terminates/exits.
1.2 Origin and Basics of C++
1.2.4 Comments
- Comments are parts of the source code disregarded by the
compiler, C++ supports two ways to insert comments:
- // line comment: discards everything from where the pair of
slash signs (//) is found up to the end of the same line.
- /* block comment: discards everything between the /*
characters and the first appearance of the */ characters
with the possibility of including more than one line.
1.2 Origin and Basics of C++
1.2.5 Types of Errors in C++ Programming
- Syntax error: An error in the format of a statement in a
computer program that violates the rules of programming
language employed.
- Runtime error: It is after the program has successfully
compiled and is running, such runtime errors are abnormal
program termination, division by zero error, and overflow.
- Logical error: An error in programming that is, caused by
faulty reasoning, resulting in the program’s functioning
incorrectly if the instructions containing the error are
encountered.
1.2 Origin and Basics of C++
1.2.6 Variables and Data Types
- A variable is a symbol that represents a storage location in
the computer’s memory.
- A variable can be defined as a portion of memory to store a
determined value.
Variable = expression;
Variable_names = “Identifiers”;
- A valid identifier is a sequence of one or more letters, digits
or underscore symbols (_).
- Although for some compilers only the 32 first characters of
an identifier are significant.
- A valid identifier should begin with the letter or underscore.
- The keywords cannot be used as identifiers.
1.2 Origin and Basics of C++
1.2.7 Keywords
- Keywords are words whose meaning are defined to the
compiler, such like: asm, bool, break, case, class,
cast, continue, goto, try, catch and so on.
1.2 Origin and Basics of C++
1.2.8 Data Types
- When we program, we store the variables in our computer’s
memory, but the computer memory must know what we
want to store in them since it is not occupying the same
space in memory to store a simple number, letter or a larger
number.
- A byte can store a relatively small amount of data, usually
an integer between 0 and 255 or a single character.
- Standard C++ has 14 different fundamental types: 11
integral types and 3 floating-point types.
- The three floating-point types are float, double and long
double.
- The most frequently used fundamental types are bool,
char, int and double.
1.2 Origin and Basics of C++
1.2.8.1 Declaration of Variables
- To use variable in C++, we must declare the variable within
the data types above we want it to be.
- Example:
int a; // The variable a takes integer value.
int x,y,z; // The variables x, y and z take integer
value.
1.2 Origin and Basics of C++
#include <iostream>
using namespace std;
int main() {
char fname[20], lname[20];
a = b;
a += 2;
cout << a << endl;
return 0;
}
1.2 Origin and Basics of C++
Sample output:
1.2 Origin and Basics of C++
#include <iostream>
using namespace std;
int main() {
bool e, f;
int a = 5;
e = a > 5;
f = a == 5;
cout << e << endl;
cout << f << endl;
return 0;
}
1.2 Origin and Basics of C++
Sample output:
REFERENCES:
C++ Programming: From Problem Analysis to Program Design (D.S. Malik)
Fundamental of Programming in C++ (Walter J. Savitch)
PRESENTED BY:
Mohammed Nebil
SPECIAL THANKS:
Digital Library of Educations
Federal Democratic Republic of Ethiopia, Ministry of Educations
Ethiopian Education Short Note