C++面向对象类的实例题目二

题目描述:
编写一个程序,设计一个产品类Product,其定义如下:
class Product
{
	public:
		Product(char *n,int p,int q);	//构造函数
		~Product();						//析构函数
		void buy(int money);			//购买产品
		void get() const;				//显示剩余产品数量 
	private:
		char * name;	//产品名称
		int price;		//产品单价
		int quantity;	//剩余产品数量
}; 
并用数据进行测试。

code:

#include<iostream>
#include<cstring>
using namespace std;
class Product
{
		char *name;
		int price;
		int quantity;
	public:
		Product(char *n,int p,int q);
		~Product();
		void buy(int money);
		void get()const;
};
Product::Product(char *n,int p,int q)
{
	name = n;
	price = p;
	quantity = q;
}
Product::~Product()
{
}
void Product::buy(int money)
{
	int r,n;
	n = money/price;
	r = money%price;
	if(n > quantity)
	{
		cout<<"数量不够"<<endl;
	}
	else
	{
		quantity -= n;
		cout<<"名称:"<<name<<",单价:"<<price<<"元"<<endl;
		cout<<"顾客使用"<<money<<"元,购买"<<n<<"台,剩余"<<r<<"元"<<endl; 
	}
}
void Product::get()const
{
	cout<<"产品:"<<name<<",单价:"<<price<<",剩余:"<<quantity<<"台"<<endl; 
}
int main()
{
	Product p("Iphone6",100,20);
	p.buy(10);
	p.get();
	cout<<"\n==========================\n"<<endl; 
	p.buy(1000);
	p.get();
	return 0; 
}


输出:




转载于:https://www.cnblogs.com/zhezh/p/3773356.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值