//box.h
程序代码:
#ifndef BOX_H_
#define BOX_H_
class Box
{
private:
std::string number;
int n;
public:
Box();
Box(const std::string & co, int num = 0);
~Box();
void buy(int num);
void sell(int num);
void update(int num);
void show() const;
};
#endif
//box.cpp
程序代码:
#include <iostream>
#include <string>
#include "Box.h"
Box::Box()
{
number = "error";
n = 0;
}
Box::Box(const std::string & co,int num)
{
number = co;
n = num;
}
Box::~Box()
{
}
void Box::buy(int num)
{
n += num;
}
void Box::sell(int num)
{
n -= num;
}
void Box::show() const
{
std::cout << "箱号: " << number << std::endl;
std::cout << "数量: " << n << std::endl;
}
//usebox.cpp
程序代码:
#include <iostream>
#include <string>
#include "Box.h"
int main()
{
std::cout << "1号箱,初值2" << std::endl;
Box box_1("1",2);
box_1.show();
std::cout << "1号箱,加数值5" << std::endl;
box_1.buy(5);
box_1.show();
std::cout << "55号箱" << std::endl;
Box box_55("55");
box_55.show();
std::cout << "55号箱,加数值60" << std::endl;
box_55.buy(60);
box_55.show();
std::cout << "55号箱,减数值15" << std::endl;
box_55.sell(15);
box_55.show();
system("pause");
return 0;
}
ps:弱类弱代码