c++primer读书笔记(3)

第七章总结:
我觉得前面的有些东西与c语言方面重复较多,因此不再此重复总结。
主要总结一下指针和const的注意点。
1、int num = 5;
const int * p = & num;与int * const p = & num;的区别
const int * p 中 * p指向的是const int ->num =5;不能通过 * p来修改,* p是const属性,但是可以改变指针的指向。
int * const p = & num中p是const,因此不能改变指针的指向,但是可以改变 * P。

2、将指针参数声明为指向常量数据的指针有两个理由:
(1)避免由于无意间修改数据而导致的编程错误
(2)使用const使得函数能够处理const和非const实参,否则只能接受非const数据。
(3)数组元素是基本类型,但如果它们是指针或者指向指针的指针,则不能使用const.

3、将字符串传给函数,则表示字符串的三种方式:
(1)char数组
(2)用引号括起的字符串常量。
(3)被设置为字符串地址的char指针。

4、函数中可以传结构或结构的地址。

5、c++提供的string类,用于表示字符串。函数可以接受string队形作为参数以及将string对象作为返回值。stringl类的方法size()可以判断其存储的字符串的长度。

6、c++函数名与函数的地址作用相同。通过将函数指针作为形参,可以传递要调用的函数的名称。

//传结构体的地址的程序
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
/*
struct polar
{
    double distance;
    double angle;
};

struct rect
{
    double x;
    double y;
};

void rect_to_polar(const rect *pxy, polar * pda);
void show_polar(const polar * pda);

int main()
{
    rect rplace;
    polar pplace;

    cout << "Enter the x and y values:";
    while (cin >> rplace.x >> rplace.y)
    {
        rect_to_polar(&rplace, &pplace);
        show_polar(&pplace);
        cout << "Next two numbers(q to quit):";

    }
    cout << "Done.\n";
    return 0;

}

void show_polar(const polar * pda)
{
    const double Rad_to_deg = 57.2957751;

    cout << "distance =" << pda->distance;
    cout << ",angle = " << pda->angle * Rad_to_deg;
    cout << "degrees\n";
}


void rect_to_polar(const rect * pxy, polar * pda)
{
    pda->distance = sqrt(pxy->x * pxy->x + pxy->y * pxy->y);

    pda->angle = atan2(pxy->y, pxy->x);

}

*/
/*
//传string类的程序示例
const int SIZE = 5;

void display(const string sa[], int n);
int main()
{
    string list[SIZE];
    cout << "Enter your" << SIZE << "favorite astronomicak sights:\n";

    for (int i = 0; i < SIZE; i++)
    {
        cout << i + 1 << ":";
        getline(cin, list[i]);
    }

    cout << "Your list:\n";
    display(list, SIZE);

    return 0;
}

void display(const string sa[], int n)
{
    for (int i = 0; i < n; i++)
    {
        cout << i + 1 << ":" << sa[i] << endl;
    }
}

*/
/*
//关于array类与函数的示例
#include <array>

const int Seasons = 4;

const array<string, Seasons>Snames = { "Spring","Summer","Fail","Winter" };

void fill(array<double, Seasons> * pa);

void show(array<double, Seasons>da);

int main()
{
    array<double, Seasons>expenses;
    fill(&expenses);
    show(expenses);

    return 0;
}

void fill(array<double, Seasons> * pa)
{
    for (int i = 0; i < Seasons; i++)
    {
        cout << "Enter " << Snames[i] << "expemses:";
        cin >> (*pa)[i];
    }
}

void show(array<double, Seasons> da)
{
    double total = 0.0;

    cout << "\nEXPENSES\n";
    for (int i = 0; i < Seasons; i++)
    {
        cout << Snames[i] << ": $" << da[i] << endl;
        total += da[i];

    }
    cout << "Total Expenses: $" << total << endl;
}

*/

int main()
{


    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值