< C++ Primer > learning notes Chapter 2 variables and basic types

Keywords: C++

Write in front

The second chapter is written in two parts. The main reason is that the concepts of const and user-defined data structure are very complex and difficult to understand. In particular, the user-defined data structure in the book is closely linked to the class in Chapter 7, so I want to write in more detail.

const qualifier

After the const object is created, the value will not change, and the const object must be initialized. The initial value can be any complex expression. Participating operations cannot change the content of the object. After defining a variable with const, all places where the variable appears will be replaced with this value during compilation. If the extern keyword is not added, the default is that const variable is compiled only in a separate file.

const reference
A reference to a constant (constant reference) cannot change the value of a constant object. A constant can be referenced only when it is defined as a constant reference. As mentioned earlier, the reference type must be consistent with the type it refers to. For example:

double i=100;
const int &a=i;

const int cannot reference a variable of type double

Concept discrimination
A constant reference can refer to a non constant variable of the same type, and an ordinary reference cannot refer to a constant variable. This means that const int & can refer to int variables, and an int & cannot refer to a const int variable.

const int i=100;
int j=100;
int &a=i;//Error, non constant reference cannot reference a constant object
const int &b=i;//correct
const int &b=j;//correct

Pointer and const
Constant pointer: the pointer is defined as a constant value and must be initialized. After initialization, the pointer will not change, that is, the address stored in the pointer. What remains unchanged is the pointer itself, not the value pointed to by the pointer. Pointers and references are similar in that the contents pointed to by pointers are similar to pointers, and a constant pointer is allowed to point to a non constant object

int *p;
*const int q;
int a=100;
const int b=100;
q=&a;//Yes, but you can't change the value of a by q
p=&b;//error

Discrimination between const int and int const
No difference, the same

Discrimination between const int and const int
Completely different. One is that the pointer does not change, which is a constant (int * const), and the other is that the content pointed to by the pointer does not change (int const *). It mainly depends on the position of * and will be described in detail in the bottom const and top const.

Top const and bottom const
Top level const: any object, such as the pointer itself, is a constant
Underlying const: the object referred to by the pointer or reference is a constant
Pointers can be either top-level or bottom-level.

constexpr and constant expressions
Constant expression: the result expression can be obtained during compilation.
The result obtained during the run is not a constant expression.

Constexpr: used to declare a variable so that the compiler can verify whether the variable is a constant expression. A variable declared as constexpr must be a constant and must be initialized with a constant expression.
The initialization of a constexpr pointer must be nullptr or 0, or exist in an object with a fixed address, and constexpr is only valid for the pointer, regardless of the object to which the pointer refers.

Processing type
Typedef: a synonym for a type
Alias declaration (using): to define the alias of the type

auto type specifier
The auto specifier allows the compiler to help us estimate the variable type through the initial value, so auto must have an initial value. Auto will ignore the top-level const, so const auto is used when the top-level variable is needed.

decltype type indicator
You want to infer the type from the expression, but you don't want to initialize the variable with the value of the expression. It is a little different from auto. If the expression used by decltype is a variable, the type of the variable is returned, including the top-level and reference. And if the expression of decltype is enclosed with double brackets, the type is always a reference.
decltype((i)) / / the result must be a reference
declitype(i) / / i is a reference only when i is a reference

Custom data structure

Define a Sales_data type

struct Sales_data{
   std::string bookNo;
   unsigned units_soled = 0;
   double revenue =0.0;
};   //No fewer titles

The class starts with struct, followed by the class name and class body. A seal must be written at the end of the class body. Generally, we do not put the definition of the object and the definition of the class together. In the class body, we define the members of the class. The class written above has only data members. The definition of data members is consistent with the definition of ordinary variables. We can add an initial value for the members in the class.

2.41 rewrite Sales_data, input ISBN, sales volume and retail price of books, output ISBN, total sales volume, total sales volume and average sales price (header file is not used)

#include<string>
#include<iostream>
 
struct Sales_data{
   std::string bookNo;
   unsigned units_soled = 0;
   double revenue =0.0;
};

int main(){
	Sales_data data1,data2;
	double price = 0;
	//Read in the first data 
	std::cin>>data1.bookNo>>data1.units_soled>>price;
	data1.revenue=data1.units_soled*price;
	//Read in the second data 
	std::cin>>data2.bookNo>>data2.units_soled>>price;
	data2.revenue=data2.units_soled*price;
	
	if(data1.bookNo==data2.bookNo){
		unsigned totalCnt=data1.units_soled+data2.units_soled;
		double totalRevenue=data1.revenue+data2.revenue;
		//Output ISBN total sales average price of total sales
		std::cout<<data1.bookNo<<" "<<totalCnt
		         <<" "<<totalRevenue<<" ";
		if(totalCnt!=0) 
		std::cout<<totalRevenue/totalCnt<<std::endl;
		else
		std::cout<<"no sales"<<std::endl;
		return 0;
	} 
	else{
		std::cerr<<"Data must refer to the same ISBN"<<std::endl;
		return -1;
	}
} 

Write your own header file
Class is usually defined in the header file, and the header file name is generally consistent with the class name. In order to ensure that the header file can work normally, some preprocessing commands are often used.
#Incloud: replace #incloud with the contents of the specified file
Header file protector
#define sets the name as a preprocessing variable
#ifdef is true if and only if the variable is defined
#ifndef is true if and only if the variable is undefined
#ENDIF when the variable is true, subsequent operations are performed until the #endif instruction is encountered
Preprocessing commands ignore scope rules

2.42 complete the bookstore program with header file

//Header file program
#ifndef SALES_DATA_H_INCLUDE
#define SALES_DATA_H_INCLUDE
#endif
#include<string>
#include<iostream>
struct Sales_data{
   std::string bookNo;
   unsigned units_soled = 0;
   double revenue =0.0;
};

//Source file section
#include<string>
#include<iostream>
#include"Sales_data.h" 
 


int main(){
	Sales_data data1,data2;
	double price = 0;
	//Read in the first data 
	std::cin>>data1.bookNo>>data1.units_soled>>price;
	data1.revenue=data1.units_soled*price;
	//Read in the second data 
	std::cin>>data2.bookNo>>data2.units_soled>>price;
	data2.revenue=data2.units_soled*price;
	
	if(data1.bookNo==data2.bookNo){
		unsigned totalCnt=data1.units_soled+data2.units_soled;
		double totalRevenue=data1.revenue+data2.revenue;
		//Output ISBN total sales average price of total sales
		std::cout<<data1.bookNo<<" "<<totalCnt
		         <<" "<<totalRevenue<<" ";
		if(totalCnt!=0) 
		std::cout<<totalRevenue/totalCnt<<std::endl;
		else
		std::cout<<"no sales"<<std::endl;
		return 0;
	} 
	else{
		std::cerr<<"Data must refer to the same ISBN"<<std::endl;
		return -1;
	}
}  

Header files and source files in C + +: how to write header files
c language custom header file dev c++

Posted by IWS on Tue, 30 Nov 2021 19:18:17 -0800