Introduction to C++ Programming
I. In-Class Exercises
Programming Exercises
II. Knowledge Summary
✨ C++ File Types
C++ files are generally divided into two categories: .h/.hpp files and .cpp files.
- .h/.hpp files: Also known as header files, they provide information needed by the compiler to correctly process the program during compilation. There is no essential difference between
.hand.hppfiles, but some developers prefer to use.hppto indicate that the file contains C++ code features. - .cpp files: Also known as source files, they are mainly used to implement the content declared in header files. In competitive programming, our code files are stored in
.cppformat.
✨ Including Header Files
Header files contain function declarations and definitions needed for program execution. When writing C++ programs, we need to include required header files using the #include directive.
There are two ways to include header files:
- Angle brackets
< >: Used to include system or standard library header files; the compiler searches in system directories - Double quotes
" ": Used to include custom or third-party header files; the compiler first searches in the current directory
The commonly used universal header file <bits/stdc++.h> in competitive programming includes almost all standard library headers, saving you the trouble of including them individually.
#include <iostream> //System header file
#include <bits/stdc++.h> //Universal header (commonly used in competitions)
#include "CTurtle.hpp" //Third-party header (using double quotes)✨ Namespaces
What is a Namespace
A namespace is a mechanism provided by C++ to organize code and avoid naming conflicts. All content in the C++ standard library (such as cout, endl, cin, etc.) is defined in the std namespace and must be declared before use.
Two Ways to Reference
Method 1: Global Declaration (using namespace)
Use using namespace at the beginning of the program to import the entire namespace, after which you can directly use all its contents. This method is recommended for competitive programming.
using namespace std;
cout << "Hello" << endl; // Can directly use cout and endlMethod 2: Scope Resolution Operator (::)
Specify using namespace_name::member_name each time, suitable for large projects to avoid naming conflicts.
std::cout << "Hello" << std::endl; // Must add std:: prefix each time✨ C++ Code Structure
The basic structure of a C++ program includes the following main parts:
- Preprocessor directives: Currently, the only preprocessor directive we've learned is including header files
- Namespace: Declares the namespace used in the program
- Main function (main): The entry point of the program; all code starts executing from here
1#include <iostream> //Header file
2using namespace std; //Namespace
3
4//Main program entry
5int main() {
6 //Program code
7 cout << "Hello, world!" << endl;
8
9 //End of main program
10 return 0;
11}✨ Comments
C++ programs can use comments to explain code; comment content will not be executed by the compiler:
- Single-line comment: Starts with
//; everything from//to the end of the line is a comment - Multi-line comment: Enclosed by
/* */; can span multiple lines
1// This is a single-line comment
2
3/*
4 * This is a multi-line comment
5 * It can span many lines
6 */✨ Output Methods
There are two common output methods in C++: cout (C++ style) and printf (C style).
cout: Uses the insertion operator<<to output content; can useendlor\nfor newlinesprintf: Uses formatted strings for output; can only use\nfor newlines
cout << "Hello, world!" << endl; //C++ output, using endl for newline
cout << "Hello, world!\n"; //C++ output, using \n for newline
printf("Hello, world!\n"); //C output, using \n for newline✨ Sequential Flowchart
A flowchart is a graphical tool used to represent the steps of a program or process, typically composed of various symbols and arrows to describe the flow relationships between steps.
In a sequential flowchart:
- Rounded rectangles/ovals represent start or end
- Rectangles represent processes or steps
- Parallelograms represent input or output
- Arrows represent flow direction
Below is a simple sequential flowchart example showing the basic execution order from start to end:
III. Homework
Programming Exercises
- Hello World Advanced: L1043