390 likes | 477 Views
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.).
E N D
Introduction C General purpose Block structured Procedural Imperative Programming Languages - Gilbert Khoury - Fouad Kada
Introduction (ctd.) C++ General purpose Mid-Level language Procedural Object Oriented Programming Based on C Programming Languages - Gilbert Khoury - Fouad Kada
Program Structure Programming Languages - Gilbert Khoury - Fouad Kada
Variables Data Types Programming Languages - Gilbert Khoury - Fouad Kada
Variables Data Types (Ctd.) Programming Languages - Gilbert Khoury - Fouad Kada
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
Scope of Variables Programming Languages - Gilbert Khoury - Fouad Kada
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
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
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
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
Basic Input/Output In C: Basic Output: the method printf is used to print to the screen. Programming Languages - Gilbert Khoury - Fouad Kada
Basic Input/Output Programming Languages - Gilbert Khoury - Fouad Kada
Basic Input/Output In C: Basic Input: the method scanf is used to read from keyboard. Programming Languages - Gilbert Khoury - Fouad Kada
Basic Input/Output In C++: Basic Output: “cout” is used to write to standard output Programming Languages - Gilbert Khoury - Fouad Kada
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
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
Iteration StructuresLoops While Loop: Structure: while (expression) statement Programming Languages - Gilbert Khoury - Fouad Kada
Iteration StructuresLoops (Ctd.) Do – While Loop: Structure: while (expression) statement Programming Languages - Gilbert Khoury - Fouad Kada
Iteration StructuresLoops (Ctd.) For Loop: Structure: for (initialization; condition; increase) statement; Programming Languages - Gilbert Khoury - Fouad Kada
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
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
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
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
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
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
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
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
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;
Pointers(Ctd.) • Double pointers and pointers arithmetic are beyond the scope of this tutrial but are very powerful
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.
Classes • C++ being an OO language thus it have classes. • Same concept of JAVA classes
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 ~
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
References • http://www.cplusplus.com/doc/tutorial/ • http://www.cprogramming.com/tutorial.html#ctutorial
Thank You Programming Languages - Gilbert Khoury - Fouad Kada