intro to c++

32
File Structure using C++

Upload: ahmed-farag

Post on 15-Apr-2017

686 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Intro to C++

File Structure using C++

Page 2: Intro to C++

Simple Example

Page 3: Intro to C++

#include<iostream>#Include

means read me before you compile and do what I say essentially. means that you're signaling the compiler to include some kind of file

or library that is needed in order for some function to execute in your source file.

< and > just enclosures for the compiler to read between and import

accordingly.

*stream <iostream> is a library for basic input and out put controls since C++

alone contains no facilities for IO.

Page 4: Intro to C++

#include<iostream>

• The #include is a "preprocessor" directive that tells the compiler to put code from the header called iostream into our program before actually creating the executable.

• By including header files, you gain access to many different functions.

• For example, the cout function requires iostream

Page 5: Intro to C++

using namespace std;

• This line tells the compiler to use a group of functions that are part of the standard library (std).

• By including this line at the top of a file, you allow the program to use functions such as cout

Page 6: Intro to C++

int main()

• This line tells the compiler that there is a function named main, and that the function returns an integer, hence int.

• The "curly braces" ({ and }) signal the beginning and end of functions and other code blocks.

Page 7: Intro to C++

Cout<<“Welcome C++”;

• The cout object is used to display text.• It uses the << symbols, known as "insertion

operators", to indicate what to output.

Page 8: Intro to C++

Declaring Variables in C++

Page 9: Intro to C++

Variables in C++• // operating with variables• #include <iostream>• using namespacestd; • intmain () • { • // declaring variables:• inta, b; • Int result; • // process:• a = 5; • b = 2; • a = a + 1; • result = a - b; • // print out the result:• cout << result; • // terminate the program:• return0; • }

Page 10: Intro to C++

Scope of variables

Page 11: Intro to C++

Initialization of variables

• inta = 0;• int a (0);

Page 12: Intro to C++

Initialization of variables• // initialization of variables• #include <iostream>• using namespace std; • intmain () • { • inta=5; // initial value = 5• Int b(2); // initial value = 2• Int result; // initial value undetermined• a = a + 3; • result = a - b; • cout << result; • return0; • }

Page 13: Intro to C++

Introduction to strings • // my first string• #include <iostream>• #include <string>• using namespace std; • Int main () • { • string mystring = "This is a string"; • cout << mystring; • Return 0; • }

Page 14: Intro to C++

Introduction to strings

• string mystring = "This is a string";• string mystring ("This is a string");

Page 15: Intro to C++

Constants (#define)• // defined constants: calculate circumference• #include <iostream>• using namespace std; • #define PI 3.14159• #define NEWLINE '\n'• Int main () • { • Double r=5.0; // radius• Double circle; • circle = 2 * PI * r; • cout << circle; • cout << NEWLINE; • Return 0; • }

Page 16: Intro to C++

Declared constants (const)

• const int pathwidth = 100; • const char tabulator = '\t';

Page 17: Intro to C++

Operators: Assignment (=) • // assignment operator• #include <iostream>• using namespacestd; • intmain () • { • inta, b; // a:?, b:?• a = 10; // a:10, b:?• b = 4; // a:10, b:4• a = b; // a:4, b:4• b = 7; // a:4, b:7• cout << "a:"; • cout << a; • cout << " b:"; • cout << b; • return0; • }

Page 18: Intro to C++

Compound assignment

Page 19: Intro to C++

• // compound assignment operators• #include <iostream>• using namespacestd; • intmain () • { • inta, b=3; • a = b; • a+=2; // equivalent to a=a+2• cout << a; • return0; • }

Page 20: Intro to C++

Increase and decrease (++, --)

Page 21: Intro to C++

Conditional operator ( ? ) • // conditional operator• #include <iostream>• using namespacestd; • intmain () • { • inta,b,c; • a=2; • b=7; • c = (a>b) ? a : b; • cout << c; • return0; • }

Page 22: Intro to C++

Conditional operator ( ? )

• 7==5 ? 4 : 3 // returns 3, since 7 is not equal to 5.

• 7==5+2 ? 4 : 3 // returns 4, since 7 is equal to 5+2.

• 5>3 ? a : b // returns the value of a, since 5 is greater than 3.

• a>b ? a : b // returns whichever is greater, a or b.

Page 23: Intro to C++

Basic Input/Output • // i/o example• #include <iostream>• using namespacestd; • intmain () • { • inti; • cout << "Please enter an integer value: "; • cin >> i; • cout << "The value you entered is "<< i; • cout << " and its double is "<< i*2 << ".\n";• return0; • }

Page 24: Intro to C++

cin and strings • // cin with strings• #include <iostream>• #include <string>• using namespacestd; • intmain () • { • string mystr; • cout << "What's your name? "; • getline (cin, mystr); • cout << "Hello "<< mystr << ".\n"; • cout << "What is your favorite team? "; • getline (cin, mystr); • cout << "I like "<< mystr << " too!\n"; • return0; • }

Page 25: Intro to C++

Conditional structure: if and else

• if(x > 0) • cout << "x is positive";• else if(x < 0) • cout << "x is negative";• else• cout << "x is 0";

Page 26: Intro to C++

The while loop• // custom countdown using while• #include <iostream>• using namespacestd; • intmain () • { • intn; • cout << "Enter the starting number > "; • cin >> n; • while(n>0) { • cout << n << ", "; • --n; • } • cout << "FIRE!\n"; • return0; • }

Page 27: Intro to C++

The do-while loop• // number echoer• #include <iostream>• using namespacestd; • intmain () • { • unsigned longn; • do{ • cout << "Enter number (0 to end): "; • cin >> n; • cout << "You entered: "<< n << "\n"; • } while(n != 0); • return0; • }

Page 28: Intro to C++

The for loop• // countdown using a for loop• #include <iostream>• using namespacestd; • intmain () • { • for(intn=10; n>0; n--) { • cout << n << ", "; • } • cout << "FIRE!\n"; • return0; • }

Page 29: Intro to C++
Page 30: Intro to C++

Jump statements (break)• // break loop example• #include <iostream>• using namespacestd; • intmain () • { • intn; • for(n=10; n>0; n--) • { • cout << n << ", "; • if(n==3) • { • cout << "countdown aborted!"; • break; • } • } • return0; • }

Page 31: Intro to C++

continue• // continue loop example• #include <iostream>• using namespacestd; • intmain () • { • for(intn=10; n>0; n--) { • if(n==5) continue; • cout << n << ", "; • } • cout << "FIRE!\n"; • return0; • }

Page 32: Intro to C++

goto• // goto loop example• #include <iostream>• using namespace std; • Int main () • { • Int n=10; • loop: • cout << n << ", "; • n--; • if(n>0) goto loop; • cout << "FIRE!\n"; • return0; • }