⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 opoverld.cpp

📁 含有文章和源码
💻 CPP
字号:
                                       // Chapter 6 - Program 8
#include <iostream.h>

class box {
   int length;
   int width;
public:
   void set(int l, int w) {length = l; width = w;}
   int get_area(void) {return length * width;}
   friend box operator+(box a, box b);   // Add two boxes
   friend box operator+(int a, box b);   // Add a constant to a box
   friend box operator*(int a, box b);   // Multiply a box by a constant
};


box operator+(box a, box b)   // Add two boxes' widths together
{
box temp;
   temp.length = a.length;
   temp.width = a.width + b.width;
   return temp;
}


box operator+(int a, box b)   // Add a constant to a box
{
box temp;
   temp.length = b.length;
   temp.width = a + b.width;
   return temp;
}


box operator*(int a, box b)   // Multiply a box by a constant
{
box temp;
   temp.length = a * b.length;
   temp.width = a * b.width;
   return temp;
}


main()
{
box small, medium, large;
box temp;

   small.set(2, 4);
   medium.set(5, 6);
   large.set(8, 10);

   cout << "The area is " << small.get_area() << "\n";
   cout << "The area is " << medium.get_area() << "\n";
   cout << "The area is " << large.get_area() << "\n";

   temp = small + medium;
   cout << "The new area is " << temp.get_area() << "\n";
   temp = 10 + small;
   cout << "The new area is " << temp.get_area() << "\n";
   temp = 4 * large;
   cout << "The new area is " << temp.get_area() << "\n";
}




// Result of Execution
//
// The area is 8
// The area is 30
// The area is 80
// The new area is 20
// The new area is 28
// The new area is 1280

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -