24
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 c++

Embed Size (px)

Citation preview

Page 1: Practical basics on c++

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

>>

Page 2: Practical basics on c++

Practical basics on writing C++Basic operations quick guide

Page 3: Practical basics on c++

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;

Page 4: Practical basics on c++

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

Page 5: Practical basics on c++

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%

Page 6: Practical basics on c++

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

Page 7: Practical basics on c++

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]

Page 8: Practical basics on c++

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.

Page 9: Practical basics on c++

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.

Page 10: Practical basics on c++

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

Page 11: Practical basics on c++

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

Page 12: Practical basics on c++

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!

Page 13: Practical basics on c++

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!

Page 14: Practical basics on c++

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.

Page 15: Practical basics on c++

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

Page 16: Practical basics on c++

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

Page 17: Practical basics on c++

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

Page 18: Practical basics on c++

Practical basics on writing C++

Classes

output from bashrect area: 12rectb area: 25

2

Page 19: Practical basics on c++

Practical basics on writing C++

Compiling operation

command from bashg++ <name_of_source_code>.cpp –o <name_of_executable>

Page 20: Practical basics on c++

Practical basics on writing C++

Run command

command from bash./<name_of_executable>

Page 21: Practical basics on c++

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

Page 22: Practical basics on c++

Index

Run command

Page 23: Practical basics on c++
Page 24: Practical basics on c++

Get help for free!

[email protected] avaible to anyone.

Marco A. IzzottiOnly for friends.

348 0530717Avaible only for italians during days.