Commonly used data types in C++ include int, char, float, and double, which represent integer, character, single-precision, and double-precision types respectively. Their extended types, including their extended types, are consistent with the usage in C language. For example, the following C++ program:
#include<iostream>usingnamespacestd;intmain(){intnum;//Student number charsex;//Gender doublescore1;//Subject one score doublescore2;//Subject two score doublescore3;//Subject three score cout<<Pleaseinputstudent'sID , 'M'or'W', score1, score2andscore3<<endl;cin>>num>>sex>>score1>>score2>>score3;cout<<ID:<<num<<<sex<<Totalscoreis: <<score1+score2+score3<<endl;//Output the student information and total score return0;}
The running results are as follows:
The above demonstrates how to use int, char, and doule types.
Similar to the usage in C language, it is declared first and then used. Different from the C language, when using cin and cout to input and output data, you can use them without manually controlling the data type. Please pay attention to the difference and experiment on the computer.
In addition to the same basic variables as the C language, C++ also has an additional type called the Boolean type, which will be introduced separately below.
The Boolean type is a logical value. The keyword type is bool . There are only two defined variables, true and false, which represent the two values of true and false respectively. They generally only occupy one byte in the memory. The emergence of the bool type in C++ is perfect for values that want to express true and false logical results. It fills the shortcomings of the C language that can only be replaced by int. Please see the example program below:
#include<iostream>usingnamespacestd;intmain(){inta=9;intb=10;boolr;//Define Boolean type variable rr=a>b;cout<<r<<endl;cout<<boolsizeis:<<sizeof( r)<<endl;return0;}
Normally you will get the following results:
Please understand the program and experiment on your own computer to master the usage of bool type.