Create a new project in a Linux OS
Programming in C++ on the Linux operating system using g++ software%% GNU C ++. This compiler can be automatically installed when installing the Linux operating system if we selected the option to install g++.Compilation by using g++ is done in the Linux console (terminal). How that is done is, at the first please type C ++ source file in a text editor and then saved as a file with extension. Cpp,. Cp, cxx, c ++, cp, or cc (example: try1.cpp). Then place it in the folder home / <username> user>. Type these command
g++ <path/name file.cpp> o <file output>
Example : g++ try1 –o output1 (then press enter).
The file path is not required when we first go into the folder where the source file is stored. Output file may not be given, however by default, g++ will create the output file named a.out. In this example if there is no error then the compilation will produce (output) binary file in the name of output1.
Once the compilation is done, check whether the compilation was successful by looking at the results of compilation messages. If there are any error messages then the output file has not been established yet. We need to do editing source (via text editor) and then compile it again. Another way to check if the compilation was successful by entering the folder where the source files are stored, and type the command
ls or dir
If the output file that we define or file a.out has appeared , then compile successfully. Conversely, if the file has not appeared yet, it means the compilation is fail. Execute the output file can be performed by typing the command .
/<name file output>
Example : ./output1
This above command allows us to see the results of the program have been made.
Create a new project in devc + + in Windows OS
Step 1 : In C ++ application, select File> New Project > New Source File
Display of new projects |
Step 2 : Type the code in the work area
Display of source code |
Task 3 : Compilation and Execution of program 1)
Step 1 : Select the Execute menu> Compile
Display of compile program |
Step 2 : Select the Execute menu > Run
Display of the running program |
Exercise
- Make summation program for two value by using input numbers from the user when program run.
- Create a program to calculate the time taken based on the given speed and distance by using the input by the user.
- Create a program to consider a number is odd or even.
- Create a program to determine a number is prime or not.
- Create a program to calculate the area of a circle.
C++ : Basic Data Types (Integer, Real, Character, String, Boolean) - In the Beginning of the discussion of programming, we will first use the basic data types including data type integer, real, char, string and boolean.
Data type is the data type based on the content and it's characteristic. For example, the same analogy with the case of the daily is gallon of water which just made to accommodate certain types of objects with the objects type is liquid, such as water.
The types of basic data types of programming, such as:Integer, integer type commonly called as an integer, but the integer type does not only consist of an integer, there are other types such as short and long, which distinguishes these three types is its range number.
Reach of each data type are different either of the scope or value of the programming
Range of data types
Variable are places to store data of a specified type whose contents can be changed according to the type. Each of variable can only store a single value. So if the value is changed, the previous value is replaced with the new value. Whereas the constant actually is a variable defined by default value at the beginning, and is typically not changed. Variables and constants must be declared in order to the program can allocate a memory to accommodate the specific data and process it in the program to obtain the appropriate output. C++ Language has support variable declarations as well as initialization, for example, is :
Actually the initialization is also called as an assignment that means the process of inserting values into variables with the help of the operator (=). From the above example , the assignment process is entering the data string (character set) into each of tablebrands array [0] unto array tablebrands [4].
Constants are similar to variables, but has a fixed value. Constants can contain values of interger, float, character, and string. Constant declaration can be done in two ways:
#define identifier value
Example of application:
Declaration for # define is without featured by = notation to enter a value into the identifier and also without ended by a semicolon or (;) notation
Example:
For the last example, the declaration of variable zip is without data type, then the compiler will automatically enter it into the type of int.
1. Task 1 : Make the sum of two integers:
C++ : Operator (Arithmetic Operators, Operator of Relation, Operator Increment & Decrement, Operator Bitwise, Logical Operator, The assignment operator, and Operator of condition) - To process a more complicated processing sometimes we need a symbol or a sign to treat some variable which is called as operator.
Operator is a symbol or a sign that if put between on two operands can yield a result, for example in the mathematical plus sign (+) when placed between two other numbers will produce accretion results rate of two numbers. Plus(+) sign is called an operator. The operator has several types as follows:
Negation operator () is called an unary operator, as it can requires only a single operand, while the operator% (modulus) is used to search the rest of the division between the two numbers.
Example : 9 % 2 = 1, 9 % 3 = 0
In the above logical operators are often used in selection or looping conditions, for example:
The assignment operator '=' should not be used in conditions of selection or looping .
Here the example:
if(a=b+4) // wrong syntax,
if(a==b+4) // TRUE
written syntax. On statement 'for' i also found the assignment but not on its condition, but only on the part of forward or backward counter. Example:
for(int i=0; i<=9; i=i+2) // , i=i+2 not lied in the condition beacuse separated by sign (;) // But the part that states the condition is on (i <= 9)
Data type is the data type based on the content and it's characteristic. For example, the same analogy with the case of the daily is gallon of water which just made to accommodate certain types of objects with the objects type is liquid, such as water.
The types of basic data types of programming, such as:Integer, integer type commonly called as an integer, but the integer type does not only consist of an integer, there are other types such as short and long, which distinguishes these three types is its range number.
- Real, this type is used to declare value that requires precision numbers with the values behind the comma(fraction value behind decimal point). Such as: double, single, float.
- Char, the type of data used to store a character.
- String, the type of data in the form of a collection of characters (one or more) that lies between two quotation marks (") in C.
- Boolean, data types are used to express the statement is true (true) or incorrect (false).
Reach of each data type are different either of the scope or value of the programming
Range of data types
Basic Type | Range of Points | Digit of Precission |
---|---|---|
Char | -128 to +127 | - |
Int | -32768 +32767 | - |
Long | -2.147.438.648 to +2.147.438.647 | - |
Float | 3,4E-38 to 3,4E+38 | 6-7 |
Double | 1.7E-308 to 1.7E+308 | 15-16 |
Long Double | 3.4E-4932 to 1.1E+4932 | 19 |
Variable are places to store data of a specified type whose contents can be changed according to the type. Each of variable can only store a single value. So if the value is changed, the previous value is replaced with the new value. Whereas the constant actually is a variable defined by default value at the beginning, and is typically not changed. Variables and constants must be declared in order to the program can allocate a memory to accommodate the specific data and process it in the program to obtain the appropriate output. C++ Language has support variable declarations as well as initialization, for example, is :
string tablebrands[5]={"adidas adizero Indonesia","adidas climacool Vietnam ", "adidas f50 China "}; //array declaration in 5 array,
//Each array capable of storing strings
tablebrands[3]="Nike mercurial Indonesia"; //initialize each array
tablebrands[4]="Nike Air Vietnam";
Actually the initialization is also called as an assignment that means the process of inserting values into variables with the help of the operator (=). From the above example , the assignment process is entering the data string (character set) into each of tablebrands array [0] unto array tablebrands [4].
Constants are similar to variables, but has a fixed value. Constants can contain values of interger, float, character, and string. Constant declaration can be done in two ways:
Ø Using the (#define)
Constant declarations in this way will be easier to do,because includes a preprocessor directive # define. And this syntax is put together with the # include statement (above main ()). The Syntax format is:#define identifier value
Example of application:
#define phi 2.414159265
#define Newline ‘\n’
#define width 100
Declaration for # define is without featured by = notation to enter a value into the identifier and also without ended by a semicolon or (;) notation
Ø Using constants (const)
With the const keyword,the declaration is similar to a variable declaration plus coupled with the"const" word and initializated immediately.Example:
const int width = 100;
const char tab = ‘p’;
const zip = 912;
For the last example, the declaration of variable zip is without data type, then the compiler will automatically enter it into the type of int.
Implementation of Data Type
The most important step in early making a program is to declare a variable that will be used and the type of data type, then do the initialization of these variables, then defines the problemsolving processes that can be kind of calculation formulas or instructions and other commands. Declaring a variable and its data type in C++ language can be seen in the following steps:1. Task 1 : Make the sum of two integers:
- Step 1 : Declaration of the variables Explanation: The variables that are used to process the sum of two integers are a and b . Therefore, two numbers are processed are integer, then the type of data used in the variable a and variable b is an integer.
- Step 2 : Initialization Explanation: Initialize the variables a and b is assigning initial values for the two variables that will be used.
- Step 3 : Process Explanation: Contains the sum process.
- Step 4 : Finalisasi Explanation: is the step to end the program, including display the results to the output devices (screen), and return 0 is used to terminate and return a value, because of our program use INT (see the main function), hence this needs the return value. Then we use return 0 in order to avoid misunderstanding between us with the program.
#include <iostream>
using namespace std;
int main () {
//variable declaration :
int a, b;
int results;
//initialization
a=5;
b=2;
//process
results=a+b;
//show results to the display
cout << results;
getchar ();
//stop program
return 0;
}
2. Task 2 : Compile & Execute program.
Display the results of the program |
C++ : Operator (Arithmetic Operators, Operator of Relation, Operator Increment & Decrement, Operator Bitwise, Logical Operator, The assignment operator, and Operator of condition) - To process a more complicated processing sometimes we need a symbol or a sign to treat some variable which is called as operator.
Operator is a symbol or a sign that if put between on two operands can yield a result, for example in the mathematical plus sign (+) when placed between two other numbers will produce accretion results rate of two numbers. Plus(+) sign is called an operator. The operator has several types as follows:
Arithmetic Operators
Operator | Description | Example |
---|---|---|
+ | Addition(Sum) | a+b |
- | Subtraction | a-b |
* | Multiplication | a*b |
/ | Division | a/b |
% | The rest of division (modulo) | a%b |
- | Negation | -a |
Negation operator () is called an unary operator, as it can requires only a single operand, while the operator% (modulus) is used to search the rest of the division between the two numbers.
Example : 9 % 2 = 1, 9 % 3 = 0
Operator of Relation
Operator | Description | Example | |
---|---|---|---|
== | Equal to | a == b | is a equal to b |
!= | Not equal to | a != b | is a not equal to b |
> | Greater than | a > b | a greater than b |
< | Less than | a < b | a less than b |
>= | Greater than or equal to | a >= b | a greater than or equal to b |
<= | Less than or equal to | a <= b | a less than or equal to b |
Operator Increment & Decrement
Operator | Description | Example | Actual expression |
---|---|---|---|
++ | Increment | a++ | a=a+1 |
-- | Decrement | b-- | b=b-1 |
Operator Bitwise
Operator | Description | Example |
---|---|---|
<< | Shifts n bits to the left (left shift) | a >> b |
>> | hifts n bits to the right (left right) | a << b |
& | Bitwis AND | a & b |
| | Bitwise OR | a | b |
^ | Bitwise XOR | a ^ b |
~ | Bitwise NOT | ~ b |
Logical Operator
Operator | Description | Example |
---|---|---|
&& | Logical AND | a && b |
|| | Logical OR | a || b |
! | LOgical NOT | !b |
In the above logical operators are often used in selection or looping conditions, for example:
If ( (a+b>c) && (a+c>b) &&( b+c>a))
while(x>=’f’ || x<=’c’)
The assignment operator '=' should not be used in conditions of selection or looping .
Here the example:
if(a=b+4) // wrong syntax,
if(a==b+4) // TRUE
written syntax. On statement 'for' i also found the assignment but not on its condition, but only on the part of forward or backward counter. Example:
for(int i=0; i<=9; i=i+2) // , i=i+2 not lied in the condition beacuse separated by sign (;) // But the part that states the condition is on (i <= 9)
Operator of condition
Operator of condition is notated by notation ‘?’ that used to obtain the value of two
possibilities:
expression1 ? expression2 : expression3
When value of expression1 = true, then the value is equal to expression2, otherwise is equal
to expression3
EmoticonEmoticon