博客
关于我
[C++]初识
阅读量:522 次
发布时间:2019-03-06

本文共 3174 字,大约阅读时间需要 10 分钟。

说明

关于C++的内容,都是在已经了解了C语言的基础之上总结的,所以有不少C++的基础,因为也是C语言的基础,所以不会特意说明。

输入输出

我们从一个简单的例子开始:

#include int main(){    int v1 = 0, v2 = 0;    std::cout << "Enter two numbers:" << std::endl;    std::cin >> v1 >> v2;    std::cout << "The sum of " << v1 << " and " << v2 << " is " << v1 + v2 << std::endl;    return 0;}

在这个程序中,cout是标准输出,cin是标准输入,cerr是标准错误,clog是标准日志记录。对应的头文件在标准库中:

namespace std {    // ... (注:以下为标准流对象的声明,具体内容可参考标准库文档)    extern istream cin;    extern ostream cout;    extern ostream cerr;    extern ostream clog;    // ...}

为了方便使用,可以在代码开头加入以下声明:

using namespace std;

其中,endl是一个操纵符,用于结束当前行并刷写缓冲区。<

<和>
>分别是输出和输入运算符。<
<运算符返回左侧的ostream对象,允许连续使用;>
>运算符同样返回左侧的istream对象。

运行上述程序可以看到以下效果:

F:\Codes\cppprimer>IoStream.exeEnter two numbers:1 2The sum of 1 and 2 is 3F:\Codes\cppprimer>IoStream.exeEnter two numbers:1 2aThe sum of 1 and 2 is 3F:\Codes\cppprimer>IoStream.exeEnter two numbers:a bThe sum of 0 and 0 is 0

从上述程序可以看到,输入的处理有更复杂的内容,我们将在后续章节详细介绍。

控制流

与C语言相比,C++在控制流方面有以下几个主要区别:

首先是for循环:

int main(){    for (int i = 0; i < 10; ++i) {        cout << i << endl;    }    return 0;}

与C语言的不同之处在于,i这个变量在C++中可以直接放到for循环语句中。在C语言中,这种写法可能存在版本差异,且一般不推荐使用。需要注意的是,i只在for循环对应的代码块中才有效,外部无效。

其次是结合输入的控制流:

int main(){    int sum = 0;    int value;    while (cin >> value) {        sum += value;    }    cout << "The sum is: " << sum << endl;    return 0;}

运行该程序时,可以按下Ctrl+z再按回车以结束输入,程序将打印总和。类似的逻辑也可以在if语句中使用:

int main(){    int value;    if (cin >> value) {        cout << "The input: " << value << endl;    }    return 0;}

运行结果如下:

F:\Codes\cppprimer>Control.exe1The input: 1

需要注意的是,C++中的输入操作比C语言的scanf函数更加严格,需要确保输入的格式和类型匹配。

类简介

类是C++中用于定义自定义数据结构和行为的核心概念。以下是一个常见的类定义示例:

class Sales_item {    friend std::istream& operator>>(std::istream&, Sales_item&);    friend std::ostream& operator<<(std::ostream&, const Sales_item&);    friend bool operator<(const Sales_item&, const Sales_item&);    friend bool operator==(const Sales_item&, const Sales_item&);public:    Sales_item() = default;    Sales_item(const std::string& book): bookNo(book) {}    Sales_item(std::istream& is) { is >> *this; }public:    Sales_item& operator+=(const Sales_item&);    std::string isbn() const { return bookNo; }    double avg_price() const;private:    std::string bookNo;    unsigned units_sold = 0;    double revenue = 0.0;}

类的关键点包括:

  • 类使用class关键字定义。
  • 输入和输出运算符需要声明为friend,因为它们不是类的成员方法。
  • public接口声明类的公共方法和成员,private声明私有数据和不可见的成员。
  • 构造函数通常放在public中,以便用户创建对象。
  • operator+=等成员函数需要声明为public,以允许对象的操作。
  • 关于operator==和operator!=,需要特别注意以下几点:

  • operator==需要重载为friend函数,因为它不是类的成员方法。
  • operator!=可以直接定义为非friend函数,因为它可以通过operator==实现。
  • operator==的实现需要访问类的私有成员,因此需要声明为friend。
  • 例如,operator==的实现可能如下:

    inline bool operator==(const Sales_item& lhs, const Sales_item& rhs) {    return lhs.units_sold == rhs.units_sold &&            lhs.revenue == rhs.revenue &&            lhs.isbn() == rhs.isbn();}

    而operator!=的实现则可以简单地使用operator==:

    inline bool operator!=(const Sales_item& lhs, const Sales_item& rhs) {    return !(lhs == rhs);}

    关于operator+=,需要注意的是它是一个成员函数,因此需要定义为public。例如:

    Sales_item& operator+(const Sales_item& lhs, const Sales_item& rhs) {    Sales_item ret(lhs);    ret += rhs;    return ret;}

    这个示例中,operator+被定义为非成员函数,因此需要特别处理。

    此外,类中还可以包含其他成员函数和数据成员,用于定义复杂的数据结构和逻辑。

    转载地址:http://akodz.baihongyu.com/

    你可能感兴趣的文章
    npm和package.json那些不为常人所知的小秘密
    查看>>
    npm和yarn清理缓存命令
    查看>>
    npm和yarn的使用对比
    查看>>
    npm如何清空缓存并重新打包?
    查看>>
    npm学习(十一)之package-lock.json
    查看>>
    npm安装 出现 npm ERR! code ETIMEDOUT npm ERR! syscall connect npm ERR! errno ETIMEDOUT npm ERR! 解决方法
    查看>>
    npm安装crypto-js 如何安装crypto-js, python爬虫安装加解密插件 找不到模块crypto-js python报错解决丢失crypto-js模块
    查看>>
    npm安装教程
    查看>>
    npm报错Cannot find module ‘webpack‘ Require stack
    查看>>
    npm报错Failed at the node-sass@4.14.1 postinstall script
    查看>>
    npm报错fatal: Could not read from remote repository
    查看>>
    npm报错File to import not found or unreadable: @/assets/styles/global.scss.
    查看>>
    npm报错unable to access ‘https://github.com/sohee-lee7/Squire.git/‘
    查看>>
    npm淘宝镜像过期npm ERR! request to https://registry.npm.taobao.org/vuex failed, reason: certificate has ex
    查看>>
    npm版本过高问题
    查看>>
    npm的“--force“和“--legacy-peer-deps“参数
    查看>>
    npm的安装和更新---npm工作笔记002
    查看>>
    npm的常用配置项---npm工作笔记004
    查看>>
    npm的问题:config global `--global`, `--local` are deprecated. Use `--location=global` instead 的解决办法
    查看>>
    npm编译报错You may need an additional loader to handle the result of these loaders
    查看>>