an introduction to c++ a first look. void functions #include #include void main( ) { cout
TRANSCRIPT
![Page 1: An Introduction to C++ A First Look. void Functions #include #include void main( ) { cout](https://reader037.vdocuments.site/reader037/viewer/2022110321/56649f4f5503460f94c70684/html5/thumbnails/1.jpg)
An Introduction to C++An Introduction to C++
A First LookA First Look
![Page 2: An Introduction to C++ A First Look. void Functions #include #include void main( ) { cout](https://reader037.vdocuments.site/reader037/viewer/2022110321/56649f4f5503460f94c70684/html5/thumbnails/2.jpg)
void Functionsvoid Functions
#include <iostream.h>#include <iostream.h>
void main( )void main( ){{
cout << “Hello world” << endl;cout << “Hello world” << endl;
}}
![Page 3: An Introduction to C++ A First Look. void Functions #include #include void main( ) { cout](https://reader037.vdocuments.site/reader037/viewer/2022110321/56649f4f5503460f94c70684/html5/thumbnails/3.jpg)
void Functionsvoid Functions
#include <iostream.h>#include <iostream.h>
void main()void main(){{
cout << “Hello world” << endl;cout << “Hello world” << endl;
}}
Hello worldHello world
program executionprogram execution
![Page 4: An Introduction to C++ A First Look. void Functions #include #include void main( ) { cout](https://reader037.vdocuments.site/reader037/viewer/2022110321/56649f4f5503460f94c70684/html5/thumbnails/4.jpg)
Input and OutputInput and Output
The term The term stream stream refers to the abstract model of anrefers to the abstract model of aninput or output device in which the input device input or output device in which the input device produces a stream of characters and an output produces a stream of characters and an output device receives a stream of characters.device receives a stream of characters.
#include <#include <iostream.hiostream.h>> cin and cout are defined cin and cout are defined
#include <#include <fstream.hfstream.h>> files may be created/, modified & deletedfiles may be created/, modified & deleted
![Page 5: An Introduction to C++ A First Look. void Functions #include #include void main( ) { cout](https://reader037.vdocuments.site/reader037/viewer/2022110321/56649f4f5503460f94c70684/html5/thumbnails/5.jpg)
StreamsStreams
C++ programmers say that the output operation C++ programmers say that the output operation ‘‘inserts’ characters into an output streaminserts’ characters into an output stream
<< insertion operator<< insertion operator
The input operation ‘extracts’ characters The input operation ‘extracts’ characters from an input stream.from an input stream.
>> >> extraction operatorextraction operator
![Page 6: An Introduction to C++ A First Look. void Functions #include #include void main( ) { cout](https://reader037.vdocuments.site/reader037/viewer/2022110321/56649f4f5503460f94c70684/html5/thumbnails/6.jpg)
cin, coutcin, cout
cout cout <<<< “Enter a word: “; “Enter a word: “;cin cin >>>> aword; aword;
cout cout <<<< “Thanks for entering “ << aword ; “Thanks for entering “ << aword ;cout cout <<<< endl; endl;
<< insertion operator<< insertion operator>> >> extraction operatorextraction operator
![Page 7: An Introduction to C++ A First Look. void Functions #include #include void main( ) { cout](https://reader037.vdocuments.site/reader037/viewer/2022110321/56649f4f5503460f94c70684/html5/thumbnails/7.jpg)
Functions that return thingsFunctions that return things
#include <iostream.h>#include <iostream.h>
int main( )int main( ){{
int number;int number;
cout << “Enter a number : “;cout << “Enter a number : “;cin >> number;cin >> number;cout << “Thanks for “ << number << endl;cout << “Thanks for “ << number << endl;
return 0;return 0;}}
![Page 8: An Introduction to C++ A First Look. void Functions #include #include void main( ) { cout](https://reader037.vdocuments.site/reader037/viewer/2022110321/56649f4f5503460f94c70684/html5/thumbnails/8.jpg)
Functions that return thingsFunctions that return things
#include <iostream.h>#include <iostream.h>
int main()int main(){{
int number;int number;
cout << “Enter a number : “;cout << “Enter a number : “;cin >> number;cin >> number;cout << “Thanks for “ << number << endl;cout << “Thanks for “ << number << endl;
return 0;return 0;}}
Enter a number: 10Enter a number: 10Thanks for 10Thanks for 10
program executionprogram execution
![Page 9: An Introduction to C++ A First Look. void Functions #include #include void main( ) { cout](https://reader037.vdocuments.site/reader037/viewer/2022110321/56649f4f5503460f94c70684/html5/thumbnails/9.jpg)
C++ built-in DatatypesC++ built-in Datatypes
int int (e.g. -6)(e.g. -6)
long long (e.g. 40000)(e.g. 40000)
floatfloat (e.g. -8.4)(e.g. -8.4)
double double (e.g. 3.14)(e.g. 3.14)
boolbool (e.g. true)(e.g. true)
charchar (e.g. ‘a’)(e.g. ‘a’)
![Page 10: An Introduction to C++ A First Look. void Functions #include #include void main( ) { cout](https://reader037.vdocuments.site/reader037/viewer/2022110321/56649f4f5503460f94c70684/html5/thumbnails/10.jpg)
C++ built-in DatatypesC++ built-in Datatypes
int int (e.g. -6)(e.g. -6)
long long (e.g. 40000)(e.g. 40000)
floatfloat (e.g. -8.4)(e.g. -8.4)
double double (e.g. 3.14)(e.g. 3.14)
boolbool (e.g. true)(e.g. true)
charchar (e.g. ‘a’)(e.g. ‘a’)
Used on AP examsUsed on AP exams
![Page 11: An Introduction to C++ A First Look. void Functions #include #include void main( ) { cout](https://reader037.vdocuments.site/reader037/viewer/2022110321/56649f4f5503460f94c70684/html5/thumbnails/11.jpg)
Other Datatypes implemented using ClassesOther Datatypes implemented using Classes
apstring name; apstring name; (e.g. “Smith”)(e.g. “Smith”)
apvector<int> Aapvector<int> A (e.g. integer arrays)(e.g. integer arrays)
apmatrix<int> A apmatrix<int> A (e.g. multi-dimensional arrays)(e.g. multi-dimensional arrays)
BigInt number BigInt number (e.g. 999999999)(e.g. 999999999)
apstack<int> Aapstack<int> A (e.g. stacks)(e.g. stacks)
apqueue<int> Aapqueue<int> A (e.g. queues)(e.g. queues)AB Exam OnlyAB Exam Only
![Page 12: An Introduction to C++ A First Look. void Functions #include #include void main( ) { cout](https://reader037.vdocuments.site/reader037/viewer/2022110321/56649f4f5503460f94c70684/html5/thumbnails/12.jpg)
PreprocessorPreprocessor
pre-processor commands begin with pre-processor commands begin with ## pre-processor commands are serviced pre-processor commands are serviced
first by the pre-processor which occurs first by the pre-processor which occurs before compilationbefore compilation
![Page 13: An Introduction to C++ A First Look. void Functions #include #include void main( ) { cout](https://reader037.vdocuments.site/reader037/viewer/2022110321/56649f4f5503460f94c70684/html5/thumbnails/13.jpg)
PreprocessorPreprocessor
#include is a pre-processor directive which #include is a pre-processor directive which inserts the contents of the file into the inserts the contents of the file into the current filecurrent file• #include <iostream.h>#include <iostream.h>
– angle brackets indicate to get the *.h file from a angle brackets indicate to get the *.h file from a previous defined ‘header directory’, which is known previous defined ‘header directory’, which is known to the environmentto the environment
• #include “apstring.h” #include “apstring.h” – double quotes mean that the file to merge in, is double quotes mean that the file to merge in, is
found in current directoryfound in current directory
![Page 14: An Introduction to C++ A First Look. void Functions #include #include void main( ) { cout](https://reader037.vdocuments.site/reader037/viewer/2022110321/56649f4f5503460f94c70684/html5/thumbnails/14.jpg)
Preprocessor...Preprocessor...
#ifndef #ifndef _DICE_H _DICE_H#define _DICE_H#define _DICE_H
..
..
..#endif#endif
The above technique is standard practice for most headerThe above technique is standard practice for most headerfiles. IFNDEF ensures that the code sandwiched betweenfiles. IFNDEF ensures that the code sandwiched betweenIFNDEF and ENDIF, will not be seen by the compiler if the IFNDEF and ENDIF, will not be seen by the compiler if the symbol symbol _DICE_H _DICE_H has already been defined. This has already been defined. This trick ensures that this code is only given once to the compiler.trick ensures that this code is only given once to the compiler.
![Page 15: An Introduction to C++ A First Look. void Functions #include #include void main( ) { cout](https://reader037.vdocuments.site/reader037/viewer/2022110321/56649f4f5503460f94c70684/html5/thumbnails/15.jpg)
Function PrototypesFunction Prototypes
A function’s prototype is the first line of the functionA function’s prototype is the first line of the function
void GetPayment (double amt)void GetPayment (double amt){{
}}
![Page 16: An Introduction to C++ A First Look. void Functions #include #include void main( ) { cout](https://reader037.vdocuments.site/reader037/viewer/2022110321/56649f4f5503460f94c70684/html5/thumbnails/16.jpg)
Function PrototypesFunction Prototypes
The compiler needs the function The compiler needs the function prototype for each function made prototype for each function made available to it, available to it, unless unless the function the function has been defined prior to its use.has been defined prior to its use.
![Page 17: An Introduction to C++ A First Look. void Functions #include #include void main( ) { cout](https://reader037.vdocuments.site/reader037/viewer/2022110321/56649f4f5503460f94c70684/html5/thumbnails/17.jpg)
Function PrototypesFunction Prototypes
![Page 18: An Introduction to C++ A First Look. void Functions #include #include void main( ) { cout](https://reader037.vdocuments.site/reader037/viewer/2022110321/56649f4f5503460f94c70684/html5/thumbnails/18.jpg)
Function PrototypesFunction Prototypes
Library functions have their prototypes defined Library functions have their prototypes defined within ‘header files’ located at the top of most C++within ‘header files’ located at the top of most C++programs.programs.
#include <math.h>#include <math.h>#include <iostream.h>#include <iostream.h>
void main()void main(){{
}}
![Page 19: An Introduction to C++ A First Look. void Functions #include #include void main( ) { cout](https://reader037.vdocuments.site/reader037/viewer/2022110321/56649f4f5503460f94c70684/html5/thumbnails/19.jpg)
/* math.h standard header *//* math.h standard header */#ifndef __MATH_H#ifndef __MATH_H#define __MATH_H#define __MATH_H
/* float declarations *//* float declarations */float cos(float x);float cos(float x);float sin(float x) ;float sin(float x) ;float tan(float x);float tan(float x);
#endif#endif
Inside .... Inside .... #include <math.h> #include <math.h>
![Page 20: An Introduction to C++ A First Look. void Functions #include #include void main( ) { cout](https://reader037.vdocuments.site/reader037/viewer/2022110321/56649f4f5503460f94c70684/html5/thumbnails/20.jpg)
Parameter PassingParameter Passing
pass by VALUEpass by VALUE• int SQUARED ( int N );int SQUARED ( int N );• function SQUARED ( N : integer) : integer;function SQUARED ( N : integer) : integer;
pass by REFERENCEpass by REFERENCE• int SQUARED ( int & N);int SQUARED ( int & N);• function SQUARED (var N : integer) : integer;function SQUARED (var N : integer) : integer;
pass by CONSTANT REFERENCEpass by CONSTANT REFERENCE• int SQUARED (const int & N);int SQUARED (const int & N);
![Page 21: An Introduction to C++ A First Look. void Functions #include #include void main( ) { cout](https://reader037.vdocuments.site/reader037/viewer/2022110321/56649f4f5503460f94c70684/html5/thumbnails/21.jpg)
ExampleExample
Write a C++ main program which asks Write a C++ main program which asks the user to enter an integer called, ‘N’.the user to enter an integer called, ‘N’.
Output the absolute value of N by Output the absolute value of N by writing a function called, writing a function called, ABSOLUTE(N).ABSOLUTE(N).
Sample Session:Sample Session: Enter N: -9Enter N: -9
Absolute Value = 9Absolute Value = 9
![Page 22: An Introduction to C++ A First Look. void Functions #include #include void main( ) { cout](https://reader037.vdocuments.site/reader037/viewer/2022110321/56649f4f5503460f94c70684/html5/thumbnails/22.jpg)
SolutionSolution #include <iostream.h>#include <iostream.h>
int ABSOLUTE( int N);int ABSOLUTE( int N);
int main()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;}}
![Page 23: An Introduction to C++ A First Look. void Functions #include #include void main( ) { cout](https://reader037.vdocuments.site/reader037/viewer/2022110321/56649f4f5503460f94c70684/html5/thumbnails/23.jpg)
SolutionSolution
int ABSOLUTE( int N);int ABSOLUTE( int N);{{
int value=N;int value=N;
if (value < 0) if (value < 0) value = -1 * value;value = -1 * value;
return value;return value;}}
![Page 24: An Introduction to C++ A First Look. void Functions #include #include void main( ) { cout](https://reader037.vdocuments.site/reader037/viewer/2022110321/56649f4f5503460f94c70684/html5/thumbnails/24.jpg)
ConstantsConstants
#define MAXWORDS 1000#define MAXWORDS 1000
OR BETTER .... TO DOOR BETTER .... TO DO
const int MAXWORDS = 1000;const int MAXWORDS = 1000;
![Page 25: An Introduction to C++ A First Look. void Functions #include #include void main( ) { cout](https://reader037.vdocuments.site/reader037/viewer/2022110321/56649f4f5503460f94c70684/html5/thumbnails/25.jpg)
ConditionsConditions
If ( numA == numB)If ( numA == numB)numC = numA;numC = numA;
elseelsenumC = numB;numC = numB;
== == equalsequals!= != not equal tonot equal to< < less thanless than> > greater thangreater than<= <= less than orless than or equal toequal to>= >= greater thangreater than or equal toor equal to&& && ANDAND|| || OROR
![Page 26: An Introduction to C++ A First Look. void Functions #include #include void main( ) { cout](https://reader037.vdocuments.site/reader037/viewer/2022110321/56649f4f5503460f94c70684/html5/thumbnails/26.jpg)
ExerciseExercise
Write a C++ program which constructs Write a C++ program which constructs 3 double variables called, ‘X’ , ‘Y’ & ‘Z’.3 double variables called, ‘X’ , ‘Y’ & ‘Z’.
Output the middle number of ‘X’, ‘Y’ & ‘Z’.Output the middle number of ‘X’, ‘Y’ & ‘Z’.
Sample Session:Sample Session: Enter x: 6Enter x: 6
Enter y: 5Enter y: 5Enter z: 7Enter z: 7The middle number is 6The middle number is 6
![Page 27: An Introduction to C++ A First Look. void Functions #include #include void main( ) { cout](https://reader037.vdocuments.site/reader037/viewer/2022110321/56649f4f5503460f94c70684/html5/thumbnails/27.jpg)
SolutionSolution#include <iostream.h>#include <iostream.h>
int main()int main(){{ double x, y, z, sum, highest, lowest, middle; double x, y, z, sum, highest, lowest, middle;
cout << “Enter x: “ ; cout << “Enter x: “ ; cin >> x;cin >> x;cout << “Enter y: “ ; cout << “Enter y: “ ; cin >> y;cin >> y;cout << “Enter z: “’cout << “Enter z: “’ cin >> z;cin >> z;sum = x + y + z;sum = x + y + z;
highest = x;highest = x; //Determine highest//Determine highestif (y > highest) highest = y;if (y > highest) highest = y;if (z > highest) highest = z;if (z > highest) highest = z;
lowest = x;lowest = x; //Determine lowest//Determine lowestif (y < lowest) lowest = y;if (y < lowest) lowest = y;if (z < lowest) lowest = z;if (z < lowest) lowest = z;
middle = sum - highest - lowest;middle = sum - highest - lowest;cout << “The middle number is “ << middle;cout << “The middle number is “ << middle;return 0;return 0;
}}
![Page 28: An Introduction to C++ A First Look. void Functions #include #include void main( ) { cout](https://reader037.vdocuments.site/reader037/viewer/2022110321/56649f4f5503460f94c70684/html5/thumbnails/28.jpg)
ClassesClasses
Classes are objects with behavioursClasses are objects with behaviours
DICE CLASSDICE CLASS
ObjectObject RedRed
BehavioursBehaviours Roll()Roll() NumSides()NumSides() NumRolls()NumRolls()
![Page 29: An Introduction to C++ A First Look. void Functions #include #include void main( ) { cout](https://reader037.vdocuments.site/reader037/viewer/2022110321/56649f4f5503460f94c70684/html5/thumbnails/29.jpg)
ClassesClasses
Classes are objects with behavioursClasses are objects with behaviours
DICE CLASSDICE CLASS
ObjectObject RedRed
BehavioursBehaviours Roll()Roll() NumSides()NumSides() NumRolls()NumRolls()
Member FunctionsMember Functionsa Red diea Red die
![Page 30: An Introduction to C++ A First Look. void Functions #include #include void main( ) { cout](https://reader037.vdocuments.site/reader037/viewer/2022110321/56649f4f5503460f94c70684/html5/thumbnails/30.jpg)
The Dice ClassThe Dice Class#include <iostream.h>#include <iostream.h>#include “dice.h”#include “dice.h”
int main( )int main( ){{ int g, r, totalrolls;int g, r, totalrolls;
Dice red(6), green(6);Dice red(6), green(6);
g = green.Roll( );g = green.Roll( ); // Roll the Dice// Roll the Dicer = red.Roll( );r = red.Roll( );totalrolls = red.NumRolls( ) + green.NumRolls( );totalrolls = red.NumRolls( ) + green.NumRolls( );
cout << “Sum: “ << g + r << endl;;cout << “Sum: “ << g + r << endl;;cout << “Total : “ << totalrolls << endl;cout << “Total : “ << totalrolls << endl;return 0;return 0;
}}
![Page 31: An Introduction to C++ A First Look. void Functions #include #include void main( ) { cout](https://reader037.vdocuments.site/reader037/viewer/2022110321/56649f4f5503460f94c70684/html5/thumbnails/31.jpg)
ExerciseExercise
Write a C++ program which constructs Write a C++ program which constructs 5 red dice with names, 5 red dice with names, r1, r2, r3, r4 & r5.r1, r2, r3, r4 & r5.
Construct an integer variable called, Construct an integer variable called, ‘‘points’ and initialize it to 0.points’ and initialize it to 0.
Roll the 5 dice once! If a Yahtzee occursRoll the 5 dice once! If a Yahtzee occursincrement the points variable by 50.increment the points variable by 50.
![Page 32: An Introduction to C++ A First Look. void Functions #include #include void main( ) { cout](https://reader037.vdocuments.site/reader037/viewer/2022110321/56649f4f5503460f94c70684/html5/thumbnails/32.jpg)
SolutionSolution#include <iostream.h>#include <iostream.h>#include “dice.h”#include “dice.h”
int main( )int main( ){{ int points=0;int points=0;
Dice r1(6), r2(6), r3(6), r4(6), r5(6);Dice r1(6), r2(6), r3(6), r4(6), r5(6);int a = r1.Roll( ); int b = r2.Roll( );int a = r1.Roll( ); int b = r2.Roll( );int c = r3.Roll( );int c = r3.Roll( ); int d = r3.Roll( );int d = r3.Roll( );int e = r5.Roll( );int e = r5.Roll( );
if ((a==b) && (a==c) && (a==d) &&if ((a==b) && (a==c) && (a==d) && a==e))a==e))
points = points + 50;points = points + 50;
cout << points;cout << points;return 0;return 0;
}}
![Page 33: An Introduction to C++ A First Look. void Functions #include #include void main( ) { cout](https://reader037.vdocuments.site/reader037/viewer/2022110321/56649f4f5503460f94c70684/html5/thumbnails/33.jpg)