[C++] Learning Notes [1]

Variables and Constants

What a variable means: Name a specified amount of memory space to make it easier for us to manage and manipulate it

Syntax for variable creation: data type variable name = variable initial value;For example, int a=10;

Constant: Used to record unmodifiable data in a program

C++ defines constants in two ways:

1. #define macro constant

  • Usually above a file, representing a constant

2. Constants of const modification

  • const data type constant name = constant value
  • The const keyword is usually prefixed to the variable definition, modifying the variable to be constant and not modifiable

The exercises for the two points are as follows:

#include <iostream>
using namespace std;

//Macro Constant
#define DAY 7

/*
main A function is the entry to a program
 Each program must have only one
 There is one and only one
*/


int main()
{
	//const-modified variable
	const int month = 12;
	//month=14; //Error, Constant cannot be modified
	cout << "A total of one year:" << month << "Month" << endl;

	//Syntax for variable creation: data type variable name = variable initial value
	int a = 10;


	//How many days a week to print
	cout << "There are a total of:" << DAY << "day" << endl;

	//If the value of DAY is modified
	//DAY = 14; //Error, DAY is a macro constant and cannot be modified


	//Output variables to screen
	cout << "a=" << a << endl;

	//Line 7 shows a Hello World output to the screen
	cout << "Hello world" << endl;
	return 0;
}

2. Keyword and Identifier

Role: Keyword is a pre-reserved word (identifier) in C++.

  • Do not use keywords when defining variables or constants, otherwise the system will produce ambiguities and errors

The C++ keywords are as follows:

  • auto
  • bool
  • int
  • inline
  • sizeof
  • default
  • char
  • float
  • false
  • continue
  • .... Encountered later and described in detail

Identifier Naming Rules

  • Identifier cannot be a keyword
  • Identifiers can only consist of letters, numbers, and underscores
  • The first character must be a letter or underscore
  • Letters in identifiers are case sensitive
  • Suggestion: It's best to know your name by name

3. Data Types

C++ specifies that when you create a variable or constant, you must specify the appropriate data type, otherwise you cannot allocate memory to the variable

What data types mean: Allocate appropriate memory space to variables

(1) Integer type

Role: Integer variables represent data of integer type

There are several types of integer data that can be represented in C++, depending on the memory space used.

The exercise code is as follows:

#include <iostream>
using namespace std;

int main()
{
	//Short Integer-32768~32767
	short num1 = 10;
	short num11 = 32768;
	short num111 = 32769;

	//integer  
	int num2 = 10;

	//Long Integer
	long num3 = 10;

	//long long
	long long num4 = 10;

	cout << "num1:" << num1 << endl;
	//When the boundary is crossed, the output will continue from its lower bound, and the result will be clear.
	cout << "num11:" << num11 << endl;
	cout << "num111:" << num111 << endl;
	cout << "num2:" << num2 << endl;
	cout << "num3:" << num3 << endl;
	cout << "num4:" << num4 << endl;


	return 0;
}

The results are as follows:

(2) sizeof keywords

Role: Use the sizeof keyword to statistics the size of memory occupied by the type

Syntax: sizeof (data type/variable)

The exercise code is as follows:

#include <iostream>
using namespace std;


int main()
{
	//Place data type
	cout << "short:" << sizeof(short) << endl;
	cout << "int:" << sizeof(int) << endl;
	cout << "long:" << sizeof(long) << endl;
	cout << "long long:" << sizeof(long long) << endl;


	//Place variable
	int a = 10;
	cout << "a:" << sizeof(a) << endl;

	long long b = 10;
	cout << "b:" << sizeof(b) << endl;
	//Compare size: shrot<int<=long<=long long

	return 0;
}

The results are as follows:

(3) Real type [Floating point type]

Role: Used to represent decimals

Floating-point variables are divided into two types:

  • Single Precision: float
  • Double precision: double

The difference between them is that the range of significant numbers represented is different

The exercise code is as follows:

#include <iostream>
using namespace std;



int main()
{
	//Create Single Precision Variable
	float f1 = 3.14159267;//The compiler considers it a double precision type
	float f2 = 3.14159267f;//Clearly tells the compiler that f2 is a single-precision type variable


	//Create Double Variable
	double d1 = 3.14159267;

	cout << "f1:" << f1 << endl;
	cout << "f2:" << f2 << endl;
	cout << "d1:" << d1 << endl;


	//In the C++ compiler, 6-bit significant numbers are output by default
	//3.13 is a 3-digit significant number
	//3.1 is a 2-digit significant number


	//Calculate the memory space occupied by float s and double s
	cout << "flaot The amount of memory used is:" << sizeof(float) << endl;
	cout << "double The amount of memory used is:" << sizeof(double) << endl;


	//Scientific notation for decimals
	float f3 = 3e2;//3*10^2
	cout << "f3:" << f3 << endl;


	float f4 = 3e-2;//3*0.1^2
	cout << "f4:" << f4 << endl;
	return 0;
}

The results are as follows:

(4) Character type

Role: Character variables are used to display a single character

Syntax: char ch='a';

Note: Single quotation marks can only contain one character, not more than one

  • Character variables only take up one byte in C and C++.
  • Character variables do not store the character itself in memory, they store the corresponding ASCII encoding in the storage unit

The exercise code is as follows:

#include <iostream>
using namespace std;


int main()
{
	char ch = 'a';
	cout << "ch:" << ch << endl;
	cout << "char The size of memory occupied:" << sizeof(char) << endl;

	//View ASCII coded values for character variables
	//a-97
	//A-65
	cout << "a Of ASCII Coded value:" << (int)ch << endl;

	char cha = 'b';
	cout << "b Of ASCII Coded value:" << (int)cha << endl;


	//Common errors with character variables
	//char ch2 = "b";//Single quotation marks for character variables
	//Char CH3 ='abc'; //Only one character can be used within single quotation marks

	return 0;
}

The results are as follows:


ASCII consists of roughly two parts

  • ASCII Non-Print Control Characters: The numbers 0-31 on the ASCII table are assigned to the control characters to control peripherals such as printers
  • ASCII Print Characters: Numbers 32-126 are assigned to characters that can be found on the keyboard and appear when viewed as a printed document.

(5) Escape characters

Role: Used to represent some ASCII characters that cannot be displayed

Commonly used at this stage are:\n, \t, \

Escape CharacterMeaning
\aalert
\bBackspace, move current position to previous column
\nLine Break
\rEnter
\tHorizontal Tabulation
\Represents a backslash character\
\ ?Represents a question mark

The exercise code is as follows:

#include <iostream>
using namespace std;

int main()
{

	//  \n wraps lines
	cout << "hello world\n";

	//  Output\
	cout << "\\" << endl;

	//   \t: neat output data
	cout << "aaa\thelloworld" << endl;
	cout << "aaaa\thelloworld" << endl;
	cout << "aaaaa\thelloworld" << endl;
	cout << "aaaaaa\thelloworld" << endl;


	cout << "aaa helloworld" << endl;
	cout << "aaaa helloworld" << endl;
	cout << "aaaaa helloworld" << endl;
	cout << "aaaaaa helloworld" << endl;
	return 0;
}

The results are as follows:

(6) String type

Role: Used to represent a string of characters

Two Styles

  • C language style: char ch[] = "abcg";
  • C++ style: string ch="abcd";

The exercise code is as follows:

#include <iostream>
#include <string>
using namespace std;


int main()
{
	//C Language
	char str[] = "abcd helloworld";

	//C++, required header file #include <string>
	string str1 = "abcg helloworld";


	cout << str << endl;
	cout << str1 << endl;
	return 0;
}

The results are as follows:

(7) Boolean type bool

Role: Boolean data types represent true or false values

bool type value, true as long as non-zero value

bool type takes up only one byte of memory space

The bool type has only two values:

  • True: true [essentially 1]
  • False: false [0 in essence]

bool type takes up one byte size

The exercise code is as follows:

#include <iostream>
#include <string>
using namespace std;


int main()
![Insert a picture description here](https://img-blog.csdnimg.cn/a28504d83b1440908c364adba135fae7.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA5pe66LSi6KaB5Yqg5rK5,size_20,color_FFFFFF,t_70,g_se,x_16#pic_center)
{
	int flag1 = true;
	int flag2 = false;

	cout << flag1 << endl;//1
	cout << flag2 << endl;//0

	cout << "bool Type memory size:" << sizeof(bool) << endl;//1
	return 0;
}

The results are as follows:

(8) Data entry

Role: Used to get data from keyboard

Keyword: cin

Syntax: CIN >> x;

The exercise code is as follows:

#include <iostream>
#include <string>
using namespace std;

int main()
{
	int a = 0;
	cout << "Please give a To perform an assignment:" << endl;
	cin >> a;
	cout <<"Integer variable a The value is:" << a << endl;
	return 0;
}

✨✨✨
Start from scratch! Keep it up!

Tags: C C++

Posted on Fri, 10 Sep 2021 14:29:40 -0400 by gskaruz