题目描述:
编写一个程序,设计一个产品类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;
}

输出:




C++面向对象类的实例题目二的更多相关文章

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

    题目描述: 写一个程序计算正方体.球体和圆柱体的表面积和体积 程序代码: #include<iostream> #define PAI 3.1415 using namespace std ...

  2. C++面向对象类的实例题目四

    题目描述: 以面向对象的概念设计一个类,此类包含3个私有数据:unlead.lead(无铅汽油和有铅汽油)以及total(当天总收入,无铅汽油的价格是17元/升,有铅汽油的加个是16元/升),请以构造 ...

  3. C++面向对象类的实例题目十

    题目描述: 编写一个程序,其中有一个汽车类vehicle,它具有一个需要传递参数的构造函数,类中的数据成员:车轮个数wheels和车重weight放在保护段中:小车类car是它的私有派生类,其中包含载 ...

  4. C++面向对象类的实例题目九

    题目描述: 编写一个学生和老师数据输入和显示程序,学生数据有编号.姓名.班号和成绩,教师数据有编号.姓名.职称和部门. 要求将编号.姓名.输入和显示设计成一个类person,并作为学生数据操作类stu ...

  5. C++面向对象类的实例题目八

    题目描述: 编写一个程序输入3个学生的英语和计算机成绩,并按照总分从高到低排序.要求设计一个学生类Student,其定义如下: 程序代码: #include<iostream> using ...

  6. C++面向对象类的实例题目七

    题目描述: 编写两个有意义的类,使一个类嵌套在另一个类中. 分析: 本题涉及两个类student和cdegree,前者为学生类,包含学生的学号(nubner),姓名(name)和成绩(degree), ...

  7. C++面向对象类的实例题目五

    题目描述: 编写一个程序,采用一个类求n!,并输出5!的值. 程序代码: #include<iostream> using namespace std; class CFactorial ...

  8. C++面向对象类的实例题目十一

    题目描述: 写一个程序计算三角形,正方形和圆形3种图形的面积 程序代码: #include<iostream> #include<cmath> #define PAI 3.14 ...

  9. C++面向对象类的实例题目六

    问题描述: 编写一个程序计算两个给定长方形的面积,其中在设计类成员函数addarea()(用于计算两个长方形的总面积)时使用对象作为参数. 程序代码: #include<iostream> ...

随机推荐

  1. LeetCode 298. Binary Tree Longest Consecutive Sequence

    原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...

  2. Leetcode Longest Uncommon Subsequence I

    原题链接在这里:https://leetcode.com/problems/longest-uncommon-subsequence-i/#/description 题目: Given a group ...

  3. 2017/2/22怎么判断mongodb服务已经启动了?

    打开任务管理器,看看服务下面是否有个MongoDB,有就表示成功

  4. 隐藏select中的“请选择”项

    <select> <option value="" style="display: none">请选择</option> & ...

  5. 基于JQ的多选/全选/反选及获取选中的值

    <!-- author:青芒 --> <!DOCTYPE html> <html lang="en"> <head> <met ...

  6. Websphere中的几个常用概念

    什么是单元(Cell)?什么是节点(Node)?Node.Profile 与 Server 之间的关系是什么? 答: 单元: 单元是整个分布式网络中一个或多个节点的逻辑分组.单元是一个配置概念,是管理 ...

  7. UDP10040 和 setsockopt设置大全

    今天无意之中碰到 UDP 10040 错误  原来是缓冲区不够,以下转载的解决方法以供不时之需.   1.closesocket(一般不会立即关闭而经历TIME_WAIT的过程)后想继续重用该sock ...

  8. POJ3249(DAG上的dfs)

    Test for Job Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 10567   Accepted: 2482 Des ...

  9. Java-API-Package:javax.annotation

    ylbtech-Java-API-Package:javax.annotation 1.返回顶部 1. Package javax.annotation Enum Summary Resource.A ...

  10. AngularJS:模块

    ylbtech-AngularJS:模块 1.返回顶部 1. AngularJS 模块 模块定义了一个应用程序. 模块是应用程序中不同部分的容器. 模块是应用控制器的容器. 控制器通常属于一个模块. ...