void Apple::print_fun(){ cout << this->id << " "; cout << this->weight << " "; cout << this->diameter << endl; }是什么意思
时间: 2023-12-17 08:03:09 浏览: 29
这是一个C++类中的成员函数,函数名为print_fun()。该函数用于输出苹果对象的id、重量和直径。其中,this是一个指向当前对象的指针,id、weight和diameter是该对象的成员变量。cout是C++标准库中的输出流对象,用于向控制台输出内容。<<是C++中的输出运算符,用于将内容插入到输出流中。endl是C++中的换行符,用于结束当前行并将光标移动到下一行开头。
相关问题
void Print() const { cout << _year << "-" << _month << "-" << _day << endl; }//在这里const的作用是什么
在这个例子中,`void Print() const`中的`const`关键字的作用是修饰`this`指针,使得`this`指向的成员变量不能被改变。也就是说,如果在`Print()`函数中有任何尝试修改成员变量的操作,编译器都会报错。这样做的好处是可以保证`Print()`函数不会对对象的状态进行修改,从而保证了函数的安全性和可靠性。
举个例子,如果我们在`Print()`函数中尝试修改成员变量`_year`的值,编译器就会报错,因为`Print()`函数被声明为`const`,而`const`成员函数不能修改成员变量的值。
```cpp
void Print() const
{
_year = 2022; // 编译器会报错
cout << _year << "-" << _month << "-" << _day << endl;
}
```
#include<iostream> using namespace std; void showflags(long f) { long i = 0x8000; for (; i; i = i >> 1) { if (i & f) cout << "1"; else cout << "0"; } cout << endl; } int main() { showflags(cout.flags()); cout << "x_width=" << cout.width() << endl; cout << "x_fill=" << cout.fill() << endl; cout << "x_precision=" << cout.precision() << endl; cout << 123 << " " << 123.45678 << "\n"; cout << "-------------" << endl; cout << "* * * x_width=10,x_fill= ,x_precision=4 * * *" << endl<<endl<<endl<<endl; cout.width(10); cout.precision(4); cout << 123 << " " << 123.45678 << " " << 234.567 << endl;//1 cout << "x_width=" << cout.width() << endl; cout << "x_fill=" << cout.fill() << endl; cout << "x_precision=" << cout.precision() << endl; cout << "-------------" << endl; cout << "* * * x_width=10,x_fill=&,x_precision=4 * * *" << endl; cout.fill('&'); cout.width(10); cout << 123 << " " << 123.45678 << endl; cout.setf(ios::left); cout.width(10); cout << 123 << " " << 123.45678 << endl; cout << "x_width=" << cout.width() << endl; cout << "x_fill=" << cout.fill() << endl; cout << "x_precision=" << cout.precision() << endl<<endl<<endl; showflags(cout.flags());//2 cout.setf(ios::right | ios::unitbuf); cout << endl << endl << endl; showflags(cout.flags());//3 }
这段代码主要演示了 C++ 中的格式化输出,包括了流对象的标志位、宽度、填充字符和精度等属性。代码的执行流程如下:
1. 首先输出流对象 cout 的标志位,即输出格式的相关属性,使用 showflags 函数实现。
2. 输出数值 123 和 123.45678,观察宽度、填充字符和精度的默认值。
3. 更改宽度为 10,精度为 4,再次输出数值 123、123.45678 和 234.567,观察结果。
4. 更改填充字符为 &,并设置左对齐,重新输出数值 123 和 123.45678,观察结果。
具体的代码解释如下:
```c++
#include<iostream>
using namespace std;
void showflags(long f) {
long i = 0x8000;
for (; i; i = i >> 1) {
if (i & f)
cout << "1";
else
cout << "0";
}
cout << endl;
}
int main() {
showflags(cout.flags()); // 输出流对象的标志位,使用 showflags 函数实现
cout << "x_width=" << cout.width() << endl; // 输出宽度
cout << "x_fill=" << cout.fill() << endl; // 输出填充字符
cout << "x_precision=" << cout.precision() << endl; // 输出精度
cout << 123 << " " << 123.45678 << "\n"; // 输出数值 123 和 123.45678
cout << "-------------" << endl;
cout << "* * * x_width=10,x_fill= ,x_precision=4 * * *" << endl<<endl<<endl<<endl;
cout.width(10); // 设置宽度为 10
cout.precision(4); // 设置精度为 4
cout << 123 << " " << 123.45678 << " " << 234.567 << endl; // 输出数值 123、123.45678 和 234.567
cout << "x_width=" << cout.width() << endl; // 输出宽度
cout << "x_fill=" << cout.fill() << endl; // 输出填充字符
cout << "x_precision=" << cout.precision() << endl;
cout << "-------------" << endl;
cout << "* * * x_width=10,x_fill=&,x_precision=4 * * *" << endl;
cout.fill('&'); // 设置填充字符为 &
cout.width(10); // 设置宽度为 10
cout << 123 << " " << 123.45678 << endl; // 输出数值 123 和 123.45678
cout.setf(ios::left); // 设置左对齐
cout.width(10); // 设置宽度为 10
cout << 123 << " " << 123.45678 << endl; // 输出数值 123 和 123.45678
cout << "x_width=" << cout.width() << endl; // 输出宽度
cout << "x_fill=" << cout.fill() << endl; // 输出填充字符
cout << "x_precision=" << cout.precision() << endl<<endl<<endl;
showflags(cout.flags()); // 输出流对象的标志位
cout.setf(ios::right | ios::unitbuf); // 设置右对齐和刷新缓冲区
cout << endl << endl << endl;
showflags(cout.flags()); // 输出流对象的标志位
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![text/plain](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)