Download presentation
Presentation is loading. Please wait.
Published byLuke Black Modified over 9 years ago
1
C++ Basics Structure of a Program
2
C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before run the program Consist of #include statement class declaration (optional) variables and constants declaration (optional) int main() function and any other functions
3
// my first program in C++ #include using namespace std; int main ( ) { cout << "Hello World!"; return 0; } Output monitor: Hello World!
4
Comments // my first program in C++ Any descriptions or explanations of the program or code Will not be compiled or executed by computer Begin with // (double slashes) – a line comment Can be located any place in the program Block comment ( /* … */ ) – contains a multiple- line comment
5
Directives for Preprocessor #include #include – tell the compiler’s preprocessor to include library files before compiling the source code usually standard library file, or custom class file for standard library; “ ” for custom file No semi-colon (;) at the end of the line
6
Using Namespace using namespace std; Namespace – collection of name definitions std – the location where all C++ library elements are declared To use the library elements, using statement should be added Located below all #include statements Must put semi-colon at the end of the line
7
Beginning of a Program int main ( ) is the beginning of any C++ program Only one int main ( ) function is allowed in a source code Immediately followed by a pair of curly braces ( { } ) which holds the C++ instructions If one or more custom functions are declared, int main ( ) function is usually located as the last function C++ is case-sensitive if and IF are different
8
Standard Input and Output cout << "Hello World!"; cout – standard output (to console or monitor) Use operator << to separate each item to display cin – standard input (from keyboard) Use operator >> to a variable Each C++ statement must ends with a semi- colon (;)
9
Text Strings A series of alpha-numeric characters (including special characters) Contains no numeric value Must be enclosed by a pair of quotations – usually double, but in some case, single quotations Every blank space counts inside “ ” Double quotation without any character or blank space means ‘blank’ or ‘Null’ text string
10
Return Statement Return – cause the main function or any custom function to finish and return a value return 0; - main function’s return value is declared as int, so a numeric value 0 is returned (will not be used at all)
11
System Pause Statement System statement – controls a system functioning Enclosed by system ; Some software does not pause the result console window after the program execution Pause the screen until any key press on the keyboard
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.