one
1. A C++ program can be defined as a collection of objects that interact by calling each other's methods. Now let's briefly look at what are classes, objects, methods, immediate variables.
- Objects - Objects have state and behavior. For example: a dog's state - color, name, breed, behavior - shaking, barking, eating. Objects are instances of classes.
- Class - A class can be defined as a template/blueprint that describes the behavior/state of an object.
- Method - Basically, a method represents a behavior. A class can contain multiple methods. You can write logic, manipulate data, and perform all actions in methods.
- Instant Variables - Each object has its own unique instant variable. The state of the object is created from the values of these immediate variables.
2. C++ language structure:
#include <iostream>
using namespace std;
// main() 是程序开始执行的地方
int main()
{
cout << "Hello World"; // 输出 Hello World
return 0;
}
(1) The C++ language defines some header files that contain necessary or useful information in the program. In the above program, the header file <iostream> is included .
(2) The next line using namespace std; tells the compiler to use the std namespace. Namespaces are a relatively new concept in C++. Others will be introduced later.
two
C++ identifiers
A C++ identifier is a name used to identify a variable, function, class, module, or any other user-defined item. An identifier begins with the letters AZ or az or the underscore _, followed by zero or more letters, underscores, and numbers (0-9).
Punctuation characters such as @, &, and % are not allowed within C++ identifiers. C++ is a case-sensitive programming language. Therefore, in C++, Manpower and manpower are two different identifiers.
Several valid identifiers are listed below:
mohd zara abc move_name a_123
myname50 _temp j a23b9 retVal
C++ keywords
The following table lists reserved words in C++. These reserved words cannot be used as constant names, variable names, or other identifier names.
asm | else | new | this |
auto | enum | operator | throw |
bool | explicit | private | true |
break | export | protected | try |
case | external | public | typedef |
catch | false | register | typeid |
char | float | reinterpret_cast | typename |
class | for | return | union |
const | friend | short | unsigned |
const_cast | goto | signed | using |
continue | if | sizeof | virtual |
default | inline | static | void |
delete | int | static_cast | volatile |
do | long | struct | wchar_t |
double | mutable | switch | while |
dynamic_cast | namespace | template |
three
whitespace in C++
A line containing only whitespace, known as a blank line, may have a comment, which is completely ignored by the C++ compiler.
In C++, spaces are used to describe whitespace, tab, newline, and comments. Spaces separate parts of a statement, allowing the compiler to recognize where an element (such as an int) in the statement ends and the next element begins. Therefore, in the following statement:
int age;
Here, there must be at least one whitespace character (usually a whitespace) between int and age so that the compiler can distinguish them. On the other hand, in the following statement:
fruit = apples + oranges ; // get the total number of fruits
The space character between fruit and =, or = and apples is not required, but you can add as many spaces as necessary to improve readability.
Most of the content of this article comes from the original URL of the C++ tutorial of the rookie tutorial: www.runoob.com/cplusplus/cpp-basic-syntax.html