Basic Elements of A C++ Program. 1.1. Simple C++ Program 1.2. Data Types 1.3. Arithmetic Expression 1.4. Additional Operators
Basic Elements of A C++ Program. 1.1. Simple C++ Program 1.2. Data Types 1.3. Arithmetic Expression 1.4. Additional Operators
1 // Writing my age
2
3 #include <iostream>
4 using namespace std;
5
6 int main()
7 {
8 int age; // input - age
9
10 // Get data on age
11 cout << "Enter your age => ";
12 cin >> age;
13
14 //Display message
15 cout << "I am " << age << " years old" << endl;
16 return 0;
17 }
#include <iostream>
preprocessor directive
begin with symbol #
<iostream> is called library – header file name enclosed in bracket
library is a collection of useful functions, operators and symbols
that may be accessed by a program
<iostream> library defines the names cin and cout the operators >>
and <<
statement
one or more lines of code ends with a semicolon ;
int main ()
main function of the program
consist of one or more statement enclosed in curly bracket { }
cout <<
format: cout << “ ”;
statement enclosed in qoutes (“ ”) display the message on the
screen
an interactive message which the program asking for data from
the user as input data is called a prompt
return 0;
the final line of a program
by returning (0) indicates that the program has ran normally
1.2 Data Types
Sample program:
****************************************************************
// Writing my personal details
#include <iostream>
using namespace std;
int main()
{
string name; // input - name
int age; // input - age
double height; // input - height
char blood; // input - blood type
//Display message
cout << "My name is " << name << endl;
cout << "I am " << age << " years old" << endl;
cout << "I am " << height << "m tall" << endl;
cout << "My name blood type is " << blood << endl;
return 0;
}
Numbers
Floating point numbers
Numbers with fractional part/decimal place
Can be written as decimal points, eg.:
0.573 0.9
or with exponents, eg.:
76e+2 13E-3
where “e” or “E” can be read as “times 10 to the power of”.
76e+2 = 76 × 102 = 7600 13E-3 = 13 × 10-3 = 0.0013
3 types of floating point numbers: float, double and long double.
Use to determine the number range.
Integers
Exact numbers without decimal place eg.:
10 -76 0 +37
Types of integers:
Table 1.2 Integer types in C++
Type Range
short -32,767 .. 32,767
unsigned short 0 .. 65,535
int -2,147,483,647 .. 2,147,483,647
unsigned 0 .. 4,294,967,295
long -2,147,483,647 .. 2,147,483,647
unsigned long 0 ..4,294,967,295
Characters
Store single characters – char
Enclosed the characters in single quotes
Strings
Group of characters
Use double quotes to enclose a string’s value in a program
1.3 Arithmetic Expression
Multiple operators
Step-by-step evaluation rules of an expression containing several
operators:
Table 1.2: Evaluation of Expressions
1. Parentheses Evaluate parenthesized subexpressions first.
2. Precedence Evaluate operators according to this precedence:
i) unary+ unary-
ii) * / %
iii) binary+ binary-
3. Associativity If there is a sequence of two or more operators of the
same precedence, evaluate unary operators right to left
(right associativity) and binary operators left to right (left
associativity).
Operators / and %
Calculate the integer quotient and remainder.
E. g: 38/7 -> 5, 38%7 -> 3
Rounding
To round a positive value of type double to the nearest integer, add
0.5 before explicitly converting to an int to avoid loss of the value’s
fractional part.
1.4 Additional Operators
C++ provides predefined units called functions for operators other than
+−*/
A program that uses functions must have access to the C++ math library,
a collection of predefined mathematical functions.
The list of names and descriptions of some of the most commonly used
functions along with the name of the file to #include in order to gain
access to each function is as in table below:
The abs function return int value while fabs for floating point arguments.
The arguments for log and log 10 must be positive.
The arguments for sin, cos and tan must be expressed in radians, not
degrees.
Example 1:
Ask the user to enter 3 numbers and calculate the average of the numbers.
Sample program:
****************************************************************
#include <iostream>
int main()
{
double num1, num2, num3, average;
average = (num1+num2+num3)/3;
return 0;
}
Example 2:
Sample program:
****************************************************************
#include <iostream>
#include <cmath>
int main()
{
double volume, radius=3.3, pi=3.142;
volume = 4*pi*pow(radius,3)/3;
cout << "Volume of the sphere is " << volume << "
cm^3." << endl;
return 0;
}