程序代码见下:
#include
#include
#include
using namespace std;
class Product{
private:
string ProductName;
string MatName;
int MatPrice;
int ServicePrice;
int SalePrice;
public:
Product();
void CalSalePrice();
~Product();
};
Product::Product()
{
ifstream input("E:\\Product.txt");
if(input.fail())
{
cerr<<"can not open the file of Produce"<
}
while(!input.eof())
{
input>>ProductName>>MatName>>MatPrice>>ServicePrice;
}
input.close();
}
void Product::CalSalePrice()
{
SalePrice=MatPrice*1.5+ServicePrice*2;
}
Product::~Product()
{
ofstream output("E:\\Output.txt");
if(output.fail())
{
cerr<<"can not open the file of Output"<
}
output<
int main()
{
Product object;
object.CalSalePrice();
return 0;
}
文件的路径我定义的是在E盘中,你自己可以更改。
同时,在文件中不同的名字之间要用空格隔开。
建议:我觉得你的程序中的变量(关于价格的变量)最好定义为浮点型的,在程序我是按照你的要求定义为整型的。你自己如果觉得我提的好可以自己改一下。