c++primer读书笔记(4)

第八章的总结:
1、c++扩展了c语言的函数功能,通过inline关键字用于函数定义,并在首次调用该函数前提供其函数定义可以使得c++编译器将该函数视为内联函数。编译器不是让程序跳到独立的代码段,以执行函数,而是用相应的代码替换函数调用。

2、引用变量是一种伪指针变量,它允许为变量创建一个别名。引用变量主要是为了处理结构和类函数调用提供参数值。

3、函数重载的要求是函数形参的个数、类型、顺序的不同,
而函数的返回类型不能作为函数重载的条件。

4、函数模板自动完成重载函数的过程,只需要使用泛型和具体算法来定义函数。编译器为程序中使用的特定参数类型生成正确的函数定义。

代码练习

代码1
#include <iostream>
#include <string>
using namespace std;
/*
int main()
{
    int rats = 101;
    int & rodents = rats;
    cout << "rats = " << rats;
    cout << ",rodents = " << rodents << endl;

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

    int bunnies = 50;
    rodents = bunnies;

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

    cout << "bunnises address = " << &bunnies;
    cout << ",rodents address = " << &rodents << endl;

    return 0;
}


代码2
//swap
void swapr(int & a, int & b);
void swapp(int * p, int * q);
void swapv(int a, int b);

int main()
{
    int wallet1 = 300;
    int wallet2 = 350;

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

    cout << "Using reference to swap contents:\n";
    swapr(wallet1, wallet2);
    cout << "wallet1 = $ " << wallet1;
    cout << ",wallet2 = $" << wallet2 << endl;

    cout << "Using pointers to swap conters again:\n";
    swapp(&wallet1, &wallet2);
    cout << "wallet1 = $ " << wallet1;
    cout << ",wallet2 = $" << wallet2 << endl;

    cout << "Tryinf to use passing by value:\n";
    swapv(wallet1, wallet2);
    cout << "wallet1 = $" << wallet1;
    cout << ",wallet2 = $" << wallet2 << endl;

    return 0;
}

void swapr(int & a, int & b)
{
    a = a ^ b;
    b = a ^ b;
    a = a ^ b;
}
void swapp(int *p, int *q)
{
    int temp;
    temp = *p;
    *p = *q;
    *q = temp;
}

void swapv(int a, int b)
{
    int temp;
    temp = a;
    a = b;
    b = temp;
}
*/


代码3
string version1(const string &s1, const string &s2);
const string & version2(string &s1, const string &s2);
const string & version3(string &s1, const string &s2);
int main()
{
    string input;
    string copy;
    string result;

    cout << "Enter a string:";
    getline(cin, input);
    copy = input;
    cout << "Your stirng as enterd:" << input << endl;
    result = version1(input, "***");
    cout << "Your stting enhance: " << result << endl;
    cout << "Your original string: " << input << endl;

    result = version2(input, "###");
    cout << "Your string enhanced: " << result << endl;
    cout << "Your original string: " << input << endl;

    cout << "Resetting original string:\n";
    input = copy;
    result = version3(input, "@@@");

    cout << "Your string enhanced: " << result << endl;
    cout << "Your original string: " << input << endl;

    return 0;
}

string version1(const string & s1, const string & s2)
{
    string temp;
    temp = s2 + s1 + s2;
    return temp;

}
const string & version2(string & s1, const string & s2)
{
    s1 = s2 + s1 + s2;
    return s1;
}
const string & version3(string & s1, const string & s2)//不能返回局部的引用
{
    string temp;
    temp = s2 + s1 + s2;
    return temp;
}

1、引用与指针的区别:引用是对数据的直接操作,而指针是通过地址间接操作。引用变量和原本变量的地址是相同的。

2、不能返回局部的引用或指针。

3、const与引用的结合,在函数中如果实参不需改变,就将函数形参用const修饰。

函数模板的总结:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值