Open In App

Operator Overloading in Dart

Last Updated : 01 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Dart is an Object-oriented language and is quite similar to that Java Programming. Dart is extensively used to create single-page websites and web applications. The best example of a dart application is Gmail. We’ll take a look at features of Dart that are useful in some situations but aren’t crucial when using Dart, and you’ll probably not use it on a daily basis. Operator overloading gives you the ability to add custom implementations to a certain set of operators in your classes.

For More, Refer to Dart Tutorial.

These operators can be:

  • Arithmetic operators like + – * etc.
  • Equality operators like == and !=
  • Relational operators, > >= < and <=
  • and many more

For More, Refer to Operators in Dart.

Operator Overloading

As the name suggests, operator overloading means giving special meanings that use them to add user-defined data types as in C++ which is an operation on objects. Similarly, dart also has this operator overloading feature. Hence, it helps add objects, perform division of objects, etc.

Basically, operator overloading means that we give more meaning to operators that is as we know that operators are used to performing different operations on variables but with operator overloading, we can also add user-defined datatypes that are objects.

For doing so in dart we need to use the operator function and in which we can declare various operations to be done when this operator is used with objects. After that, we need to declare objects in the main function and these are declared using a new keyword.

The Syntax for Object Declaration:

class_name object_name = new class_name();

The Syntax for Operator Function:

class_name operator operator_symbol(argument) {
    // statements
}

Example of How to Use Objects:

Dart




import 'dart:io';
/* we need to import dart:io */
  
class arithmeticOperator {
      /* creation or starting of class */
    double a = 0;
      
    void input() {
        print('Enter number\n');
        a = double.parse(stdin.readLineSync()!);            
    }
    
      void display(){
        print('$a\n');
      }
}
  
void main() {
      arithmeticOperator b = new arithmeticOperator();
      b.input();
      b.display();
}


Example of Operator Overloading of ‘+’ Operator:

Dart




import 'dart:io';
  
class arithmeticOperator {
        
      double a = 0;
        
      void input() {
        print('Enter number\n');
        a = double.parse(stdin.readLineSync()!);
      }
    
      void display() {
        print('$a\n');
      }
    
      arithmeticOperator operator + (arithmeticOperator b) {
        /* with operator function of classarith return type we can use
        '+' operaator to add two variables of data type arith */
        arithmeticOperator c = new arithmeticOperator();
        c.a = b.a + a;
        return c;         
      }
}
  
void main() {
    arithmeticOperator b = new arithmeticOperator();
      /* creation of variables of data type arith using new keyword */
      arithmeticOperator c = new arithmeticOperator();
      arithmeticOperator d = new arithmeticOperator();                                  
      b.input();
      c.input();
      d = b + c;
      /* here the control goes to operator function */
      s3.display();
}


Note: Not just Operator Overloading, Dart also supports Function Overloading. Hence, there are some features of C++ that Dart also supports but its Compiling Speed is Faster than the C++ Compiler.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads