
请求高手,帮我做道C++题
使用C++的类建立一个简单卖玩具的程序。类内必须具有单价、售出数量以及每种玩具售出的总金额等数据,为该类建立一些必要的函数,并在主程序中使用对象数组建立若干个带有单价和售...
使用C++的类建立一个简单卖玩具的程序。类内必须具有单价、售出数量以及每种玩具售出的总金额等数据,为该类建立一些必要的函数,并在主程序中使用对象数组建立若干个带有单价和售出数量的对象,显示每种玩具售出的总金额。
展开
展开全部
/*TOY.h文件*/
#ifndef TOY_H
#define TOY_H
#include <string>
using namespace std;
class TOY{
private:
string name; //商品名称
float price; //商品价格
int count; //商品售出数量
public:
void setPrice(float price); //设置商品价格
void setCount(int count); //设置商品售出数量
void setName(string name); //设置商品名称
void addCount(); //增加商品售出数量
float getPrice(); //获得商品价格
int getCount(); //获得商品售出数量
float totalMoney(); //计算商品售出总额
void printToyInfo(); //输出商品信息
TOY();
TOY(string name,float price);
~TOY();
};
#endif
/*TOY.cpp文件*/
#include "TOY.h"
#include <iostream>
TOY::TOY()
{
name = "footBall";
price = 60.00;
count = 0;
}
TOY::TOY(string name, float price)
{
this->name = name;
this->price = price;
count = 0;
}
void TOY::setCount(int count)
{
this->count = count;
}
void TOY::setPrice(float price)
{
this->price = price;
}
void TOY::setName(string name)
{
this->name = name;
}
void TOY::addCount()
{
count++;
}
float TOY::getPrice()
{
return price;
}
int TOY::getCount()
{
return count;
}
float TOY::totalMoney()
{
return price*count;
}
void TOY::printToyInfo()
{
cout<<name<<"\t"<<price<<"\t"<<count<<"\t"<<totalMoney()<<std::endl;
}
TOY::~TOY()
{
}
/*demo.cpp文件*/
#include <iostream>
#include "TOY.h"
#define MAX 100
using namespace std;
void main ()
{
TOY* toy[MAX];
int index;
toy[0] = new TOY();
toy[1] = new TOY("basketBall",90.20);
toy[0]->setCount(20);
toy[1]->setCount(30);
cout<<"Name\t\tPrice\tCount\tTotal"<<endl;
toy[0]->printToyInfo();
toy[1]->printToyInfo();
}
相当粗糙,仅供参考,
另外主程序中如果用容器会更好。
#ifndef TOY_H
#define TOY_H
#include <string>
using namespace std;
class TOY{
private:
string name; //商品名称
float price; //商品价格
int count; //商品售出数量
public:
void setPrice(float price); //设置商品价格
void setCount(int count); //设置商品售出数量
void setName(string name); //设置商品名称
void addCount(); //增加商品售出数量
float getPrice(); //获得商品价格
int getCount(); //获得商品售出数量
float totalMoney(); //计算商品售出总额
void printToyInfo(); //输出商品信息
TOY();
TOY(string name,float price);
~TOY();
};
#endif
/*TOY.cpp文件*/
#include "TOY.h"
#include <iostream>
TOY::TOY()
{
name = "footBall";
price = 60.00;
count = 0;
}
TOY::TOY(string name, float price)
{
this->name = name;
this->price = price;
count = 0;
}
void TOY::setCount(int count)
{
this->count = count;
}
void TOY::setPrice(float price)
{
this->price = price;
}
void TOY::setName(string name)
{
this->name = name;
}
void TOY::addCount()
{
count++;
}
float TOY::getPrice()
{
return price;
}
int TOY::getCount()
{
return count;
}
float TOY::totalMoney()
{
return price*count;
}
void TOY::printToyInfo()
{
cout<<name<<"\t"<<price<<"\t"<<count<<"\t"<<totalMoney()<<std::endl;
}
TOY::~TOY()
{
}
/*demo.cpp文件*/
#include <iostream>
#include "TOY.h"
#define MAX 100
using namespace std;
void main ()
{
TOY* toy[MAX];
int index;
toy[0] = new TOY();
toy[1] = new TOY("basketBall",90.20);
toy[0]->setCount(20);
toy[1]->setCount(30);
cout<<"Name\t\tPrice\tCount\tTotal"<<endl;
toy[0]->printToyInfo();
toy[1]->printToyInfo();
}
相当粗糙,仅供参考,
另外主程序中如果用容器会更好。
展开全部
貌似不难,但是没时间做。。。。。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
关注一下~
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
HOHO,这样的东西用200分就想搞定?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询