题目描述:
编写一个程序,设计一个产品类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. Android项目的目录结构 初学者记录

    Android项目的目录结构 Activity:应用被打开时显示的界面 src:项目代码 R.java:项目中所有资源文件的资源id Android.jar:Android的jar包,导入此包方可使用 ...

  2. 从AD域获取用户AD信息

    public static Dictionary<string, string> SearchADInfo(string adName) { string strTemp = " ...

  3. nyoj-1099-Lan Xiang's Square(几何,水题)

    题目链接 /* Name:nyoj-1099-Lan Xiang's Square Copyright: Author: Date: 2018/4/26 9:19:19 Description: 给4 ...

  4. STL迭代器辅助函数——advance

    Advance(i, n) increments the iterator i by the distance n. If n > it it , the call has no effect. ...

  5. tensorflow中使用tf.ConfigProto()配置Session运行参数&&GPU设备指定

    tf.ConfigProto()函数用在创建session的时候,用来对session进行参数配置: config = tf.ConfigProto(allow_soft_placement=True ...

  6. npm镜像安装

    安装淘宝NPM镜像 https://npm.taobao.org/ npm install -g cnpm --registry=https://registry.npm.taobao.org 配置 ...

  7. js性能优化文章集锦

    总结的js性能优化方面的小知识http://www.it165.net/pro/html/201503/35336.html 如何优化你的JS代码http://www.php100.com/html/ ...

  8. JavaScript正则常用知识总结

    一.JavaScript正则相关方法 str.match(regexp)与regexp.exec(str)功能类似. str.search(regexp)与regexp.test(str)功能类似. ...

  9. Linux性能监控工具sysstat系列简介

    简介 sysstat提供了Linux性能监控的工具集,包括sar.sadf.mpstat.iostat.pidstat等,这些工具可以监控系统性能和使用情况.各工具的作用如下: iostat - 提供 ...

  10. 全球知名的HTTPS网站检测工具-Qualys SSL Labs

    推荐一个在线版全球知名的HTTPS网站检测工具-Qualys SSL Labs.Qualys SSL Labs同时也是很具有影响力的SSL安全和性能研究机构. SSL Labs会对HTTPS网站的证书 ...