c++---------流类

[复制链接]
发表于 2025-11-23 21:08:31 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×

  • 格式化输入(cin的格式化)

    • 根本用法与控制符

      • 在C++中,std::cin用于从标准输入(通常是键盘)读取数据。它默认以空缺字符(空格、制表符、换行符)为分隔符来读取差异的数据。比方,读取两个整数:
      1. #include <iostream>
      2. int main() {
      3.   int num1, num2;
      4.   std::cin >> num1 >> num2;
      5.   std::cout << "两个数分别是: " << num1 << "和" << num2 << std::endl;
      6.   return 0;
      7. }
      复制代码

      • 可以使用一些控制符来改变cin的活动。比方,std::hex用于以十六进制格式读取数据,std:ct用于以八进制格式读取数据。读取十六进制整数的示例:
      1. #include <iostream>
      2. int main() {
      3.   int hex_num;
      4.   std::cin >> std::hex >> hex_num;
      5.   std::cout << "十六进制转换后的十进制数是: " << hex_num << std::endl;
      6.   return 0;
      7. }
      复制代码
    • 输入验证与错误处理惩罚

      • 当输入的数据范例与渴望不符时,cin会进入错误状态。可以通过cin.fail()来查抄是否出现错误。比方,渴望输入一个整数,但用户输入了一个字符:
      1. #include <iostream>
      2. int main() {
      3.   int num;
      4.   std::cin >> num;
      5.   if (cin.fail()) {
      6.     std::cerr << "输入错误,请输入一个整数" << std::endl;
      7.     cin.clear();
      8.     std::string discard;
      9.     std::cin >> discard;
      10.   } else {
      11.     std::cout << "输入的整数是: " << num << std::endl;
      12.   }
      13.   return 0;
      14. }
      复制代码
      在上述代码中,cin.clear()用于扫除错误状态,std::string discard; std::cin >> discard;用于抛弃输入缓冲区中的错误数据。



  • 格式化输出(cout的格式化)

    • 根本格式化控制符

      • std::cout是用于标准输出(通常是控制台)的对象。可以使用控制符来格式化输出。比方,std::setw()用于设置输出宽度,std::setfill()用于设置添补字符,std::fixed和std::scientific用于控制浮点数的输特别式。输出一个右对齐的整数示例:
      1. #include <iostream>
      2. #include <iomanip>
      3. int main() {
      4.   int num = 123;
      5.   std::cout << std::setw(6) << std::right << num << std::endl;
      6.   return 0;
      7. }
      复制代码
    • 自界说输特别式(通过重载<<运算符)

      • 对于自界说的类,可以重载<<运算符来实现自界说的输特别式。比方,界说一个Point类并重载<<运算符:
      1. class Point {
      2. public:
      3.   int x;
      4.   int y;
      5.   Point(int a = 0, int b = 0) : x(a), y(b) {}
      6. };
      7. std::ostream& operator<<(std::ostream& os, const Point& p) {
      8.   os << "(" << p.x << ", " << p.y << ")";
      9.   return os;
      10. }
      11. int main() {
      12.   Point p(3, 4);
      13.   std::cout << p << std::endl;
      14.   return 0;
      15. }
      复制代码

  • 数据文件(文件输入输出)

    • 文件写入(ofstream)

      • std:fstream用于将数据写入文件。可以指定文件名和打开模式(如ios:ut用于输出,ios::app用于追加等)。比方,将一些文本写入文件:
      1. #include <iostream>
      2. #include <fstream>
      3. int main() {
      4.   std::ofstream out_file("output.txt", ios::out);
      5.   if (out_file) {
      6.     out_file << "这是第一行" << std::endl;
      7.     out_file << "这是第二行" << std::endl;
      8.     out_file.close();
      9.   } else {
      10.     std::cerr << "无法打开文件进行写入" << std::endl;
      11.   }
      12.   return 0;
      13. }
      复制代码
    • 文件读取(ifstream)

      • std::ifstream用于从文件中读取数据。比方,读取刚才写入文件中的内容:
      1. #include <iostream>
      2. #include <ifstream>
      3. int main() {
      4.   std::ifstream in_file("output.txt");
      5.   if (in_file) {
      6.     std::string line;
      7.     while (std::getline(in_file, line)) {
      8.       std::cout << line << std::endl;
      9.     }
      10.     in_file.close();
      11.   } else {
      12.     std::cerr << "无法打开文件进行读取" << std::endl;
      13.   }
      14.   return 0;
      15. }
      复制代码
    • 二进制文件操纵(fstream)

      • 对于二进制文件,可以使用std::fstream举行读写操纵。比方,将一个整数数组以二进制情势写入文件,然后再读取出来:
      1. #include <iostream>
      2. #include <fstream>
      3. int main() {
      4.   int numbers[] = {1, 2, 3, 4, 5};
      5.   // 写入二进制文件
      6.   std::fstream bin_file("binary.bin", ios::out | ios::binary);
      7.   if (bin_file) {
      8.     bin_file.write(reinterpret_cast<char*>(numbers), sizeof(numbers));
      9.     bin_file.close();
      10.   } else {
      11.     std::cerr << "无法打开二进制文件进行写入" << std::endl;
      12.   }
      13.   // 读取二进制文件
      14.   int read_numbers[5];
      15.   std::fstream bin_read_file("binary.bin", ios::in | ios::binary);
      16.   if (bin_read_file) {
      17.     bin_read_file.read(reinterpret_cast<char*>(read_numbers), sizeof(read_numbers));
      18.     for (int i = 0; i < 5; ++i) {
      19.       std::cout << read_numbers[i] << " ";
      20.     }
      21.     bin_read_file.close();
      22.   } else {
      23.     std::cerr << "无法打开二进制文件进行读取" << std::endl;
      24.   }
      25.   return 0;
      26. }
      复制代码

  • 类条理(面向对象中的类继承条理布局)

    • 根本概念

      • 类条理布局是通过类的继承关系构建的。基类(父类)界说了一些通用的属性和活动,派生类(子类)继承基类并可以添加自己的特定属性和活动。比方,界说一个Shape基类和Circle、Rectangle派生类:
      1. class Shape {
      2. public:
      3.   virtual double area() = 0;
      4. };
      5. class Circle : public Shape {
      6. public:
      7.   double radius;
      8.   Circle(double r) : radius(r) {}
      9.   double area() override {
      10.     return 3.14 * radius * radius;
      11.   }
      12. };
      13. class Rectangle : public Shape {
      14. public:
      15.   double width;
      16.   double height;
      17.   Rectangle(double w, double h) : width(w), height(h) {}
      18.   double area() override {
      19.     return width * height;
      20.   }
      21. };
      复制代码
    • 多态性与虚函数

      • 多态性答应通过基类指针或引用调用派生类的函数。在上述例子中,Shape类中的area函数是虚函数,通过基类指针调用area函数时,会根据指针所指向的实际对象(Circle或Rectangle)来调用相应的area函数实现。比方:
      1. int main() {
      2.   Shape* shape1 = new Circle(3);
      3.   Shape* shape2 = new Rectangle(4, 5);
      4.   std::cout << "圆的面积: " << shape1->area() << std::endl;
      5.   std::cout << "矩形的面积: " << shape2->area() << std::endl;
      6.   delete shape1;
      7.   delete shape2;
      8.   return 0;
      9. }
      复制代码
    • 继承中的访问控制(public、private、protected)

      • 在继承关系中,public继承表现派生类继承基类的public和protected成员,而且这些成员在派生类中的访问权限稳固。private继承会将基类的public和protected成员变为派生类的private成员。protected继承会将基类的public成员变为派生类的protected成员。比方:
      1. class Base {
      2. public:
      3.   int public_member;
      4. protected:
      5.   int protected_member;
      6. private:
      7.   int private_member;
      8. };
      9. class PublicDerived : public Base {
      10. public:
      11.   void accessMembers() {
      12.     public_member = 1;
      13.     protected_member = 2;
      14.   }
      15. };
      16. class PrivateDerived : private Base {
      17. public:
      18.   void accessMembers() {
      19.     public_member = 3;
      20.     protected_member = 4;
      21.   }
      22. };
      复制代码
      在PublicDerived类中,可以直接访问基类的public和protected成员。而在PrivateDerived类中,固然可以访问基类的public和protected成员,但是这些成员在PrivateDerived类外部是不可访问的,由于它们被继承为private成员。


回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表