- Program entry point: Every C++ program begins execution in the
main()function. The function returns an integer (0 for success) and can accept command-line arguments viaargcandargv. - Comments and whitespace: Use
//for single-line comments and/* */for multi-line blocks. Whitespace (spaces, tabs, newlines) is ignored by the compiler but improves readability. - Include directives: The
#include <iostream>preprocessor directive brings in the standard input/output library. Headers like<string>and<vector>provide additional functionality. - Standard namespace: The statement
using namespace std;allows direct access to standard library names likecoutandcinwithout prefixing withstd::. Avoid this in header files to prevent namespace pollution. - Output with cout: The
coutobject, combined with the insertion operator<<, prints text to the console. Example:cout << 'Hello, World!' << endl;. - Input with cin: Use
cinwith the extraction operator>>to read user input. For example:int age; cin >> age;stores the integer directly. - Newline vs endl:
endlinserts a newline and flushes the output buffer, while'\n'only inserts a newline. Prefer'\n'for performance unless you need immediate flush. - Basic structure: A minimal C++ program consists of includes, namespace declaration, and a
main()function body. This skeleton forms the foundation for every executable. - Variables and data types: C++ offers several built-in data types including int for integers, double for floating-point numbers, char for single characters, and bool for Boolean values. Variables must be declared with a type before use and can be initialized at declaration. For example, int age = 25; double price = 19.99;.
- Function definitions: Functions encapsulate reusable blocks of code. A function definition includes a return type, a name, a parameter list, and a body enclosed in braces. The main function is the entry point, but you can define other functions. Example: int square(int x) { return x * x; }.
- Conditional statements: If, else if, and else allow your program to make decisions based on conditions. Conditions are expressions that evaluate to true or false. Use logical operators like &&, ||, and ! to combine conditions. Example: if (score >= 90) grade = 'A'; else if (score >= 80) grade = 'B'; else grade = 'C';.
- Loop constructs: For loops, while loops, and do while loops repeat execution of a block of code. For loops are ideal when you know the number of iterations, while loops continue as long as a condition holds true. Example: for (int i = 0; i
#include <iostream>
using namespace std;
int main() {
cout << 'Enter your name: ';
string name;
cin >> name;
cout << 'Hello, ' << name << '!' << endl;
return 0;
}This program reads a name from standard input and prints a greeting. Note the use of cin and cout with the string type. The endl ensures the output appears immediately.
- C++ programs start in
main()and return an integer. - Use
#includeto import libraries andusing namespace std;for convenience. coutwith<<prints output;cinwith>>reads input.- Prefer
'\n'overendlfor better performance in most cases.