网站做的最好的公司,猪八戒里面做网站骗子很多,外贸soho做网站怎么做,北京一家专门做会所的网站格式化输入#xff08;cin的格式化#xff09; 基本用法与控制符 在C中#xff0c;std::cin用于从标准输入#xff08;通常是键盘#xff09;读取数据。它默认以空白字符#xff08;空格、制表符、换行符#xff09;为分隔符来读取不同的数据。例如#xff0c;读取两个…格式化输入cin的格式化 基本用法与控制符 在C中std::cin用于从标准输入通常是键盘读取数据。它默认以空白字符空格、制表符、换行符为分隔符来读取不同的数据。例如读取两个整数 #include iostream
int main() {int num1, num2;std::cin num1 num2;std::cout 两个数分别是: num1 和 num2 std::endl;return 0;
}可以使用一些控制符来改变cin的行为。例如std::hex用于以十六进制格式读取数据std::oct用于以八进制格式读取数据。读取十六进制整数的示例 #include iostream
int main() {int hex_num;std::cin std::hex hex_num;std::cout 十六进制转换后的十进制数是: hex_num std::endl;return 0;
}输入验证与错误处理 当输入的数据类型与期望不符时cin会进入错误状态。可以通过cin.fail()来检查是否出现错误。例如期望输入一个整数但用户输入了一个字符 #include iostream
int main() {int num;std::cin num;if (cin.fail()) {std::cerr 输入错误请输入一个整数 std::endl;cin.clear(); std::string discard;std::cin discard; } else {std::cout 输入的整数是: num std::endl;}return 0;
}在上述代码中cin.clear()用于清除错误状态std::string discard; std::cin discard;用于丢弃输入缓冲区中的错误数据。 格式化输出cout的格式化 基本格式化控制符 std::cout是用于标准输出通常是控制台的对象。可以使用控制符来格式化输出。例如std::setw()用于设置输出宽度std::setfill()用于设置填充字符std::fixed和std::scientific用于控制浮点数的输出格式。输出一个右对齐的整数示例 #include iostream
#include iomanip
int main() {int num 123;std::cout std::setw(6) std::right num std::endl;return 0;
}自定义输出格式通过重载运算符 对于自定义的类可以重载运算符来实现自定义的输出格式。例如定义一个Point类并重载运算符 class Point {
public:int x;int y;Point(int a 0, int b 0) : x(a), y(b) {}
};
std::ostream operator(std::ostream os, const Point p) {os ( p.x , p.y );return os;
}
int main() {Point p(3, 4);std::cout p std::endl;return 0;
}数据文件文件输入输出 文件写入ofstream std::ofstream用于将数据写入文件。可以指定文件名和打开模式如ios::out用于输出ios::app用于追加等。例如将一些文本写入文件 #include iostream
#include fstream
int main() {std::ofstream out_file(output.txt, ios::out);if (out_file) {out_file 这是第一行 std::endl;out_file 这是第二行 std::endl;out_file.close();} else {std::cerr 无法打开文件进行写入 std::endl;}return 0;
}文件读取ifstream std::ifstream用于从文件中读取数据。例如读取刚才写入文件中的内容 #include iostream
#include ifstream
int main() {std::ifstream in_file(output.txt);if (in_file) {std::string line;while (std::getline(in_file, line)) {std::cout line std::endl;}in_file.close();} else {std::cerr 无法打开文件进行读取 std::endl;}return 0;
}二进制文件操作fstream 对于二进制文件可以使用std::fstream进行读写操作。例如将一个整数数组以二进制形式写入文件然后再读取出来 #include iostream
#include fstream
int main() {int numbers[] {1, 2, 3, 4, 5};// 写入二进制文件std::fstream bin_file(binary.bin, ios::out | ios::binary);if (bin_file) {bin_file.write(reinterpret_castchar*(numbers), sizeof(numbers));bin_file.close();} else {std::cerr 无法打开二进制文件进行写入 std::endl;}// 读取二进制文件int read_numbers[5];std::fstream bin_read_file(binary.bin, ios::in | ios::binary);if (bin_read_file) {bin_read_file.read(reinterpret_castchar*(read_numbers), sizeof(read_numbers));for (int i 0; i 5; i) {std::cout read_numbers[i] ;}bin_read_file.close();} else {std::cerr 无法打开二进制文件进行读取 std::endl;}return 0;
}类层次面向对象中的类继承层次结构 基本概念 类层次结构是通过类的继承关系构建的。基类父类定义了一些通用的属性和行为派生类子类继承基类并可以添加自己的特定属性和行为。例如定义一个Shape基类和Circle、Rectangle派生类 class Shape {
public:virtual double area() 0;
};
class Circle : public Shape {
public:double radius;Circle(double r) : radius(r) {}double area() override {return 3.14 * radius * radius;}
};
class Rectangle : public Shape {
public:double width;double height;Rectangle(double w, double h) : width(w), height(h) {}double area() override {return width * height;}
};多态性与虚函数 多态性允许通过基类指针或引用调用派生类的函数。在上述例子中Shape类中的area函数是虚函数通过基类指针调用area函数时会根据指针所指向的实际对象Circle或Rectangle来调用相应的area函数实现。例如 int main() {Shape* shape1 new Circle(3);Shape* shape2 new Rectangle(4, 5);std::cout 圆的面积: shape1-area() std::endl;std::cout 矩形的面积: shape2-area() std::endl;delete shape1;delete shape2;return 0;
}继承中的访问控制public、private、protected 在继承关系中public继承表示派生类继承基类的public和protected成员并且这些成员在派生类中的访问权限不变。private继承会将基类的public和protected成员变为派生类的private成员。protected继承会将基类的public成员变为派生类的protected成员。例如 class Base {
public:int public_member;
protected:int protected_member;
private:int private_member;
};
class PublicDerived : public Base {
public:void accessMembers() {public_member 1; protected_member 2; }
};
class PrivateDerived : private Base {
public:void accessMembers() {public_member 3; protected_member 4; }
};在PublicDerived类中可以直接访问基类的public和protected成员。而在PrivateDerived类中虽然可以访问基类的public和protected成员但是这些成员在PrivateDerived类外部是不可访问的因为它们被继承为private成员。