ComputerProgramming3. The Nuts and Bolts of C++ElementsofaC++programAmainfunctionAprogramconsistsofmanyfunctionsItisexpectedtoreturnanintegervalueInputsandoutputsStandardoutputstream(std::cout)Standardinputstream (std::cin)Insertanew-linecharacter(std::endl)Standard libraryItis acollection of classesandfunctions,suchas iostream.Datatype:characterstringsandthenew-linecharacterOperators:<<isaninsertionoperator.Statements
3. The Nuts and Bolts of C++ Computer Programming Elements of a C++ program ⚫ A main() function − A program consists of many functions. − It is expected to return an integer value. ⚫ Inputs and outputs − Standard output stream (std::cout) − Standard input stream (std::cin) − Insert a new-line character (std::endl) ⚫ Standard library − It is a collection of classes and functions, such as iostream. ⚫ Data type: character strings and the new-line character ⚫ Operators: << is an insertion operator. ⚫ Statements
ComputerProgramming3.The Nuts and Bolts of C++PreprocessorandProgramCodespreprocessor#include <iostream>int main()fThe actualstd::cout << "Hello World!"program<< std::endlireturn O;Preprocessor:Runbeforecompiling.Actualprogram:Instructthe compileronhowtoEveryC++programmusthavecompiletheprogramthemain()functionWill not generate machine codesItisthebeginningpointofeveryC++programStartwith a pound (#) symbol
3. The Nuts and Bolts of C++ Computer Programming #include <iostream> int main() { std::cout << "Hello World!" << std::endl; return 0; } preprocessor The actual program • Preprocessor: • Instruct the compiler on how to compile the program • Will not generate machine codes • Start with a pound (#) symbol • Actual program: • Every C++ program must have the main() function • It is the beginning point of every C++ program Preprocessor and Program Codes Run before compiling
ComputerProgramming3.The Nuts and Bolts of C++Preprocessor#include <iostream>When compiling a file, we need to obtain the definitions ofsome terms in the program codes.These definitions are recorded in some headerfiles.These files are shipped with the compiler or other resources.#include tells the compiler where to find the header filesand insert this fileto thatlocation of the programe.g.#include <iostream>tells the compilerit shouldget the file iostream through the default path.e.g.#include "myfile"tells the compileritshould getthe filemyfile in the current folder
3. The Nuts and Bolts of C++ Computer Programming Preprocessor #include <iostream> • When compiling a file, we need to obtain the definitions of some terms in the program codes. • These definitions are recorded in some header files. • These files are shipped with the compiler or other resources. • #include tells the compiler where to find the header files and insert this file to that location of the program e.g. #include <iostream> tells the compiler it should get the file iostream through the default path. e.g. #include "myfile" tells the compiler it should get the file myfile in the current folder
ComputerProgramming3.The Nuts and Boltsof C++Think from the point ofProgramCodesview of the compilerThe basic element of a program is function.Afunctionis composed of:1.ReturnType2.Function nameint main()3.Input parameters4.Programcodesstd::cout << "Hello World!"enclosed by the<< std::endl;opening and closingreturn 0;bracesNote:The meaning of std::cout is checked in iostream
3. The Nuts and Bolts of C++ Computer Programming Program Codes int main() { std::cout << "Hello World!" << std::endl; return 0; } • The basic element of a program is function. • A function is composed of: 1. Return Type 2. Function name 3. Input parameters 4. Program codes enclosed by the opening and closing braces Note: The meaning of std::cout is checked in iostream Think from the point of view of the compiler
ComputerProgramming3.The Nutsand BoltsofC++The main () function is the beginning point of a programWhen executing a program, the operating system will firstcall themain () function ofthis program.If the abovemain ) function executes successfully,itshould return an integer O to the operating system.Callmain()Microsofmain()Windows0ReturnMeans everything fine on executingmain()as it is thelast statement
3. The Nuts and Bolts of C++ Computer Programming main() Call main() Return 0 Means everything fine on executing main()as it is the last statement. • The main() function is the beginning point of a program. • When executing a program, the operating system will first call the main() function of this program. • If the above main() function executes successfully, it should return an integer 0 to the operating system