Operator overloading


Using Operator overloading we can overload operators to work with the classes defined by us. For example: consider I have a class to store complex numbers. I may now overload the + – = operators to work with objects of my complex class the way these operators work with int or float data types. This enables me to add objects of complex data type using the + operator. Well its a lot like functions. The only difference I could figure out was that if you use operators instead of functions, you feel like the code is more abstracted. Moreover I think it’s really cool. But I’m sure that there must have been a more beautiful goal behind operator overloading.

Note that just like a function an operator also has to be called by an object.

I studied operator overloading using the following program.


// to study operator overloading by implementing a class on coordinates of a point in space
#include <iostream>
using namespace std;

class coords {
int x,y,z;
public:
coords() {
x=y=z=0;
}
coords( int a , int b , int c ) {
x=a;
y=b;
z=c;
}
void print() {
cout << "(" << x << "," << y << "," << z << ")" ;
}
coords operator+ (coords op2) {
coords temp;
temp.x = x + op2.x;
temp.y = y + op2.y;
temp.z = z + op2.z;
return temp;
}
coords operator-() {
x=-x;
y=-y;
z=-z;
return *this;
}
};

int main() {
coords p1(10,20,30);
cout << "p1.print()\t\t" ;
p1.print();
cout << endl;
coords p2(1,2,3);
cout << "p2.print()\t\t" ;
p2.print();
cout << endl;
coords p3;
cout << "p3.print()\t\t" ;
p3.print();
cout << endl;
p3 = p1 + p2;
cout << "p3 = p1 + p2" << endl;
cout << "p3.print()\t\t" ;
p3.print();
cout << endl;
p3 = - p3;
cout << "p3 = - p3" << endl;
cout << "p3.print()\t\t" ;
p3.print();
cout << endl;
p3 = p1 + p2 + p3;
cout << "p3 = p1 + p2 + p3" << endl;;
cout << "p3.print()\t\t" ;
p3.print();
cout << endl;
return 0;
}

Output:


p1.print()        (10,20,30)
p2.print()        (1,2,3)
p3.print()        (0,0,0)
p3 = p1 + p2
p3.print()        (11,22,33)
p3 = - p3
p3.print()        (-11,-22,-33)
p3 = p1 + p2 + p3
p3.print()        (0,0,0)

Arduino Workshop


Arduino_uno_board
Arduino UNO

Arduino is an electronics platform which is meant for artists, students and hobbyists. It is extremely easy to use. Arduino was created at Interaction Design Institute Ivrea, Italy by Massimo Banzi and Casey Reas. It’s hardware and software is open source. This is the first time I’m hearing about open source hardware. It means that the layout and schematics of the hardware is available to the user. This means that anyone can make their own arduino board after refering the original schematics. I attended a workshop on arduino at Ettimadai Campus of Amrita Vishwa Vidyapeetham, thats how I know about it. Arduino is programmed using simple C language. Its IDE came bundled with a lot of examples to make anyone feel homely. Arduino UNO uses the ATmega328P microcontroller.

The workshop was conducted by Ajith Peter sir. He is an electronics enthusiast. He has a lot of sensors and shields. He let us play with all the cool stuff we had. He is really friendly. He is presently doing his PhD in IIT-B. The workshop was conducted by Dept of Computational Engineering and Networking. CEN’s HOD, Soman sir also presided over the workshop. He is an expert in maths. A talk with him helped me to visualize the first and second semester maths geometrically. I had no idea what they meant. He helped me relate it to the simple high school maths.

InduinoX Board
InduinoX Board

We did a lot of stuff with Arduino. The first program we did was to make the LED on the board blink. Ajith sir explained the importance of this simple piece of code. It is the equivalent of the “Hello World!” program which every programmer begins with. Arduino doesn’t come bundled with any in-built output devices, so debugging is a problem. This is where the simple blink comes to the rescue. You can insert the blink code between modules of your program to check their functionality. We played with a lot of sensors. We used the Passive Infra Red(PIR) sensor to build a motion detector. Ultrasonic range finder to findto any obstruction in front of the device. Then we used GPS module to get gps data and used the serial monitor to display the data. We even connected a LCD display to the arduino and learnt how to use it. We learnt about serial communication, UART (Universal Asynchronous Receiver Transmitter), I2C protocol. Ajith sir also showed us how to build a circuit board using toner transfer and told us about the importance of spacing and thickness of lines on a circuit board. He introduced us to softwares like Eagle CADsoft and fritzing which maybe used to build schematics of various circuits and then create the circuit board using toner transfer.

Arduino with music shield
Arduino with music shield
Ultrasonic sensor and PIR sensor
Ultrasonic sensor and PIR sensor
GPS module connected to Arduino
GPS module connected to Arduino

The workshop was fun-filled and at the same time very informative. We’re planning to conduct a similar workshop in Amritapuri campus.