博客
关于我
[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/

    你可能感兴趣的文章
    PowerEdge T630服务器安装机器学习环境(Ubuntu18.04、Nvidia 1080Ti驱动、CUDA及CUDNN安装)
    查看>>
    PowerPC-object与elf中的符号引用
    查看>>
    QFileSystemModel
    查看>>
    Powershell DSC 5.0 - 参数,证书加密账号,以及安装顺序
    查看>>
    PowerShell 批量签入SharePoint Document Library中的文件
    查看>>
    Powershell 自定义对象小技巧
    查看>>
    pytorch从预训练权重加载完全相同的层
    查看>>
    PowerShell~发布你的mvc网站
    查看>>
    PowerShell使用详解
    查看>>
    Powershell制作Windows安装U盘
    查看>>
    powershell命令
    查看>>
    Powershell如何查看本地公网IP
    查看>>
    pytorch从csv加载自定义数据模板
    查看>>
    powershell对txt文件的服务器进行ping操作
    查看>>
    powershell常用
    查看>>
    PowerShell操作XML遇到的问题
    查看>>
    PowerShell攻击工具Empire实战
    查看>>
    PowerShell攻击工具Nishang实战
    查看>>
    PowerShell攻击工具PowerSploit实战
    查看>>
    Powershell管理系列(四)Lync server 2013 批量启用语音及分配分机号
    查看>>