1 / 39

Introduction

Introduction. C General purpose Block structured Procedural Imperative. Introduction (ctd.). C ++ General purpose Mid-Level language Procedural Object Oriented Programming Based on C. Program Structure. Variables Data Types. Variables Data Types (Ctd.).

Download Presentation

Introduction

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction C General purpose Block structured Procedural Imperative Programming Languages - Gilbert Khoury - Fouad Kada

  2. Introduction (ctd.) C++ General purpose Mid-Level language Procedural Object Oriented Programming Based on C Programming Languages - Gilbert Khoury - Fouad Kada

  3. Program Structure Programming Languages - Gilbert Khoury - Fouad Kada

  4. Variables Data Types Programming Languages - Gilbert Khoury - Fouad Kada

  5. Variables Data Types (Ctd.) Programming Languages - Gilbert Khoury - Fouad Kada

  6. Variables Data Types (Ctd.) Declaring Variables: <data type> <variable name> <data type> <var1, var2, var3, … > Initializing Variables: int a = 0; int a(0); Notice: If a variable is declared and called to output, without being initialized, the output will be a weird value, not an error message as in JAVA. Programming Languages - Gilbert Khoury - Fouad Kada

  7. Scope of Variables Programming Languages - Gilbert Khoury - Fouad Kada

  8. Strings – C++ • C++ language library provides support for strings through the standard string class. • NOT a fundamental type, but it behaves in a similar way as fundamental types do in its most basic usage. Programming Languages - Gilbert Khoury - Fouad Kada

  9. Strings (Ctd.) Example of String manipulation: #include <iostream> #include <string> using namespace std; int main () { string mystring; mystring = "This is the initial string content"; cout << mystring << endl; mystring = "This is a different string content"; cout << mystring << endl; return 0; } Programming Languages - Gilbert Khoury - Fouad Kada

  10. Constants • The “const” prefix: it is the equivalent of the “final” in JAVA • The “define” gives the ability to define our own names for constants that you use very often without having to resort to memory-consuming variables usage: #define identifier value example: #define PI 3.14159265 #define NEWLINE '\n' Programming Languages - Gilbert Khoury - Fouad Kada

  11. Operators • Assignment: the assignment in C\C++ works in the same way JAVA does, for example: a=5; puts 5 in the variable a. • Arithmetic Operators(+,-,*,/,%): same as JAVA • Increase and decrease (++ , --) : same as JAVA • Relational and equality operators ( ==, !=, >, <, >=, <= ): same as JAVA • Logical operators ( !, &&, || ): same as JAVA Programming Languages - Gilbert Khoury - Fouad Kada

  12. Basic Input/Output In C: Basic Output: the method printf is used to print to the screen. Programming Languages - Gilbert Khoury - Fouad Kada

  13. Basic Input/Output Programming Languages - Gilbert Khoury - Fouad Kada

  14. Basic Input/Output In C: Basic Input: the method scanf is used to read from keyboard. Programming Languages - Gilbert Khoury - Fouad Kada

  15. Basic Input/Output In C++: Basic Output: “cout” is used to write to standard output Programming Languages - Gilbert Khoury - Fouad Kada

  16. Basic Input/Output In C++: Basic Input: “cin” is used to read from keyboard, but it fails to read strings with spaces so instead we use the method getline Programming Languages - Gilbert Khoury - Fouad Kada

  17. Conditional StructuresIf - Else In both C and C++, the if statement, is similar to JAVA. Ex: if (x == 100) cout << "x is 100"; else cout << "x is not 100"; Programming Languages - Gilbert Khoury - Fouad Kada

  18. Iteration StructuresLoops While Loop: Structure: while (expression) statement Programming Languages - Gilbert Khoury - Fouad Kada

  19. Iteration StructuresLoops (Ctd.) Do – While Loop: Structure: while (expression) statement Programming Languages - Gilbert Khoury - Fouad Kada

  20. Iteration StructuresLoops (Ctd.) For Loop: Structure: for (initialization; condition; increase) statement; Programming Languages - Gilbert Khoury - Fouad Kada

  21. Functions (I) Using functions we can structure our programs in a more modular way, accessing all the potential that structured programming can offer to us in C++. A function is a group of statements that is executed when it is called from some point of the program. The following is its format: type name ( parameter1, parameter2, ...) { statements } Programming Languages - Gilbert Khoury - Fouad Kada

  22. Functions (I) (Ctd.) Here is an example of a multiply function in C: #include <stdio.h> intmult (int x, int y) { return x * y; } int main() { int x; int y; scanf( "%d", &x ); scanf( "%d", &y ); printf( "The product of your two numbers is %d\n", mult( x, y ) ); } Programming Languages - Gilbert Khoury - Fouad Kada

  23. Functions (I) (Ctd.) Here is an example of a multiply function in C++: #include <iostream> using namespace std; int addition (int a, int b) { intr; r=a+b; return (r); } intmain () { int z; z = addition (5,3); cout << "The result is " << z; return 0; } Programming Languages - Gilbert Khoury - Fouad Kada

  24. Functions (II) Arguments by value or by reference: In the addition function, arguments were passed directly by their value. We might need to pass them by reference in some cases: void duplicate (int& a, int& b, int& c) { a*=2; b*=2; c*=2; } intmain () { int x=1, y=3, z=7; duplicate (x, y, z); return 0; } Programming Languages - Gilbert Khoury - Fouad Kada

  25. Functions (II) (Ctd.) Overloaded Functions: In C++ two different functions can have the same name if their parameter types or number are different. That means that you can give the same name to more than one function if they have either a different number of parameters or different types in their parameters. Programming Languages - Gilbert Khoury - Fouad Kada

  26. Functions (II) (Ctd.) #include <iostream> using namespace std; intoperate (inta, int b) { return (a*b); } float operate (float a, float b) { return (a/b); } int main () { ………… } Programming Languages - Gilbert Khoury - Fouad Kada

  27. Arrays In C and C++, arrays are declared, initialized and accessed the same way as done in JAVA. Structure: type name [elements]; Declaration: int billy [5]; Initialization: int billy [5] = { 16, 2, 77, 40, 12071 }; Accessing Values: billy[2] = 75; Programming Languages - Gilbert Khoury - Fouad Kada

  28. Arrays (Ctd.) Code Example in C++: #include <iostream> using namespace std; intbilly [] = {16, 2, 77, 40, 12071}; int n, result=0; intmain () { for ( n=0 ; n<5 ; n++ ) { result += billy[n]; } cout << result; return 0; } Programming Languages - Gilbert Khoury - Fouad Kada

  29. Pointers • memory of a computer can be imagined as a succession of memory cells • in some cases we may be interested in knowing the address where a variable is stored at runtime in order to operate with relative positions to it -> that’s the use of pointers. • & is the reference operator and can be read as "address of" • * is the dereference operator and can be read as "value pointed by" • To declare a pointer we specify its type and its name and we also use the * symbol • E.g.: int * num; float * p2; double * p1;

  30. Pointers(Ctd.) • Double pointers and pointers arithmetic are beyond the scope of this tutrial but are very powerful

  31. Dynamic memory • Declaring arrays in JAVA is simple and taked for granted…here it is different…we have to use pointers. e.g. int * bob; bob = new int[4]; • After we are done with using an array with dynamic memory, we have to delete it due to the lack of automatic garbage collection.

  32. Dynamic memory(Ctd.)

  33. Classes • C++ being an OO language thus it have classes. • Same concept of JAVA classes

  34. Classes (Ctd.) • Constructor in C++ have the same concept of the JAVA constructors • Destructors a used because C++ doesn’t have a garbage collector so we must “delete” the class after we are done • Constructors and destructors have the same name of the class with no return type. • Destructors are preceded by a ~

  35. Classes(Ctd.)

  36. Classes(Ctd.) • We can, as in JAVA, overload a constructor. • We can have pointers to classes but this subject is beyond the scope of this tutorial

  37. References • http://www.cplusplus.com/doc/tutorial/ • http://www.cprogramming.com/tutorial.html#ctutorial

  38. Thank You  Programming Languages - Gilbert Khoury - Fouad Kada

More Related