Overload + operator to add two Pool objects. - CSharp Custom Type

CSharp examples for Custom Type:Operator Overloading

Description

Overload + operator to add two Pool objects.

Demo Code

using System;//from  w  w  w.j  a v  a 2  s  . c o m
class Pool {
   private double length;   // Length of a box
   private double width;  // Breadth of a box
   private double height;   // Height of a box
   public double getVolume() {
      return length * width * height;
   }
   public void setLength( double len ) {
      length = len;
   }
   public void setBreadth( double bre ) {
      width = bre;
   }
   public void setHeight( double hei ) {
      height = hei;
   }
   public static Pool operator+ (Pool b, Pool c) {
      Pool box = new Pool();
      box.length = b.length + c.length;
      box.width = b.width + c.width;
      box.height = b.height + c.height;
      return box;
   }
}
class Tester {
   static void Main(string[] args) {
      Pool Pool1 = new Pool();   // Declare Pool1 of type Pool
      Pool Pool2 = new Pool();   // Declare Pool2 of type Pool
      Pool Pool3 = new Pool();   // Declare Pool3 of type Pool
      double volume = 0.0;    // Store the volume of a box here
      Pool1.setLength(6.0);
      Pool1.setBreadth(7.0);
      Pool1.setHeight(5.0);
      Pool2.setLength(12.0);
      Pool2.setBreadth(13.0);
      Pool2.setHeight(10.0);
      volume = Pool1.getVolume();
      Console.WriteLine("Volume of Pool1 : {0}", volume);
      volume = Pool2.getVolume();
      Console.WriteLine("Volume of Pool2 : {0}", volume);
      Pool3 = Pool1 + Pool2;
      volume = Pool3.getVolume();
      Console.WriteLine("Volume of Pool3 : {0}", volume);
   }
}

Result


Related Tutorials