pr12006.cpp
来自「c++编程宝典源码及Quincy99编译器 是《标准C++编程宝典》电子工业出」· C++ 代码 · 共 42 行
CPP
42 行
////////////////////////////////////////
// File Name: pr12006.cpp
////////////////////////////////////////
#include <iostream>
#include <cstring>
////////////////////////////////////////
// The ItemQty class.
////////////////////////////////////////
class ItemQty
{
int onhand;
char desc[25];
public:
ItemQty(int oh, char *d)
{ onhand = oh; std::strcpy(desc, d); }
void display() const
{ std::cout << '\n' << desc << ": " << onhand; }
// Overloaded unary - operator.
int operator-() const
{ return -onhand; }
};
////////////////////////////////////////
// The main() function.
////////////////////////////////////////
int main()
{
ItemQty item1(100, "crankshaft");
ItemQty item2(-50, "driveshaft");
item1.display();
std::cout << '\n' << -item1; // invoke the overloaded -
item2.display();
std::cout << '\n' << -item2; // invoke the overloaded -
return 0;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?