C++实验 多态 虚函数 模拟银行账户管理程序 停车场管理系统

比较简单, 直接放代码

account.h

class Account
{
public:
	Account();
	Account(double ba);
	virtual void credit(double a);
	virtual bool debit(double a);
	double getBalance();
	~Account();

private:
	double balance;
};
class SavingsAccount :public Account
{
public:
	SavingsAccount();
	SavingsAccount(double ba, double rate);
	double calculateInterest();
	~SavingsAccount();

private:
	double interestRate;
};
class CheckingAccount :public Account
{
public:
	CheckingAccount();
	CheckingAccount(double ba, double t);
	virtual void credit(double a);
	virtual bool debit(double a);
	~CheckingAccount();

private:
	double tip;
};


BankAccount.cpp

#include "account.h"
#include<iostream>;
using namespace std;
Account::Account(double ba)
{
	balance = ba;
}
void Account::credit(double a)
{
	balance += a;
}
bool Account::debit(double a)
{
	if (a > balance)
	{
		cout << "Debit amount exceeded account balance" << endl;
		return false;
	}
	else
	{
		balance -= a;
		return true;
	}
}
double Account::getBalance()
{
	return balance;
}

Account::Account()
{
	balance = 0;
}

Account::~Account()
{
}
SavingsAccount::SavingsAccount(double ba, double rate) :Account(ba)
{
	interestRate = rate;
}
double SavingsAccount::calculateInterest()
{
	return interestRate * getBalance()/100;
}
SavingsAccount::SavingsAccount():Account(100)
{
	interestRate = 0;
}
SavingsAccount::~SavingsAccount()
{
}
CheckingAccount::CheckingAccount():Account(100)
{
	tip = 0;
}
CheckingAccount::CheckingAccount(double ba, double t) : Account(ba)
{
	tip = t;
}
void CheckingAccount::credit(double a)
{
	
	if (a+getBalance()>tip)
	{
		Account::credit(a);
		Account::debit(tip);
	}
	else
	{
		cout << "Transaction fee exceeded account balance while crediting" << endl;
	}

}
bool CheckingAccount::debit(double a)
{
	if (Account::debit(a))
	{
		if (getBalance() > tip)
		{
			Account::debit(tip);
			return true;
		}
		else
		{
			cout << "Transaction fee exceeded account balance while debiting" << endl;
			Account::credit(a);
			return false;
		}
	}
	else
	{
		return false;
	}

}
CheckingAccount::~CheckingAccount()
{
}



main.cpp

#include<iostream>
#include"account.h"
#include <iomanip>
using namespace std;
int main() {
	Account* accounts[3];
	accounts[0] = new SavingsAccount(100, 3);   //余额100元,利息3%
	accounts[1] = new CheckingAccount(100, 5);  //余额100元,交易费5元
	accounts[2] = new CheckingAccount(50, 5);   //余额50元,交易费5元

	for (int i = 0; i < 3; i++) {
		cout << "第" << i + 1 << "次循环的结果:" << endl;
		accounts[i]->debit(200);   //借款200元
		accounts[i]->debit(40);
		accounts[i]->credit(50);   //存款50元
		accounts[i]->debit(49);
		accounts[i]->debit(43);
		accounts[i]->credit(1);
		//将Account指针强制转换为SavingAccount指针
		SavingsAccount* derivedPtr =
			dynamic_cast<SavingsAccount*>(accounts[i]);
		if (derivedPtr != NULL)   //如果类型兼容,转换成功
			derivedPtr->credit(derivedPtr->calculateInterest());
		cout << fixed << setprecision(2);   //使用定点数格式,2位小数部分
		cout << "账户的余额为:" << accounts[i]->getBalance() << endl;
	}
}

停车场写在一起了,懒得浪费时间

#include <iostream>
using namespace std;
class Park
{
public:
	Park();
	Park(int n);
	void showInfo();
	void go(string b);
	void give(string b);
	void getPaid(int n);
	~Park();

private:
	int num;
	int income;
	string plate[10];
	bool  space[10];
};
void Park::getPaid(int n)
{
	income += n;
}
void Park::showInfo()
{
	if (num == 0)
	{
		cout << "停车场目前停放了0辆汽车";
	}
	else
	{
		cout << "停车场目前停放了"<<num<<"辆汽车:";
		for (int i = 0; i < num; i++)
		{
			if (space[i] == 1)
				cout << plate[i] << endl;
		}
	}
	cout << "停车场收入是:" << income << endl;
}
void Park::go(string b)
{
	for (int i = 0; i < num; i++)
	{
		if (plate[i] == b)
		{
			space[i] = 0;
		}
  }
}
void Park::give(string b)
{
	cout << b << "进入停车场,分配停车位"<<endl;
	int i = 0;
	for (i; i < num; i++)
	{
		if (space[i] == 0)
		{
			space[i] = 1;
			plate[i] = b;
			break;
		}
	}
	
	if (i == num)
	{
		cout << "无法为"<<b<<"分配停车位" << endl;
	}
}
Park::Park()
{
}
Park::Park(int n)
{
	num = n;
	for (int i = 0; i < n; i++)
	{
		space[i] = 0;
	}
	income = 0;
}
Park::~Park()
{
}
class Automobile
{
public:
	Automobile();
	Automobile(string k);
	void enter(Park *p)
	{
		p->give(this->plate);
	}
	void leave(Park& park)
	{
		park.go(this->plate);    // 让停车场收回停车位
		pay(park);// 向支付支付停车费,由派生类实现本方法

	}
	~Automobile();
	string getPlate();
protected:
	virtual void pay(Park& park) = 0;  // 向停车场支付停车费,由派生类实现

	

private:
	string plate;

};
string Automobile::getPlate()
{
	return plate;
}
Automobile::Automobile()
{
	plate = "null";
}
Automobile::Automobile(string k)
{
	plate = k;
}



Automobile::~Automobile()
{
}
class Truck : public Automobile
{
public:
	Truck();

	Truck(string b,int i);
	virtual void pay(Park& p);
	~Truck();

private:
	double weight;
};
void Truck::pay(Park& p)
{
	cout << getPlate()<<"离开停车场,缴纳停车费3元" << endl;
	p.getPaid(3);

}
Truck::Truck()
{
	weight = 0;
}
Truck::Truck(string b,int i):Automobile(b)
{
	weight = i;
}

Truck::~Truck()
{
}
class Car :public Automobile
{
public:
	Car();
	Car(string a, string b);
	virtual void pay(Park& p);
	~Car();

private:
	string brand;
};
void Car::pay(Park& p)
{
	cout << getPlate() << "离开停车场,缴纳停车费1元" << endl;
	p.getPaid(1);

}

Car::Car()
{
	brand = "null";
}
Car::Car(string a,string b):Automobile(a)
{
	brand = b;
}

Car::~Car()
{
}
class Bus : public Automobile
{
public:
	Bus();
	Bus(string a,int i);
	virtual void pay(Park& p);
	~Bus();

private:
	int max;
};

void Bus::pay(Park& p)
{
	cout << getPlate() << "离开停车场,缴纳停车费2元" << endl;
	p.getPaid(2);

}
Bus::Bus()
{
}
Bus::Bus(string a,int i):Automobile(a)
{
	max = i;
}

Bus::~Bus()
{
}

int main() {
	int N;
	cout << "请输入停车位数量:";
	cin >> N;// 输入停车位数量,此处输入2

	Park park(N);// 创建一个停车场对象

	Automobile* auto1 = new Car("鲁B-12345", "奥迪A6");  // 创建轿车对象
	Automobile* auto2 = new Truck("鲁B-23456", 15);      // 创建卡车对象
	Automobile* auto3 = new Bus("鲁B-34567", 50);        // 公交车对象
	Automobile* auto4 = new Car("鲁B-45678", "宝马320");// 创建轿车对象

	auto1->enter(&park);   // car进入停车场,分配停车位
	auto2->enter(&park);   // truck进入停车场,分配车位
	auto1->leave(park);   // car离开停车场,缴纳停车费
	auto3->enter(&park);   // bus进入停车场,分配车位

	/* 显示当前停放的车辆的车牌号码,以及当前的全部停车费收入*/
	park.showInfo();

	auto4->enter(&park);      // car进入停车场,分配停车位
	// car进入停车场,分配停车位。因为没有空余停车位,所以无法分配

	auto3->leave(park);  // bus离开停车场,缴纳停车费
	auto2->leave(park);  // truck离开停车场,缴纳停车费

	/* 显示当前停放的车辆的车牌号码,以及当前的全部停车费收入*/
	park.showInfo();

	return 0;
}


  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值