260 likes | 601 Views
Principles of Scientific Programming. Tarik Roukny Yves Dominici. C++ and Software Programming. Chapter 3 : Types. Outline. Chap 1 – Introduction Chap 2 – Compiler and Memory Chap 3 – Types Chap 4 – Control Structures Chap 5 – Functions Chap 6 – Source Files
E N D
Principles of Scientific Programming Tarik Roukny Yves Dominici
C++ and Software Programming Chapter 3 : Types
Outline • Chap 1 – Introduction • Chap 2 – Compiler and Memory • Chap 3 – Types • Chap 4 – Control Structures • Chap 5 – Functions • Chap 6 – Source Files • Chap 7 – Classes and Objects
Types • A set of values and a set of operations • In C++ • Every variable must be associated to a type • Example • int All signed integers in 32 bits From -2.147.483.648 to 2.147.483.648 Operation: +,-,/,*,…
Declaring • Informing the compiler about a new entity and the type associated with it • Variables: • TYPE NAME_OF_VARIABLE • int number_of_friends; • Functions: • TYPE NAME_OF_FUNCTION (TYPE NAME_OF_PARAMETER, …) • int score (int identifier);
Defining • For functions, there is a difference between Declaration and Definition int score (int identifier); … int score (int identifier){ cout << ‘ the score of user ’ << identifier << endl; … } • For variables, the Declaration and the Definition is simultaneous
Other concepts • Allocation In order to keep variables in memory, the compiler allocate memory space/address for each variable depending on the type of the variable • Scope The existence of a variable is confined within its block (default) From its declaration to the ending of the block in which it was instanciated • Block A program is always the result of several blocks (at least 1) Everything between { … } Block A is higher then Block B if B contains A When 2 variables have the same name • The highest block defines the variable in use • Other variables with the same name are hidden
Example with function #include <iostream> using namespace std; intfunction_sum (int a, int b){ int c; c = a + b; return c; } int main () { cout << "Hello"<<endl; int x; int y; int result; cout << "Enter one number: "<<endl; cin >> x; cout << "Enter another number: "<<endl; cin >> y; result = function_sum (x,y); cout << "The sum of the two numbers is : "<< result << endl; return 0; } 03_sum.cpp
Example with condition I #include <iostream> using namespace std; int main () { cout << "Hello"<<endl; int degree; inthot_weather = 24; cout << "Enter the current temperature: "<<endl; cin >> degree; if (degree > hot_weather) { cout << "the weather is hot today! "<<endl; } else { cout << "it is not a hot day today"<< endl; } return 0; } 04_hot.cpp
Example with condition II #include <iostream> using namespace std ; int main () { cout << "Hello" << endl ; int a = 7 ; int b = 5 ; int c = 14 ; cout << "Initial values" << endl ; cout << a << " " << b << " " << c << endl ; if (a > b) { int a, b ; a = 1 ; b = 9 ; cout << a << " " << b << " " << c << endl ; } cout << a << " " << b << " " << c << endl ; return 0 ; } Scope of c Scope of old a,b Scope of new a,b 05_block.cpp
Memory Management • The level between the blocks is translated into Push and Popmanagement strategy Code Stack Heap Static variables Dynamic memory Compiled code pop push Old Variable Ending of an old block Begining of a new block New Variable X y …
Basic Types • Bool • Char • Int • Float • Void • Pointer • Array • Structure • Class
Basic Types • Bool • Logical element • Values • True • False • Operations • && • || • ! • Used for conditional instructions • Char • Int • Float • Void • Pointer • Array • Structure • Class #include <iostream> using namespace std ; int main () { int age = 27 ; char sex = 'M' ; bool ok ; if (age > 18 && sex == 'M') { ok = true ; } if (ok) { cout << "you are allowed to enrol " << endl; } } 06_bool.cpp
Basic Types • Bool • Char • Values • Any character supported by the system • Defined between ‘’ • Ex: char score = ‘a’ • Size • 8 bits • Int • Float • Void • Pointer • Array • Structure • Class
Basic Types • Bool • Char • Int • Integer value • Size • 32 bits • Operations • +,-,/,*,etc. • Float • Void • Pointer • Array • Structure • Class
Basic Types • Bool • Char • Int • Float • Real values • 3 forms • float (4 bytes) • Double(8 bytes) • long double (16 bytes) From 3.3621x10-4932 to 3.3621x10+4932 • Operations • +,-,/,*,etc. • Void • Pointer • Array • Structure • Class
Basic Types #include <iostream> using namespace std ; void announce_decision (int a) { intminimum_age = 18 ; if (a > minimum_age) { cout << "you are accepted!" << endl ; } else { cout << "you cannot be accepted!" << endl ; } } int main () { int age ; cout << "Enter your age: "<<endl; cin >> age ; announce_decision (age) ; return 0; } • Bool • Char • Int • Float • Void • Artifact • Indicates that a function does not return anything • Pointer • Array • Structure • Class 07_void.cpp
Basic Types • Bool • Char • Int • Float • Void • Pointer • Variable containing the adress of another variable • Highly used for dynamical allocation • Array • Structure • Class Beyond the scope of this course
Basic Types • Bool • Char • Int • Float • Void • Pointer • Array • Given a type T, T[n]represents a set of n subsequent variables of type T • Variables are accessible from 0 to n-1 • The i-th variables is accessed by calling T[i-1] • Limitation • Size of table must remain constant • As many dimension as possible • String • Array of char • Structure • Class #include <iostream> using namespace std ; int main () { int scores[] = {12,14,18}; int sum = 0 ; sum += scores[0] + scores[1] + scores[2]; float mean = sum / 3; cout << mean << endl; return 0; } 08_mean.cpp
Basic Types • Bool • Char • Int • Float • Void • Pointer • Array • Structure • Set of arbitrary and heterogeneous types • Allows to create personal types • Definition • Declaration • Allocation • Class
Example of structure struct client { int identifier ; int age ; float money ; } ; void present_client (client c) { cout << endl << "Here are your info: " << endl ; cout << "your identifier : " << c.identifier << endl ; cout << "your age : " << c.age << endl ; cout << "your money : " << c.money << endl << endl ; } int main () { intyour_identifier ; cout << "Hello, what is your identifier?" << endl ; cin >> your_identifier ; intyour_age ; cout << "How old are you?" << endl ; cin >> your_age ; intyour_money ; cout << "How much money do you have?" << endl ; cin >> your_money ; client c = {your_identifier, your_age, your_money} ; present_client (c) ; cout << "Update your money " << endl ; intyour_new_money ; cin >> your_new_money ; c.money = your_new_money ; present_client (c) ; return 0; } 09_struct.cpp
2 important notions • Constant variable • Given a type T, const T is a new type • Has all the same characteristics • Cannot be modified during the run • Needs to be initialized at the beginning • Improves • Security • Clarity • Reference • When variables are passed on • Copied • No change to the original value • Referenced (symbol &) • Change on the original value
Example for the referencing #include <iostream> using namespace std ; void f_copy (int a, int b) { cout << "from f_copy" << endl; a += 2 ; b += 5 ; cout << a << " " << b << endl; } void f_reference (int &a, int &b) { cout << "from f_reference" << endl; a += 1 ; b += 3 ; cout << a << " " << b << endl; } int main () { int a = 8 ; int b = 2 ; cout << "initially" << endl; cout << a << " " << b << endl; f_copy(a,b); cout << "after f_copy" << endl; cout << a << " " << b << endl; f_reference(a,b); cout << "after f_reference" << endl; cout << a << " " << b << endl; return 0; } 10_reference.cpp
Operators • C++ has a lot of possible operations • +,-,*,/,% • <,>,<=,>= • <<,>> • ++,-- • &&, || • Priority is similar as in usual algebra: • a+b*ca+(b*c) • Associativity from the right: • a=b=ca=(b=c) • Sequence for boolean operators: from right to left • && and || • == and !=
Casting • Type-casting • Converting an expression of a given type into another type • Implicit conversion • Nothing to do • Explicit conversion - More robust type1 a ; type2 b; b = (type2) a;
Example implicit #include <iostream> using namespace std; int main() { int a = 100 ; float b ; b = a ; cout << b << endl; b = b / 1.1 ; cout << b << endl; a = b ; cout << a << endl; return 0; } 18_cast.cpp