c ++ primer fifth edition of the first chapter Chapter Two

Chapter One

input Output

#include<iostream>
int main(){
	int v1 = 0, v2 = 0;
	std::cin >> v1 >> v2;
	std::cout <<"v1+v2="<< v1 + v2;//拼接输出用“<<”
	return 0;
}

Output string constants

std::cout<<”wdadw”;
std::cout<<endl;//刷新缓冲区
//不是标准库文件用include”xx.h”

Control Flow

while (std::cin>>v1)//输入非int结束

Chapter two

Basic data types

Selection Criteria type of data
It can not be negative Unsigned
>int long long
Small integer signed char/unsigned char
Decimal Do not use double float

String is char [], there is the end of '\ 0'

variable

The definition of a multiple use: a statement.

The definition allocates storage space, the statement is not assigned.

extern int i;//使用外部变量

Identifier

Types of operating
The class name Capitalized
More than one word The first first letter lowercase, uppercase first letter of the second word
variable name lower case

Quote

The definition must be initialized

int i=1;
int &d=i;

pointer

Types of Must be initialized when defining It is an object / can be assigned, you copy, it is a pointer They can point to different variables
pointer x v v
Quote v x x

Pointer definitions

int i=2;
int *p=&i;
int *p2=p;

Also can be defined

int i=1;
int *p=nullptr;
p=&i;

Dereference

//地址
cout << p2 << endl;//地址
cout << p << endl;//和p2数值一样
//*p获得指向的变量值,可对其修改
cout << *p2 << endl;//2
cout << *p << endl;//2
*p=1;//修改变量i值=1
cout << *p2 << endl;//1
cout << *p << endl;//1

Null pointer

int *p= nullptr;//nullptr是预处理变量,由编译前的预处理器替换。
int *p= 0;//也可以

void * Pointer

int i=1;  
void *p=nullptr; 
p=&i;  
cout<<p<<endl;
cout<<*p<<endl;//报错,表达式必须是指向完整对象类型的指针

Pointer Pointer

int i=0;
int *p=&i;
int **p2=&p;//指针的指针

A pointer reference

int i=0;
int *p=&i;
int *&a=p;//指向指针的引用

external const

Different files can be shared

In test.cpp

#include <iostream>
 extern const int i=1;

In main.cpp

extern const int i;
cout<<i<<endl;//1

Const reference

Read-only

int i=1;
const int &i2=i;
//i2=2;//error
const int i=1;
int &i2=i;//error,要定义成常量引用

Constant pointer (read only, point Variable)

const double pi=3.14;
//double *pa=&pi;//指向变量的指针
const double *pb=&pi;//指向常量的指针
double b=1.1;
pb=&b;//指向可变

Constant pointer (read, change point)

int a,c=0;
int *const pa=&a;//指针功能:指向不变,读写
*pa=1;
// pa=&c;//error
const int b=0;
const int *const pb=&b;//指针功能:指向不变,只读

constexpr and constant expressions

const int i=2;//是常量表达式
int a=3;//不是
const修饰过的数据之间的拷贝问题
“=”左边的数据不会导致“=“右边的数
据发生变化就可以拷贝。
#include <iostream>
using namespace std;
/*
*	constexpr指针的初始值,必须为nullptr,0,固定地址。
*	但是函数体内部的变量地址不固定,所以不能指向那些变量。
*	函数体外的变量地址不变。但是函数可以定义函数体外的一类变量,
*	它们地址固定,可以指向它们。如i,j
*/
int i=2;
int j=3;
int main()
{
/*	 常量表达式:编译期就可以计算出结果
*    constexpr 变量:跟const差不多,可以修饰函数,在编译时就能计算出结果。
*   声明constexpr用到的类型,为字面值类型,如算术类型,引用,指针。
*    但是自定义类,IO类,string都不是。   
*/
	constexpr int *p=&i;//顶层const,常量指针,读写,指向不变
	*p=3;
	p=&j;//error 不可改变指向
	constexpr const int *p2=&j;//常量指针+只读
	*p2=4;//error
    return 0;
}

Processing Type

Type alias

auto type specifier

decltype type indicator

Both methods type aliases

//method01:
typedef double wages;//wages等价于double
typedef wages base,p*;//p等价于double*
//method02:
using IN=int;

Pointer, and the constant type alias

//对于符合类型,如指针,
typedef char *pstring;
const pstring cstr=0;//不是简单的替换,而是一种修饰,const修饰pstring,cstr是常量指针
const pstring *ps;//ps是指针,指向一个对象(一个常量指针)

auto type specifier

//编译器确定类型,必须有初始值
auto item=va;
//一般会忽略顶层const(指向不变),保留底层const
const int ci=i;//顶层const
auto b=ci;//b为int类型
//如果希望变为顶层const,可以这样定义
const auto b2=i;//b2是const int

decltype type indicator

//只用类型不用值
decltype(f()) sum =x;//f()可以是变量或是表达式
int i=42,*p=&i,&r=i;
decltype(r+0) b;//r是int&,加0,b为int
decltype(*p) c=i;//c是int&
//双括号是引用
decltype((i));//int&
decltype(i);//int

Custom data structures

Sales_data type definitions

Use Sales_data class

Write your own header files

Class definitions Sales_data.h

#ifndef SALES_DATA_H//如果没有包含,就执行以下
#define SALES_DATA_H
#include "Version_test.h"
#include <string>
struct Sales_data {
	std::string bookNo;
#ifdef IN_CLASS_INITS
	unsigned units_sold = 0;
	double revenue = 0.0;
#else
	unsigned units_sold;  
	double revenue;
#endif
};
#endif

Sales_data.cpp

#include "Sales_data.h"
...
int main(){...}

Preprocessor

确保头文件多次被包含,还可以正常运行
Published 19 original articles · won praise 0 · Views 1252

Guess you like

Origin blog.csdn.net/qq_35459198/article/details/105316622