Skip to Content
C++程序设计:原理与实践(基础篇)(原书第2版)
book

C++程序设计:原理与实践(基础篇)(原书第2版)

by 本贾尼 斯特劳斯特鲁普
August 2021
Intermediate to advanced
450 pages
9h 36m
Chinese
Pearson
Content preview from C++程序设计:原理与实践(基础篇)(原书第2版)

简单练习

1.创建三个文件:my.h、my.cpp和use.cpp。头文件my.h包含:

源文件my.cpp包含my.h和std_lib_facilities.h,定义print_foo(),此函数用cout来打印foo的值,my.cpp中还应定义函数print(int i),用cout打印i的值。

源文件use.cpp包含my.h,定义主函数main(),主函数中将foo的值置为7,用print_foo()打印它,并用print()打印整型值99。注意use.cpp并不包含std_lib_facilities.h,因为它并不直接使用标准库中的功能。

编译并运行这些文件。在Windows平台上,你需要将use.cpp和my.cpp放在同一个项目中,并在use.cpp中使用{char cc;cin>>cc;}来看到输出结果。提示:你需要包含iostream来使用cin。

2.编写三个函数swap_v(int,int)、swap_r(int&,int&)和swap_cr(const int&,const int&)。每个函数有如下函数体:

其中a和b是参数名。

尝试用如下代码调用这三个函数:

哪个函数和调用能编译通过?为什么?对每个编译通过的swap调用,在调用过后打印参数的值,检查参数值是否真正被交换了。如果你不理解得到的结果,查阅8.6节。 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

C++语言导学(原书第2版)

C++语言导学(原书第2版)

本贾尼 斯特劳斯特鲁普
管理Kubernetes

管理Kubernetes

Brendan Burns, Craig Tracey
Python编程入门与实战

Python编程入门与实战

Posts & Telecom Press, Fabrizio Romano

Publisher Resources

ISBN: 9787111562252