Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 C++ Programming C++ Basics ● C++ Program ● Variables, objects, types ● Functions ● Namespace ● Tests ● Loops ● Pointers, references.

Similar presentations


Presentation on theme: "1 C++ Programming C++ Basics ● C++ Program ● Variables, objects, types ● Functions ● Namespace ● Tests ● Loops ● Pointers, references."— Presentation transcript:

1 1 C++ Programming C++ Basics ● C++ Program ● Variables, objects, types ● Functions ● Namespace ● Tests ● Loops ● Pointers, references

2 2 C++ Program ● The simplest C++ program ● A C++ program consists of – Statements ( "Instructions") ● Statements, expressions ● Free-form, they only need to end with ; ● Can be grouped into blocks with {} – Functions ● Group instructions (statements and expressions) in a named unit ● Each C++ program C++ must contain a main function – This function is executed by the program – It must return an integer int main() { return 0; }... but it does nothing

3 3 Variables ● C++ is an object oriented programming language: in C++ we create various objects and make them perform various operations ● The object created in a program is called variable – When programmer writes: The program creates: – The variable has its name (i) ● The name is fully chosen by the programmer – It allocates memory - the current memory content is its value ● Variable i has the value 1 – The size of memory allocated and the operations we can do with a variable are defined by its type ● The variable i is of type int i: int 1 i: int 1 int i = 1;

4 4 Variable Declaration Variable declaration = the instruction that creates the variable ● We can declare a variable without giving it an initial value; its value is undefined ● We can also declare a variable with giving it its initial value – Both ways of initialization are equivalent; usually we prefer the first one – We prefer to declare variables just before their use ● The const declaration of a variable - creates a variable that can not be changed later int i; int i(1); int i = 1; Declaration Declaration + initialization oror const float pi = 3.141592; pi = 5.18;

5 5 Variable type ● The object (variable) type – Defines how the object (its binary configuration in memory) can be manipulated – The type has to be already defined in the program when we declare a variable ● Fundamental types – Numbers: integer (int), floating point numbers (float, double) – Characters: char – boolean: can have only two values: true, false – Can be manipulated via operators: ● Unary (operates on one variable) or binary (operates on two variables) ● Eg. x++, x+y, x = y,... ● Object types – Defined in the C++ standard library and by the programmer – Have their own defined functions; can also have their own defined operators

6 6 Manipulating Variables (1) ● Example of a C++ program using variables of fundamental types and manipulating them via operators: int main() { int i = 3; // declaration of variable with initialisation int j = 5; // declaration of variable with initialisation int k; // declaration of variable without initialisation k = i; // we give a value to the variable k // using operator = (« assignment ») i++; // we increment the value of the variable i k = i + j; // using a binary operator on the variables // i, j followed by using operator = on k... return 0; }... i has value 3... j has value 5... k has no value !!!... i has value 4... j has value 5... k has value 9

7 7 Manipulating Variables (2) ● Example of C++ program using a variable of object type defined in the C++ standard library (in this case, std::string) #include int main() { std::string name = “Agatha”; // declaration of variable with // initialisation name.append(“Christie”); // calling a function “append” // defined for std::string type std::string::size_type idx = name.find(“Christie”); name.insert(idx, “ “); // calling functions “find”, “insert” // defined for std::string type... return 0; }... name has value “Agatha”... name has value “AgathaChristie”... name has value “Agatha Christie”... name has value “Agatha”

8 8 Functions... ● Declaration = specification of the identifier and the type of function – The contract that the function must fulfil (Job to be done by the function) ● Definition = implementation – The specifications of the contract (How to do the job) float Sum(float a, float b); float Sum(float a, float b) { /// Add a and b data values return a+b; } ● The declaration and definition of the same function can be found in different files ● In C++, we prefer functions associated with classes and will take a closer look at this later

9 9... Functions ● Example of a C++ program using a function: // Definition of the function to sum the data a and b float Sum(float a, float b) { return a+b; } // The function main int main() { float x = 5.0; float y = 0.5; // Apply the function to the variables x and y float sum = Sum(x, y); return 0; }

10 10 Namespace ● namespace allows us to use simple names in more complex programs (often composed of several libraries) ● The definition of variables, functions, types (classes) may be under a given "namespace" – Two different functions may have the same name if they are in different namespaces namespace A { float Sum(float a, float b) { return a+b; } }; int main() { float x = 5.0; float y = 0.5; // Call the function in the namespace A float sum1 = A::Sum(x, y); // Call the function in the namespace B float sum2 = B::Sum(x, y); return 0; } namespace B { float Sum(float a, float b) { return (a+b)*(a+b); } };

11 11 Tests ● Syntax ● Example if ( expression ) { statement;... } else { statement;... } if ( x < 0 ) { abs = -x; sign = -1; } if ( x < 0 ) { abs = -x; sign = -1; } else { abs = x; signe = 1; } if ( x < 0 ) abs = -x; if ( x < 0 ) abs = -x; else abs = x; if ( expression ) { statement;... } else { statement;... } if ( x < 0 ) { abs = -x; sign = -1; } if ( x < 0 ) { abs = -x; sign = -1; } else { abs = x; signe = 1; } if ( x < 0 ) abs = -x; if ( x < 0 ) abs = -x; else abs = x; if ( expression ) { statement;... } else { statement;... } if ( x < 0 ) { abs = -x; sign = -1; } if ( x < 0 ) { abs = -x; sign = -1; } else { abs = x; signe = 1; } if ( x < 0 ) abs = -x; if ( x < 0 ) abs = -x; else abs = x; if ( expression ) { statement;... } – It is good to choose the style of indenting and use it consistently – The {} are not necessary if the expression does not exceed one line

12 12 Loops... while ( expression ) {... } int sum = 0; for ( int i = 0; i < 10; i++ ) { sum += i; } ● Syntax int i = 0; sum = 0; while ( i < 10 ) { sum += i++; } do {... } while ( expression ); for ( init-stmt; cont-expr; incr-expr ) {... } int i = 0; int sum = 0; do { sum += i++; } while ( i < 10 ); ● Example – We prefer while when it is possible that the block of code will not be executed – We prefer do if the block must always be executed (at least once)

13 13... Loops ● break - allows us to quit the loop earlier ● continue- allows to skip iteration – But be careful - it should not be abused (sign of bad programming style for ( int i = 0; i < 10; i++ ) { if ( i == j ) continue; if ( i > k ) break; // do something }

14 14 Pointers ● Pointer = an object that refers to another object ● The pointer type - formed by the type of the pointed object and * – Ex. int*, float * ● The pointer value = the address of the object pointed ● Access to the object by its pointer i: int 1 p: int* int i = 1; int* p = &i; Address operator *p = 5; Dereference operator i: int 5 p: int*

15 15 References ● Reference = an alias to an existing object ● Reference declaration: the type of object and & – Ex. int&, float& ● The differences between a reference and a pointer: – The pointer points to an object, you can change it to point to another object, it occupies memory (as the pointer is itself also an object) – The reference acts as another name of the object, it can not be changed to point to another object, it does not occupy memory space i: ir: int 1 int i = 1; int& ir = i; int* p; int* p = 0; int& ir; int& ir = 0; We can not declare a reference without its initialization; the reference can not be initialized to 0

16 16 Operators Overview ● Operators - provided in C++ for predefined (fundamental) types – Unary or binary – Arithmetic: x ++; ++ x; x --; -- x; + x; - x; x * y; x / y; x % y; x + y; x - y; – Relational: x y; x >= y; x == y; x != y; – Logical: !x; x && y; x || y – Bitwise: ~i; i & j; i ^ j; i | j; i > j; – Assignment: = ; +=; -=; *=; /=; %=; >>=; <<=; ^=; |= – Pointers: *p; &x; new T; new T[n]; delete p; delete [] p ● Type conversion – If the binary operator is applied to variables of different types, an automatic type conversion is performed, if possible int i = 1; float x = 15.6; float sum = x + i; Conversion of int to float


Download ppt "1 C++ Programming C++ Basics ● C++ Program ● Variables, objects, types ● Functions ● Namespace ● Tests ● Loops ● Pointers, references."

Similar presentations


Ads by Google