Dart Programming

Dart Classes and Objects

Whenever we talk about object-oriented programming, the first thing that comes to our minds is a “class”. A class is a user-defined data structure that helps in carrying out the desired operations on user-defined data types as well as the primitive data types. Using the classes and objects in the Dart programming language is pretty much similar to the C programming language; however, in this article, we will explicitly discuss this concept of the Dart programming language in Ubuntu 20.04.

How to Use the Classes and Objects in Dart in Ubuntu 20.04?

To learn the usage of the classes and objects in the Dart programming language in Ubuntu 20.04, you will have to go through the following three examples:

Example # 1: Creating and Using the Student Class and its Associated Object in Dart

In this example, we will be creating the Student class in Dart and will access its member functions and variables with the help of its object. For that, we have implemented the Dart script shown in the image below:

In this example, we have created the Student class in Dart with the “class” keyword followed by the name of the class. Then, inside this class, we have defined three member variables, i.e. name, age, and roll_num. We have also created a member function of this class named displayStudentInfo(). This function does not have a return type, nor does it accept any parameters. Within this function, we are simply printing the values of the member variables on the terminal. Now, for accessing the member variables and member function of the Student class, we have created an object of this class, “S1”, within our “main()” function by using the “new” keyword. Then, we wanted to initialize all the member variables of this class.

For that, we have initialized these member variables one by one by accessing them with the object of the Student class that we have created. We have assigned random values to all three of these member variables. Then, we have just called the “displayStudentInfo()” function of the Student class with the help of its object for displaying all the information of the specified student on the terminal.

To run this Dart script, we have utilized the subsequent command:

$ dart run classes.dart

The information of the specified student is shown in the image below as the output of our Dart script:

Example # 2: Creating and Using the Electricity Bill Class and its Associated Object in Dart

In this example, we want to create a Dart class that will help us in calculating the electricity bill. We have implemented the following Dart script for this purpose:

In this example, we have first created the class named “ElectricityBill”. Then, we have defined the two-member variables of this class, i.e. unitsConsumed and the pricePerUnit that correspond to the total units of electricity consumed and the price of each unit, respectively. After that, we created a member function of this class named “calculateBill()”, which is meant for calculating the total electricity bill. Inside this function, we have created a variable named “bill” and have equalized it to the product of “unitsConsumed” and “pricePerUnit”. Then, we have used a “print” statement for printing the value of the “bill” variable, i.e. the total electricity bill on the terminal. After doing that, we have defined our “main()” function inside which we have first created an object of the “ElectricityBill” class named “EB”.

Then, with the help of this object, we have initialized the member variables of this class with dummy values. Finally, we have called the “calculateBill()” function by using the object of this class for calculating the total electricity bill according to the total units of electricity consumed and the price per unit.

The total electricity bill according to the passed parameters is shown in the image below:

Example # 3: Creating and Using the Employee Salary Class and its Associated Object in Dart

In this example, we wish to design a Dart class that can calculate the gross salary of an employee depending upon his/ her base salary and all the other allowances being provided to him/ her. For doing that, we have written the following Dart script:

In this example, we have created the Dart class named “EmployeeSalary”. Then, we have defined the two member variables of this class, i.e. baseSalary and otherAllowances. After that, we have defined a member function of this class named “grossSalary()”. Within this function, we have created a variable named “salary” and have equalized it to the sum of the “baseSalary” and “otherAllowances”.

After that, we printed the value of the “salary” variable, i.e. the gross salary of the employee on the terminal. Then, inside our “main()” function, we have created an instance of the “EmployeeSalary” class named “ES”, and using this object, we have initialized the member variables of our class. After that, we have just called the “grossSalary()” function of this class using its object for calculating and printing the gross salary of the employee on the terminal.

Upon execution of this Dart script, the gross salary of the employee was printed on the terminal as shown in the image below:

Conclusion

With this article, we wanted to enlighten you on the usage of the classes and objects in the Dart programming language in Ubuntu 20.04. To make these concepts easier for you, we explained them with the help of three different examples that revolved around three distinct classes. These classes made use of the functionalities such as simple printing, calculating sums, products, etc. By using these examples as a baseline, you can easily create your classes and their associated objects in the Dart programming language and can use them for achieving the desired functionality. You just need to know the basic method of doing so (as explained in this article), and you will be good to go.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.