2.5 处理类型 2.6 自定义数据结构

时间:2021-06-08 10:40:18

REF:

《C++ Primer (中文 第五版)》

《C++ Primer (英文 第五版 )》

2.5.1 类型别名

-传统方法使用关键字typedef

typedef double wages;//wages是double的同义词
typedef wages  base, *p;//base是double的同义词,p是double*的同义词

-新标准定义了新的方法,使用别名声明

using SI = Sales_item;//等号两端的名字是同义词

-指针、常量和类型别名

//下面的声明与句用到了类型pstring,它实际是类型char*的别名
typedef char *pstring;
const pstring cstr = 0;//cstr是指向char的常量指针

  

2.5.2 auto类型说明符

-编程时,常常需要把表达式的值赋值给变量,这就要求在声明变量的时候清楚知道表达式的类型。

-auto定义变量必须有初始值

auto item = val1 + val2;//item初始化为item1和item2相加的结果

-auto可以在一条语句中声明多个变量,但是这多个变量的类型需要是一致的。

-auto一般会忽略掉顶层const,如果希望auto推断出是const需要用const auto

 

2.5.2 节练习

练习 2.33

#include "stdafx.h"
#include <iostream>

int main()
{	
	int i = 0, &r = i;
	auto a = r;//a是一个整数

	const int ci = i, &cr = ci;
	auto b = ci;//b是整数(ci的顶层const特性被忽略了)
	auto c = cr;//c是整数
	auto d = &i;//d是一个整形指针(整数的地址就是指向整数的指针)
	auto e = &ci;//e是一个指向整数常量的指针(对常量对象取地址是一种底层const)

	const auto f = ci;

	auto &g = ci;//g是一个整形常量的引用,绑定到ci上
	const auto &j = 42;

	a = 42; b = 42; c = 42;
	//d = 42; //不能将 "int" 类型的值分配到 "const int *" 类型的实体
	//e = 42; //不能将 "int" 类型的值分配到 "const int *" 类型的实体
    //g = 42;//表达式必须是可修改的左值
	
    return 0;
}

 

 

2.5.3 decltype指示符

-选择并返回操作数的数据类型

-引用从来都是作为其所指对象的同义词出现的,只有用在decltype处是一个例外

const int ci = 0, &cj = ci;
decltype(ci) x = 0;// x 的类型是const int
decltype(ci) y = x;// y 的类型是const int&, y绑定到变量x上
//decltype(cj) z     //错误:z是一个引用必须初始化

-decltype和引用

~切记:decltype((variable))的结果永远是引用,而decltype(variable)结果只有当variable是一个引用时才会是一个引用。

 

2.5.3 练习

练习2.36 

程序:

#include "stdafx.h"
#include <iostream>

int main()
{	
	int a = 3, b = 4;
	decltype(a) c = a;
	decltype((b)) d = a;
	++c;
	++d;
	std::cout << "a = " << a << std::endl;
	std::cout << "b = " << b << std::endl;
	std::cout << "c = " << c << std::endl;
	std::cout << "d = " << d << std::endl;
    return 0;
}

执行结果:

2.5 处理类型 2.6 自定义数据结构

 练习2.37

程序:

#include "stdafx.h"
#include <iostream>

int main()
{	
	//赋值是会产生引用的一类典型表达式
	int a = 3, b = 4;
	decltype(a) c = a;
	decltype(a = b) d = a;
	std::cout << "a = " << a << std::endl;
	std::cout << "b = " << b << std::endl;
	std::cout << "c = " << c << std::endl;
	std::cout << "d = " << d << std::endl;
	++d;
	std::cout << "a = " << a << std::endl;
	std::cout << "b = " << b << std::endl;
	std::cout << "c = " << c << std::endl;
	std::cout << "d = " << d << std::endl;
    return 0;
}

执行结果:

 2.5 处理类型 2.6 自定义数据结构

 

2.6.1 定义Sales_data类型

-类内部定义的名字必须唯一,但是可以与类外部定义的名字重复

2.6.1节 练习

2.5 处理类型 2.6 自定义数据结构

 2.6.2  使用Sales_data类

程序p06701.cpp(Ndiy)

#include"stdafx.h"//yxq加的
#include <iostream>
#include <string>
#include "Sales_data.h"

int main()
{
	Sales_data data1, data2;

	// code to read into data1 and data2
	double price = 0;  // price per book, used to calculate total revenue

					   // read the first transactions: ISBN, number of books sold, price per book
	std::cin >> data1.bookNo >> data1.units_sold >> price;
	// calculate total revenue from price and units_sold
	data1.revenue = data1.units_sold * price;

	// read the second transaction
	std::cin >> data2.bookNo >> data2.units_sold >> price;
	data2.revenue = data2.units_sold * price;

	// code to check whether data1 and data2 have the same ISBN
	//        and if so print the sum of data1 and data2
	if (data1.bookNo == data2.bookNo) {
		unsigned totalCnt = data1.units_sold + data2.units_sold;
		double totalRevenue = data1.revenue + data2.revenue;

		// print: ISBN, total sold, total revenue, average price per book
		std::cout << data1.bookNo << " " << totalCnt
			<< " " << totalRevenue << " ";
		if (totalCnt != 0)
			std::cout << totalRevenue / totalCnt << std::endl;
		else
			std::cout << "(no sales)" << std::endl;

		return 0;  // indicate success
	}
	else {  // transactions weren't for the same ISBN
		std::cerr << "Data must refer to the same ISBN"
			<< std::endl;
		return -1; // indicate failure
	}
}

头文件Sales_data.h

#ifndef SALES_DATA_H
#define SALES_DATA_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

  

 

执行结果

注:先解决pdb 参考https://jingyan.baidu.com/article/cdddd41cb0ac6f53cb00e1bb.html

2.5 处理类型 2.6 自定义数据结构

2.6.3 编写自己的头文件

-为了确保各文件中类的定义是一致的,类通常定义在头文件中

-头文件通常包含那些只能被定义一次的实体,如类、const、constexpr

-确保头文件多次包含仍能够安全工作的常用技术是预处理器

运用#define、#ifdef、#endif

2.5 处理类型 2.6 自定义数据结构