Programming in C If programming in Pascal is

  • Slides: 11
Download presentation
Programming in C++ “If programming in Pascal is like being put in a straightjacket,

Programming in C++ “If programming in Pascal is like being put in a straightjacket, then programming in C is like playing with knives, and programming in C++ is like juggling with chainsaws” Anonymous.

A First C++ Program #include <iostream> using namespace std; int main(){ cout << “Hello

A First C++ Program #include <iostream> using namespace std; int main(){ cout << “Hello World” << “n”; } Output: Hello World

Console Applications • This program is an example of a Console application • Console

Console Applications • This program is an example of a Console application • Console I/O functions perform – input from the system’s standard input device stdin (usually a keyboard) – and output to the system’s standard output device: stdout (usually a screen) • Standard input and output may be redirected to other devices.

A First C++ Program #include <iostream> using namespace std; int main(){ Used to include

A First C++ Program #include <iostream> using namespace std; int main(){ Used to include input and output definitions in the program Defines the entry point for the program cout << "Hello Worldn"; return 0 } Terminates the main function Executed Statement

 • int main()is a function which is the entry point for every C++

• int main()is a function which is the entry point for every C++ program. • { and } are used to bracket the body of the function main(). • The expression sends the string “Hello World” followed by a newline to the current output stream cout. • cout is an object which is normally “attached” to the terminal. (It could also be attached, say, to a printer). • You may also use cout in this way: cout << “ 1+2+3 is: “ << 1+2+3 << “n”;

Return Values • Functions in C++ normally return a value when they finish •

Return Values • Functions in C++ normally return a value when they finish • If you do not wish for any return value, you must specify the void keyword, i. e. void bob() • Alternatively, you could specify: int bob(){ … do something … return 0; } //Successful completion

Libraries • Many programming tasks are routinely performed, e. g. math functions, I/O, graphics,

Libraries • Many programming tasks are routinely performed, e. g. math functions, I/O, graphics, memory management etc. • C++ has a relatively small set of keywords, and all other functions are provided in libraries. • Library functions are not part of C++, but all compilers provide certain library functions. • Programmers can also create their own libraries, or use those provided by third parties (e. g. Open. GL).

Header Files • Libraries are usually not provided in source form, but in compiled

Header Files • Libraries are usually not provided in source form, but in compiled form • When you build (or link) your program, these functions are combined with your compiled code, to produce the final executable file. • Most library functions will need some definitions contained in particular files which are supplied with the system, called header files. • These files must then be included in your program, using the pre-processor directive #include

The pre-processor • C++ source code is compiled (by a compiler), producing object code.

The pre-processor • C++ source code is compiled (by a compiler), producing object code. • Then all the object code for each separate part of the program, include library functions, is linked together to produce the final executable code. • C++ has a pre-processor which processes the code before compilation. • The line #include <iostream> is a directive to the preprocessor to replace this line by the contents of the file iostream.

Comments • Full line comments are as follows: /* This is a full line

Comments • Full line comments are as follows: /* This is a full line comment */ • Comments on a line of code are as follows: cout <<”Hi”; //output Hi to screen • Several lines of comments: /* This program is the best * program I’ve ever written */ (Note: /**/ may not be nested)

#include <iostream> using namespace std; int main(){ int countdown=10; int number, answer=0; while (

#include <iostream> using namespace std; int main(){ int countdown=10; int number, answer=0; while ( countdown >0) { cout << “n Input a number: “; cin >> number; if (number < 0) cout << “n No negative numbers!”; else { answer+=number; countdown--; } //end if } //end while cout << “n The answer is: “ << answer; return 0; } //end main