C++编程逻辑讲解step by step:存折和信用卡类。

题目

存折和信用卡类,信用卡是一种存折,可以透支,可以存款。

代码

cpp 复制代码
#include<iostream>
#include<string>
using namespace std;
class passbook
{public:
passbook(string nam,int n,float m)
{name=nam;
num=n;
balance=m;
}
void get_value()
{cin>>name>>num>>balance;}
void display()
{cout<<name<<" "<<num<<" "<<"余额:"<<balance<<" ";}
void deposit();
void withdraw();
protected:
	string name;
	int num;
	float balance;
	};
	void passbook::deposit()
	{
		float sav;
		cout<<"存入金额:";
		cin>>sav;
	balance=balance+sav;
	}
	void passbook::withdraw()
	{
		float withdraw;
		int i;
		cout<<"取款金额:";
		
		for(i=0;i<2;i++)
		{cin>>withdraw;
	if(balance-withdraw<0)
	{cout<<"余额不足,重新输入"<<endl;
	continue;
	}
	else
	{balance=balance-withdraw;
	}
	}
	}
	
	class credit:public passbook
	{public:
	credit(string nam,int n,float m,float a):passbook(nam,n,m)
	{arrears=a;
	}
	void show()
	{display();
	cout<<"透支:"<<arrears<<endl;
	}
	void out()
	{float sd;
cout<<"取款金额:";
	cin>>sd;
	if(arrears+balance-sd<0)
	{arrears=arrears+balance-sd;
	balance=0;}
	else
	{balance=balance-sd;}
	}
	private:
		float arrears;
	};
	int main()
	{credit studl("小陈",3423132,1000,0);
	studl.show();
	studl.deposit();
	studl.show();
	studl.out();
	studl.show();
	passbook stul2("马云",43842238,100);
	stul2.display();
	stul2.deposit();
	stul2.display();
	stul2.withdraw();
	stul2.display();
	return 0;
	}
相关推荐
小灰灰搞电子2 小时前
Rust+Slint 实现动态消息提示框源码分享
开发语言·后端·rust
神仙别闹3 小时前
基于 C++ 实现两个有序链表序列的交集
java·c++·链表
传奇开心果编程4 小时前
【Rust入门知识点学与练】第21课:Trait 进阶 Advanced Traits
开发语言·学习·rust
新时代牛马4 小时前
字符设备驱动完整篇:从 cdev_add、file_operations 到chrdev_open 与排障
开发语言·python
swordbob4 小时前
ReentrantLock 与 AQS 完整学习手册
java·开发语言
政企项目老覃4 小时前
大模型幻觉治理与自动评测:金融风控场景的落地实践
人工智能·算法·机器学习
淡海水4 小时前
08-03-不可变-ImmutableDictionary-TKey-TValue-与ImmutableHashSet-T-持久化哈希树
数据结构·算法·c#·哈希算法·dictionary·immutable
白山编程大哥5 小时前
Java OutputStreamWriter 详解:从字符到字节的桥梁
java·开发语言·python
hansang_IR5 小时前
【题解】[APIO2023] 赛博乐园 / cyberland
c++·算法·图论