int main() { cout << "E02114357 赵腾伟" << endl; cout << "==============================" << endl; BankAccount bAccount1; bAccount1.show(); bAccount1.setBalance(5000); bAccount1.setRate(0.25); bAccount1.show(); cout << "==============================" << endl; BankAccount bAccount2(3000, 0.3); bAccount2.show(); bAccount2.setBalance(5000); bAccount2.setRate(0.35); bAccount2.show(); cout << "==============================" << endl; CheckingAccount cAccount(3000, 0.3); cAccount.show(); cAccount.deposit(1000, 10); cAccount.show(); cAccount.withdraw(2000, 20); cAccount.show(); cAccount.setRate(0.25); cAccount.show(); return 0; } 分析程序结构
时间: 2023-08-25 07:06:26 浏览: 35
这段代码是一个使用BankAccount和CheckingAccount类的示例程序。程序的主函数是main函数。
在main函数中,首先输出了一个学号和姓名的标识,用来标识作者。然后输出了一条分隔线。
接下来创建了一个名为bAccount1的BankAccount对象,并调用其show函数展示初始状态。然后使用setBalance函数设置余额为5000,并使用setRate函数设置利率为0.25,再次调用show函数展示更新后的状态。
再次输出一条分隔线。
然后创建了一个名为bAccount2的BankAccount对象,并在创建时传入初始余额为3000和利率为0.3的参数。调用show函数展示初始状态。然后使用setBalance函数设置余额为5000,并使用setRate函数设置利率为0.35,再次调用show函数展示更新后的状态。
再次输出一条分隔线。
接下来创建了一个名为cAccount的CheckingAccount对象,并在创建时传入初始余额为3000和利率为0.3的参数。调用show函数展示初始状态。然后使用deposit函数进行存款,金额为1000,时间为10,再次调用show函数展示存款后的状态。接着使用withdraw函数进行取款,金额为2000,时间为20,再次调用show函数展示取款后的状态。最后使用setRate函数设置利率为0.25,再次调用show函数展示更新后的状态。
程序最后返回0,表示执行成功结束。
这个程序的结构遵循了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()); 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()); // 输出流对象的标志位
}
```
#include<iostream.h> int fiddle(int x, int \\\\&y){ x=x+y; y=x-y; cout<<x=<<x<<, y=<<y<<endl; // (分) return x+y; } int main(){ int x(3),y(7),z; z=fiddle(x,y); cout<<x=<<x<<, y=<<y; cout<<, z=<<z<<end
抱歉,这段代码有语法错误,无法运行。其中包括:
1. `#include<iostream.h>` 应该改为 `#include<iostream>`
2. `cout<<x=` 应该改为 `cout<<"x="<<x`
3. `cout<<, y=` 应该改为 `cout<<", y="<<y`
4. `end` 应该改为 `endl`
正确的代码应该是这样的:
```
#include<iostream>
using namespace std;
int fiddle(int x, int &y){
x=x+y;
y=x-y;
cout<<"x="<<x<<", y="<<y<<endl;
return x+y;
}
int main(){
int x(3),y(7),z;
z=fiddle(x,y);
cout<<"x="<<x<<", y="<<y<<", z="<<z<<endl;
return 0;
}
```
这段代码定义了一个函数 `fiddle`,接受两个参数 `x` 和 `y`,并在函数内部交换了 `x` 和 `y` 的值,最后返回它们的和。在 `main` 函数中,调用了 `fiddle` 函数,并输出了 `x`、`y` 和 `z` 的值。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)