intro to c++

Post on 15-Apr-2017

686 Views

Category:

Education

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

File Structure using C++

Simple Example

#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.

#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

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

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.

Cout<<“Welcome C++”;

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

operators", to indicate what to output.

Declaring Variables in 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; • }

Scope of variables

Initialization of variables

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

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; • }

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; • }

Introduction to strings

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

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; • }

Declared constants (const)

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

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; • }

Compound assignment

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

Increase and decrease (++, --)

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

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.

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; • }

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; • }

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";

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; • }

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; • }

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; • }

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; • }

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; • }

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; • }

top related