Upload
marco-izzotti
View
436
Download
0
Embed Size (px)
Citation preview
Professional slides for your work and your learning
require a guide
require an update
Active tools in your new presentation:- operations buttons;- search commands; - bracket avaible; - active links to web.
Press F5 to continue.
version 1.2
>>
Practical basics on writing C++Basic operations quick guide
Practical basics on writing C++
I/O and math operation symbols
libraries to include
heading and functions
1
#include <iostream> //proprocessor direcrive
using namespace std;
code for outputcout << "Hello word!\n; //this istruction can be write as
//cout << "Hello word!" << endl;
Practical basics on writing C++
I/O and math operation symbols
libraries to include
heading and functions
2
#include <iostream> //proprocessor direcrive
using namespace std;
code for inputint number; //name of variablecin >> number; //reading by system of variable
Practical basics on writing C++
I/O and math operation symbols
sum of two operators+- subtraction of two operators
division of two operators/* product of two operators
calculation of the change in a division between two operators%
Practical basics on writing C++
I/O and math operation symbols
retention of a number in an element=== comparision of two elements
increment of 1++-- decrease of 1
Practical basics on writing C++
I/O and math operation symbols
member b of object aa.b*x pointed to by x
adress of x&xx->y member y of object pointed to by x
object pointed to by xx[n]
Practical basics on writing C++
Mix input data with output messages
code from text editor#include <iostream>
using namespace std;
int main () {cout << “Enter a number.\n”;int number;cin >> number;cout << “You have endered the number “ << number << endl; //”number”;return 0; //this istruction return the command to OS and exits from the program
}
result from terminalEnter a number.4You have entered the number 4.
Practical basics on writing C++
Condition if…else
code from text editor#include <iostream>
using namespace std;
int main () {cout << “Enter a number.\n”;int number;cin >> number;if (number < 10) { //condition
cout << “The number entered is less than 10.\n”; reutrn 0; }else
cout << “The number entered is higher than 10.\n”; return 0;}
result from terminalEnter a number.7The number entered is less than 10.
Enter a number.25The number entered is higher than 10.
This function can be used tomanage errors by user.
Practical basics on writing C++
Condition switch
code from text editor#include <iostream>
using namespace std;
int main () {cout << “Enter 1 to print a message, press 2 to exit\n”;int number_selection;cin >> number_selection;switch (number_selection) {
case 1:cout << “Hi!\n”;return main ();
case 2:return 0;
default:cout << “Error! Please, press 1 or 2 keypass.\n”;return main ();
}}
This function can be used toproject a menu, for example.
1
Practical basics on writing C++
Condition switch
result from terminalEnter 1 to print a message, press 2 to exit.1Hi!Enter 1 to print a message, press 2 to exit.
Enter 1 to print a message, press 2 to exit.2
This function can be used toproject a menu, for example.
Enter 1 to print a message, press 2 to exit.3Error! Please, press 1 or 2 keypass.Enter 1 to print a message, press 2 to exit.
2
Practical basics on writing C++
Cycles: for
code from text editor#include <iostream>
using namespace std;int main () {
for (int a = 10; a < 20; a = a+1){
cout << “Value: " << a << “.\n”;}return 0;
}
result from terminalValue: 10.Value: 11.Value: 12.Value: 13.Value: 14.Value: 15.Value: 16.Value: 17.Value: 18.Value: 19.
Be careful to loops!
Practical basics on writing C++
Cycles: while
code from text editor#include <iostream>
using namespace std;
int main () {int n = 10; //initialisationwhile (n>0) { cout << n << ", ";
--n;}
cout << "liftoff!\n";}
result from terminal10, 9, 8, 7, 6, 5, 4, 3, 2, 1, liftoff!
Be careful to loops!
Practical basics on writing C++
Cycles: do…while
code from text editor//example of an echo machine
#include <iostream>#include <string>
using namespace std;
int main () {string str;do {
cout << "Enter text. ";getline (cin,str);cout << "You entered the following sentence.\n " << str << '\n';
}while (str != "goodbye");
}
result from terminalEnter text.Hello!You entered the following sentences.Hello!
Be careful to loops!do…while executes at least oncethe operation, while while andfor could not execute that.
Practical basics on writing C++
Arrays and vectors: creations and operations
code from text editor//declaration of array
int name_of_array[10]; //the array’s size is 10for(int i=0; i < 14; i++) //initialisarion of array{ cout << name_of_array[10] << endl; //array’s printing}
1
Practical basics on writing C++
Arrays and vectors: creations and operations
another example#include <iostream>
using namespace std;
int foo[] = {16, 2, 77, 40, 12071};int n, result=0;int main () {
for ( n=0; n<5; ++n) {result += foo[n];
}cout << result; return 0;
}
result from terminal12206
2
Practical basics on writing C++
Classes
public class#include <iostream>
using namespace std;
class rectangle {int width, height;public:
rectangle ();rectangle (int,int);int area (void) {
return (width * height);}
};rectangle::rectangle ()
{width = 5; height = 5;}
rectangle::rectangle (int a, int b){width = a;height = b;
}int main () {rrectangle rect(3,4);rectangle rectb;cout << “rect area: " << rect.area() << endl;cout << "rectb area: " << rectb.area() << endl;return 0;
}
By default, in C++ the programmerinsert private class type.
1
Practical basics on writing C++
Classes
output from bashrect area: 12rectb area: 25
2
Practical basics on writing C++
Compiling operation
command from bashg++ <name_of_source_code>.cpp –o <name_of_executable>
Practical basics on writing C++
Run command
command from bash./<name_of_executable>
Index
I/O and math operations symbols
Mix input data with output messages
Condition if…else
Compiling operation
Classes
Cycles: for
Cycles: while
Cycles: do…while
Arrays and vectors: creations and operations
Index
Run command
Get help for free!
[email protected] avaible to anyone.
Marco A. IzzottiOnly for friends.
348 0530717Avaible only for italians during days.