Or helloworld
#include<iostream> //Introduction of header file in c + + language
using namespace std; //Namespace cout functions depend on this namespace
int main()
{
cout << " WTF " << endl; //Fill in our opening Hello here.
system("pause");//System functions to keep black windows from flashing by
return 0;
}
- include iostream
The C++ language defines some header files that contain necessary or useful information in the program. The header file is included in the above program. - using namespace std // namespace
Tell the compiler to use std namespaces. Namespace is a relatively new concept in C++. - main()
int main() is the main function from which the program executes - Court <"WTF"< < endl; // Output WTF can replace the endl in the above code with "\ n".
The message "Hello World" will be displayed on the screen.
Court is the printf function of c++, "WTF" is a string,""stream operator, endl:" n" means a string containing a carriage return character. std::endl is a stream operator, and the effect of output is similar to that of output "\ n", but it may be slightly different. std::endl outputs a newline character and refreshes the buffer immediately.
C++ Data Type
- Boolean bool
- Character char
- Integer int
- Floating-point float
- double Floating Point Type
- Untyped void
- Wide character wchar_t
References to specific bytes and modifiers: http://www.runoob.com/cplusplus/cpp-data-types.html
Printing bytes with sizeof function
This example uses endl, which inserts a newline character after each line, and the `operator'is used to pass multiple values to the screen.
#include<iostream>
#include<string>
#include <limits>
using namespace std;
int main()
{
cout << "type: \t\t" << "************size**************"<< endl;
cout << "bool: \t\t" << "Number of bytes occupied:" << sizeof(bool);
cout << "\t Maximum:" << (numeric_limits<bool>::max)();
cout << "\t\t Minimum:" << (numeric_limits<bool>::min)() << endl;
cout << "char: \t\t" << "Number of bytes occupied:" << sizeof(char);
cout << "\t Maximum:" << (numeric_limits<char>::max)();
cout << "\t\t Minimum:" << (numeric_limits<char>::min)() << endl;
cout << "signed char: \t" << "Number of bytes occupied:" << sizeof(signed char);
cout << "\t Maximum:" << (numeric_limits<signed char>::max)();
cout << "\t\t Minimum:" << (numeric_limits<signed char>::min)() << endl;
cout << "unsigned char: \t" << "Number of bytes occupied:" << sizeof(unsigned char);
cout << "\t Maximum:" << (numeric_limits<unsigned char>::max)();
cout << "\t\t Minimum:" << (numeric_limits<unsigned char>::min)() << endl;
cout << "wchar_t: \t" << "Number of bytes occupied:" << sizeof(wchar_t);
cout << "\t Maximum:" << (numeric_limits<wchar_t>::max)();
cout << "\t\t Minimum:" << (numeric_limits<wchar_t>::min)() << endl;
cout << "short: \t\t" << "Number of bytes occupied:" << sizeof(short);
cout << "\t Maximum:" << (numeric_limits<short>::max)();
cout << "\t\t Minimum:" << (numeric_limits<short>::min)() << endl;
cout << "int: \t\t" << "Number of bytes occupied:" << sizeof(int);
cout << "\t Maximum:" << (numeric_limits<int>::max)();
cout << "\t Minimum:" << (numeric_limits<int>::min)() << endl;
cout << "unsigned: \t" << "Number of bytes occupied:" << sizeof(unsigned);
cout << "\t Maximum:" << (numeric_limits<unsigned>::max)();
cout << "\t Minimum:" << (numeric_limits<unsigned>::min)() << endl;
cout << "long: \t\t" << "Number of bytes occupied:" << sizeof(long);
cout << "\t Maximum:" << (numeric_limits<long>::max)();
cout << "\t Minimum:" << (numeric_limits<long>::min)() << endl;
cout << "unsigned long: \t" << "Number of bytes occupied:" << sizeof(unsigned long);
cout << "\t Maximum:" << (numeric_limits<unsigned long>::max)();
cout << "\t Minimum:" << (numeric_limits<unsigned long>::min)() << endl;
cout << "double: \t" << "Number of bytes occupied:" << sizeof(double);
cout << "\t Maximum:" << (numeric_limits<double>::max)();
cout << "\t Minimum:" << (numeric_limits<double>::min)() << endl;
cout << "long double: \t" << "Number of bytes occupied:" << sizeof(long double);
cout << "\t Maximum:" << (numeric_limits<long double>::max)();
cout << "\t Minimum:" << (numeric_limits<long double>::min)() << endl;
cout << "float: \t\t" << "Number of bytes occupied:" << sizeof(float);
cout << "\t Maximum:" << (numeric_limits<float>::max)();
cout << "\t Minimum:" << (numeric_limits<float>::min)() << endl;
cout << "size_t: \t" << "Number of bytes occupied:" << sizeof(size_t);
cout << "\t Maximum:" << (numeric_limits<size_t>::max)();
cout << "\t Minimum:" << (numeric_limits<size_t>::min)() << endl;
cout << "string: \t" << "Number of bytes occupied:" << sizeof(string) << endl;
// < < "\ t Max:"< < (numeric_limits < string >: max) () < < " t minimum:"< ((numeric_limits < string >: min) () < < endl);
cout << "type: \t\t" << "************size**************"<< endl;
getchar();
return 0;
}
Output:
type: ************size**************
bool: Number of bytes occupied:1 Maximum:1 Minimum:0
char: Number of bytes occupied:1 Maximum: Minimum:?
signed char: Number of bytes occupied:1 Maximum: Minimum:?
unsigned char: Number of bytes occupied:1 Maximum:? Minimum:
wchar_t: Number of bytes occupied:4 Maximum:2147483647 Minimum:-2147483648
short: Number of bytes occupied:2 Maximum:32767 Minimum:-32768
int: Number of bytes occupied:4 Maximum:2147483647 Minimum:-2147483648
unsigned: Number of bytes occupied:4 Maximum:4294967295 Minimum:0
long: Number of bytes occupied:8 Maximum:9223372036854775807 Minimum:-9223372036854775808
unsigned long: Number of bytes occupied:8 Maximum:18446744073709551615 Minimum:0
double: Number of bytes occupied:8 Maximum:1.79769e+308 Minimum:2.22507e-308
long double: Number of bytes occupied:16 Maximum:1.18973e+4932 Minimum:3.3621e-4932
float: Number of bytes occupied:4 Maximum:3.40282e+38 Minimum:1.17549e-38
size_t: Number of bytes occupied:8 Maximum:18446744073709551615 Minimum:0
string: Number of bytes occupied:24
type: ************size**************
typedef statement
typedef int feet;
The following statement tells the compiler that feet is another name for int: the following statement is perfectly legitimate, creating an integer variable distance:
#include <iostream>
using namespace std;//Court functions depend on this std namespace
int main() {
typedef int feet;
feet distance = 666;
cout << "distance == " << distance;
getchar();
return 0;
}
output
distance == 666
Enumeration type
- Enumeration type
- It is a derived data type in C++ and is a set of user-defined enumeration constants.
- If a variable has only a few possible values, it can be defined as an enumeration type.
- The so-called "enumeration" refers to enumerating the values of variables one by one, and the values of variables can only be in the range of enumerated values. [Refer to JAVA Enumeration]
- To create enumerations, you need to use the keyword enum.
#include <iostream>
using namespace std;
void main() {
//Writing is similar to java
//This enumeration has an int pointer. You don't set it to start at 0 by default and increase it by 1.
//If you set it to him, it means that the value is actually increased by 1.
enum color { red, green, blue } ;
color c;
c = green;
cout << "WTF : c = " << c << "bule = " << blue << endl;
enum color2 { white, yellow = 5, black } test;
test = yellow;
cout << "WTF : test yellow = " << test << "black = " << black << endl;
system("pause");
}
output
WTF : c = 1bule = 2 WTF : test yellow = 5black = 6 Press any key to continue....
- Writing is similar to java
- This enumeration has an int pointer. You don't set it to start at 0 by default and increase it by 1.
- If you set it to him, it means that the value is actually increased by 1.
- Enumeration cannot be the same when defining. Note that
C++ variable constants (like Java)
local variable
#include <iostream>
using namespace std;
int main ()
{
// Local variable declaration
int a, b;
int c;
// Actual initialization
a = 10;
b = 20;
c = a + b;
cout << c;
return 0;
}
global variable
g is a global variable. Local variables can have the same name as global variables, but in this way, the values of global variables are overwritten. This is the same as java.
#include <iostream>
using namespace std;
// Global variable declaration
int g;
int main ()
{
// Local variable declaration
int a, b;
// Actual initialization
a = 10;
b = 20;
g = a + b;
cout << g;
return 0;
}
Initial value of variable
int => 0
char => '\0'
float => 0
double => 0
pointer => NULL
constant
Use # define
#include <iostream>
using namespace std;
// In C++, there are two simple ways to define constants
// Use the # define preprocessor.
// Use the const keyword.
#define LENGTH 10
#define WIDTH 5
#define NEWLINE '\n'
#define HELLO "HELLO"
void main() {
int area;
area = LENGTH * WIDTH;
cout << area;
cout << NEWLINE;
cout << HELLO;
getchar();
}
Output result
Why is the output like this? Line breaks don't work. 50 HELLO I hope so. 50 HELLO But the focus of this article is not here, there are doubts. Asked in the group, seemingly because: in fact, has changed the line, but my output string should have been in the first line. TODO first, if you know the principle of friends, please don't hesitate to give advice.
Use Const keyword
#include <iostream>
using namespace std;
int main()
{
const int LENGTH = 10;
const int WIDTH = 5;
const char NEWLINE = '\n';
const char* Hello = "Hehe";
int area;
area = LENGTH * WIDTH;
cout << area;
cout << NEWLINE;
cout << Hello;
getchar();
return 0;
}
Demo
Demo: https://github.com/zj614android/c-c-/tree/master/Lis2
Recommendation: c++ introductory video: https://www.imooc.com/u/1349694/courses?sort=publish
Recommendation: Newbie Course: http://www.runoob.com/cplusplus/cpp-tutorial.html