代码解析

如有失误之处,还恳请指教!!!(缺少例题8.5,打开8.5的代码时,发现自己写的全不见了#######)
8.1

// inline.cpp -- using an inline function
#include <iostream>

// an inline function definition
//内联函数定义。使用内联函数有两种措施:1. 在函数声明前加上关键字inline
//2. 在函数定义前加上关键字inline
inline double square(double x) {
    return x * x; }
int main()
{
   
    using namespace std;
    double a, b;
    double c = 13.0;
    //调用内联函数,调用方式与调用普通函数无异。
    a = square(5.0);
    b = square(4.5 + 7.5);   // can pass expressions
    cout << "a = " << a << ", b = " << b << "\n";
    cout << "c = " << c;
    cout << ", c squared = " << square(c++) << "\n";
    cout << "Now c = " << c << "\n";
    // cin.get();
    return 0;  
}
//总结:使用内联函数时只需要在函数声明或定义时操作,函数调用时与普通函数无异
//内联函数的通常做法是:省略原型,将整个定义(即函数头和所有函数代码)放在本应提供原型的地方
//不适宜使用内联函数的情况:1.函数过大,2.递归函数

8.2

// firstref.cpp -- defining and using a reference
#include <iostream>
int main()
{
   
    using namespace std;
    int rats = 101;
    //创建rats的引用变量,引用变量名称是rodents
    int &rodents = rats; // rodents is a reference

    cout << "rats = " << rats;
    cout << ", rodents = " << rodents << endl;
    rodents++;
    cout << "rats = " << rats;
    cout << ", rodents = " << rodents << endl;

    // some implementations require type casting the following
    // addresses to type unsigned
    cout << "rats address = " << &rats;
    //在创建引用变量时&符号是类型标识符,而当获取引用变量的地址时&又是地址运算符
    //需要注意这点,&在不同的场景下的意义和作用
    cout << ", rodents address = " << &rodents << endl;
    // cin.get();
    return 0;
}

8.3

// secref.cpp -- defining and using a reference
#include <iostream>
int main()
{
   
    using namespace std;
    int rats = 101;
    //为rats创建引用变量,并给此引用变量命名为rodents
    int &rodents = rats; // rodents is a reference

    cout << "rats = " << rats;
    cout << ", rodents = " << rodents << endl;

    cout << "rats address = " << &rats;
    cout << ", rodents address = " << &rodents << endl;

    int bunnies = 50;
    //将bunnies的值赋给rodents,会将rodents和rats的值同时改变为50
    //引用变量和原变量使用同一块数据,也就是说可以通过一个变量的引用变量改变此变量
    rodents = bunnies; // can we change the reference?
    cout << "bunnies = " << bunnies;
    cout << ", rats = " << rats;
    cout << ", rodents = " << rodents << endl;
    //虽然rodents的值被改变了,但是rodents的内存地址没有被改变
    cout << "bunnies address = " << &bunnies;
    cout << ", rodents address = " << &rodents << endl;
    // cin.get();
    return 0;
}

//总结:
//1.    引用时已定义变量的别名,这句话的深意是:引用变量和原始变量本身共享同一块内存地址,
//      共享同一块内存地址上的数据。
//2.    引用和指针都可以操作原始变量的数据,在课本中我们了解到的引用变量与指针之间的差异:
//   1)必须在声明引用时将其初始化,而指针可以先声明再赋值。
//   2)引用变量本身的地址就是原始变量的地址,而指针本身会有其自己的地址,指针只是指向变量
//      变量的地址,因此其本身的地址和指向的变量的地址是两回事,但是引用变量与原始变量是一
//      回事。
//   3)引用只能通过初始化设置,也就是说,只有通过初始化设置才可以改变引用变量,但是指针如
//      果不是const指针的话,可以通过赋值来改变指针指向的地址,从而改变指针指向的值。
//3.    可以通过初始化声明来设置引用,但不能通过赋值来设置---即可以通过最初始化的方式来设
//      引用,但并不能通过赋值的的方式来设置引用或者更改引用本身。

8.4

// swaps.cpp -- swapping with references and with pointers
#include <iostream>
//函数原型:该函数无返回值,含有两个参数,这两个参数分别是引用变量
//说明在调用此函数时需要将原始变量传递给引用变量
void swapr(int &a, int &b); // a, b are aliases for ints
//函数原型:该函数无返回值,含有两个参数,这两个参数分别是指针变量
//说明调用此函数时应该传递变量的地址
void swapp(int *p, int *q); // p, q are addresses of ints
//函数原型:该函数无返回值,含有两个参数,这两个参数分别是int类型的
//基础类型变量,说明调用此函数时,主函数中被当作实际参数的值将被复制一份
//并且将复制的值赋值给形式参数。
void swapv(int a, int b);   // a, b are new variables
int main()
{
   
    using namespace std;
    int wallet1 = 300;
    int wallet2 = 350;

    cout << "wallet1 = $" << wallet1;
    cout << " wallet2 = $" << wallet2 << endl;

    cout << "Using references to swap contents:\n";
    //调用形参为引用变量的函数,将原始变量作为参数传递
    swapr(wallet1, wallet2); // pass variables
    cout << "wallet1 = $" << wallet1;
    cout << " wallet2 = $" << wallet2 << endl;

    cout << "Using pointers to swap contents again:\n";
    //调用形参为指针变量的函数,将原始变量的地址作为实际参数传递
    swapp(&wallet1, &wallet2); // pass addresses of variables
    cout << "wallet1 = $" << wallet1;
    cout << " wallet2 = $" << wallet2 << endl;

    cout << "Trying to use passing by value:\n";
    //调用形参是基础类型的函数,该函数将会得到原始变量值的拷贝
    swapv(wallet1, wallet2); // pass values of variables
    cout << "wallet1 = $" << wallet1;
    cout << " wallet2 = $" << wallet2 << endl;
    // cin.get();
    return 0;
}

void swapr(int &a, int &b) // use references
{
   
    int temp;

    temp = a; // use a, b for values of variables
    a = b;
    b = temp;
}

void swapp(int *p, int *q) // use pointers
{
   
    int temp;

    temp = *p; // use *p, *q for values of variables
    *p = *q;
    *q = temp;
}

void swapv(int a, int b) // try using values
{
   
    int temp;

    temp = a; // use a, b for values of variables
    a = b;
    b = temp;
}
//总结:变量在不同的函数块内有其作用范围,即使是同名的变量,在不同的场景下代表
//     的依然是不同的意义。为此,我们很难跨函数块操作自动变量。那么,如果我们
//     必须要在不同的函数块使用同一种数据该怎么办?C++给出的解决方案便是使用
//     引用变量和指针,通过引用变量和指针均可以操作原始数据,但是也需要明白指
//     针和引用变量之间的差异,才可以更好的使用这两个操作原始数据的方式。

8.6

//strc_ref.cpp -- using structure references
#include <iostream>
#include <string>
//结构声明
struct free_throws
{
   
    std::string name;
    int made;
    int attempts;
    float percent;
};
//函数原型:无返回值,含有一个自定义结构类型的引用常量作为参数
void display(const free_throws &ft);
//函数原型:无返回值,含有一个自定义结构类型的引用变量作为参数
void set_pc(free_throws &a
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值