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;
	}
相关推荐
先吃饱再说11 小时前
判断回文字符串,从一行代码到双指针优化
算法
见过夏天11 小时前
C++ 基础入门完全指南
c++
黄敬峰14 小时前
深入理解算法核心:从递归思想、数组扁平化到快速排序
算法
得物技术15 小时前
从狂野代码到按目标生产:得物推荐 AI Harness 的工程化实践|AICon 演讲整理
人工智能·算法·架构
AI小老六19 小时前
SkillOpt 架构拆解:把 Skill 文本当参数,用执行轨迹训练 Agent
后端·算法·ai编程
胡萝卜术19 小时前
从“分数打架”到“排名投票”:为什么你的ChatBI必须用RRF?
算法·设计模式·面试
Asize20 小时前
初识DFS 与 BFS:递归、队列与图遍历
算法
罗西的思考1 天前
机器人 / 强化学习】HIL-SERL:人类在环驱动的具身智能进化框架
人工智能·算法·机器学习
美团技术团队2 天前
LongCat 开源 VitaBench 2.0:长期动态智能体基准新标杆
人工智能·算法