THE BASICS: C++ PROGRAMMING LANGUAGE
UNDERSTANDING A SIMPLE C++ PROGRAM
#include <iostream>
using namespace std;
int main() {
cout << "hello world" << endl;
return 0;
}
**This program above prints out "hello world" when it is compiled and executed.
Iostream - Input Output stream
Function: It declares a set of functions for the standard input output. It also defines I/O of cin,cout.
Cout - A predefined object of Iostream class
Std - A namespace which is used to group data to prevent name conflicts with others with the same name.
Main() - A special function that is called when the program is compiled and run
Return 0; - Returns the 0 value to previous function
Std::cout - used to call the cout object in the namespace standard
VARIABLES
Variables are quantities that may change within the context of a mathematical problem or experiment. With this in mind, variables use single letter to represent it. The letters x, y, z are common letters used to represent variables.
In other words, variable is any characteristics, number or quantity that can be measured or counted. A variable may also be called a data item. Age, business income and expenses, country of birth and class grades are examples of variables.
WHAT ABOUT VARIABLES IN C++
In C++, variables are names given to memory locations. The value stored in a location can be changed during execution. In other words, its a container (storage area) to store data. A variable is just a name given to a memory location. All the operations done on the variable effects the memory location. In C++, all the variables need to be declared before use. Variables need to be given a unique name.
Example: int distance = 20 ;
Here the distance is a variable with the data type int. The value 20 assigned to this variable.
The value of the variable can be changed:
int distance = 20 //distance is 20
int distance = 5 // distance is 5
Rules for naming variables:
A variable can only have alphabets, numbers and underscore
A variable name can't begin with a number
A variable name cannot begin with upper case letter
A variable name cannot be a keyword - example: public, float, int...
There are 3 types of variables:
Local Variables
Instance Variable
Static Variable
LITERALS
Literals are use to represent fixed values. This literals can be used directly in the code. Examples: 2. 3.8, 'c' and so on.
Here are a list of LITERALS:
Integer is a numeric literal. There are three types of integer literals in C++:
decimal : 0, -8, 24
octal: 021, 077, 033...
hexadecimal: 0x7f, 0x2a,
**In C++, Octal starts with 0 and hexadecimal starts with 0x.
Floating literal has exponential form or fractional form.
examples:
-2.0
0.000024
-0.22E05
Character literal is gotten from a single character inside single quotation marks.
'd', 'F', '2', '{'
Escape Sequences
Examples:
\b backspace
\n newline
\r return
\v vertical tab
\' single quotation mark
\0 null character
String Literals
Examples:
"good" string constant
" " null string constant
" " string constant of white spaces
"C" string constant with single character
"What a day\n" prints string with a new line
Constant
We can create values where the values can never change
example:
const int speed = 2800 ;
DATA TYPES
Data types are declaration for variables. This determines the size and type of data for variables.
example:
int distance = 20 ;
The distance is a variable with data type int. This means the variable can only store 2 bytes or 4 bytes only.
C++ BASIC I/O
In C++, cout sends out formatted output. We use the cout object along with the << operator to display output.
INTEGERS
FLOATING POINT LITERALS
CHARACTERS
ESCAPE SEQUENCES
STRING LITERALS
CONSTANTS
DATA TYPES
BASIC I/O
TYPE CONVERSION
OPERATORS
COMMENTS
Comments
Post a Comment