Presentation is loading. Please wait.

Presentation is loading. Please wait.

An Introduction to C++ A First Look. void Functions #include #include void main( ) { cout << “Hello world” << endl; }

Similar presentations


Presentation on theme: "An Introduction to C++ A First Look. void Functions #include #include void main( ) { cout << “Hello world” << endl; }"— Presentation transcript:

1 An Introduction to C++ A First Look

2 void Functions #include #include void main( ) { cout << “Hello world” << endl; }

3 void Functions #include #include void main() { cout << “Hello world” << endl; } Hello world program execution

4 Input and Output The term stream refers to the abstract model of an input or output device in which the input device produces a stream of characters and an output device receives a stream of characters. #include #include cin and cout are defined cin and cout are defined #include #include files may be created/, modified & deleted files may be created/, modified & deleted

5 Streams C++ programmers say that the output operation ‘inserts’ characters into an output stream << insertion operator The input operation ‘extracts’ characters from an input stream. >> extraction operator

6 cin, cout cout << “Enter a word: “; cin >> aword; cout << “Thanks for entering “ << aword ; cout << endl; << insertion operator >> extraction operator

7 Functions that return things #include #include int main( ) { int number; cout << “Enter a number : “; cin >> number; cout << “Thanks for “ << number << endl; return 0; }

8 Functions that return things #include #include int main() { int number; cout << “Enter a number : “; cin >> number; cout << “Thanks for “ << number << endl; return 0; } Enter a number: 10 Thanks for 10 program execution

9 C++ built-in Datatypes n int (e.g. -6) n long (e.g. 40000) n float (e.g. -8.4) n double (e.g. 3.14) n bool (e.g. true) n char (e.g. ‘a’)

10 C++ built-in Datatypes n int (e.g. -6) n long (e.g. 40000) n float (e.g. -8.4) n double (e.g. 3.14) n bool (e.g. true) n char (e.g. ‘a’) Used on AP exams

11 Other Datatypes implemented using Classes n apstring name; (e.g. “Smith”) n apvector A (e.g. integer arrays) n apmatrix A (e.g. multi-dimensional arrays) n BigInt number (e.g. 999999999) n apstack A (e.g. stacks) n apqueue A (e.g. queues) AB Exam Only

12 Preprocessor n pre-processor commands begin with # n pre-processor commands are serviced first by the pre-processor which occurs before compilation

13 Preprocessor n #include is a pre-processor directive which inserts the contents of the file into the current file #include #include – angle brackets indicate to get the *.h file from a previous defined ‘header directory’, which is known to the environment #include “apstring.h” #include “apstring.h” – double quotes mean that the file to merge in, is found in current directory

14 Preprocessor... #ifndef _DICE_H #define _DICE_H...#endif The above technique is standard practice for most header files. IFNDEF ensures that the code sandwiched between IFNDEF and ENDIF, will not be seen by the compiler if the symbol _DICE_H has already been defined. This trick ensures that this code is only given once to the compiler.

15 Function Prototypes A function’s prototype is the first line of the function void GetPayment (double amt) {}

16 Function Prototypes The compiler needs the function prototype for each function made available to it, unless the function has been defined prior to its use.

17 Function Prototypes

18 Library functions have their prototypes defined within ‘header files’ located at the top of most C++ programs. #include #include void main() {}

19 /* math.h standard header */ #ifndef __MATH_H #define __MATH_H /* float declarations */ float cos(float x); float sin(float x) ; float tan(float x); #endif Inside.... #include Inside.... #include

20 Parameter Passing n pass by VALUE int SQUARED ( int N ); int SQUARED ( int N ); function SQUARED ( N : integer) : integer; function SQUARED ( N : integer) : integer; n pass by REFERENCE int SQUARED ( int & N); int SQUARED ( int & N); function SQUARED (var N : integer) : integer; function SQUARED (var N : integer) : integer; n pass by CONSTANT REFERENCE int SQUARED (const int & N); int SQUARED (const int & N);

21 Example Write a C++ main program which asks the user to enter an integer called, ‘N’. Output the absolute value of N by writing a function called, ABSOLUTE(N). ABSOLUTE(N). Sample Session: Enter N: -9 Enter N: -9 Absolute Value = 9

22 Solution #include #include int ABSOLUTE( int N); int main() { int N ; int N ; cout << “Enter N: “ ; cout << “Enter N: “ ; cin >> N; cin >> N; cout << “Absolute Value = “ cout << “Absolute Value = “ << ABSOLUTE(N) << endl; << ABSOLUTE(N) << endl; return 0; return 0;}

23 Solution int ABSOLUTE( int N); { int value=N; if (value < 0) value = -1 * value; value = -1 * value; return value; }

24 Constants #define MAXWORDS 1000 OR BETTER.... TO DO const int MAXWORDS = 1000;

25 Conditions If ( numA == numB) numC = numA; else numC = numB; == equals != not equal to < less than > greater than <= less than or equal to equal to >= greater than or equal to or equal to && AND || OR

26 Exercise Write a C++ program which constructs 3 double variables called, ‘X’, ‘Y’ & ‘Z’. Output the middle number of ‘X’, ‘Y’ & ‘Z’. Sample Session: Sample Session: Enter x: 6 Enter x: 6 Enter y: 5 Enter z: 7 The middle number is 6

27 Solution #include #include int main() {double x, y, z, sum, highest, lowest, middle; cout > x; cout > y; cout > z; sum = x + y + z; highest = x;//Determine highest if (y > highest) highest = y; if (z > highest) highest = z; lowest = x;//Determine lowest if (y < lowest) lowest = y; if (z < lowest) lowest = z; middle = sum - highest - lowest; cout << “The middle number is “ << middle; return 0; }

28 Classes n Classes are objects with behaviours DICE CLASS Object Red Red Behaviours Roll() Roll() NumSides() NumSides() NumRolls() NumRolls()

29 Classes n Classes are objects with behaviours DICE CLASS Object Red Red Behaviours Roll() Roll() NumSides() NumSides() NumRolls() NumRolls() Member Functions a Red die

30 The Dice Class #include #include #include “dice.h” int main( ) {int g, r, totalrolls; Dice red(6), green(6); g = green.Roll( );// Roll the Dice r = red.Roll( ); totalrolls = red.NumRolls( ) + green.NumRolls( ); cout << “Sum: “ << g + r << endl;; cout << “Total : “ << totalrolls << endl; return 0; }

31 Exercise Write a C++ program which constructs 5 red dice with names, r1, r2, r3, r4 & r5. r1, r2, r3, r4 & r5. Construct an integer variable called, ‘points’ and initialize it to 0. Roll the 5 dice once! If a Yahtzee occurs increment the points variable by 50.

32 Solution #include #include #include “dice.h” int main( ) {int points=0; Dice r1(6), r2(6), r3(6), r4(6), r5(6); int a = r1.Roll( ); int b = r2.Roll( ); int c = r3.Roll( );int d = r3.Roll( ); int e = r5.Roll( ); if ((a==b) && (a==c) && (a==d) && a==e)) a==e)) points = points + 50; cout << points; return 0; }

33


Download ppt "An Introduction to C++ A First Look. void Functions #include #include void main( ) { cout << “Hello world” << endl; }"

Similar presentations


Ads by Google