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)

Laplace Transforms


I’ve been finding Laplace transforms very difficult. So finally after a lot of procrastination, I’m actually studying it. I guess there would be many people out there in same situation as me. This article is intended for such people who wants to get basic idea about it in very short time.

This is what I understood from class.

Ok this is the equation. But I never understood what it meant until today. Laplace transforms let you convert functions dependent on time to functions dependent on frequency.  Once you get the physical idea, it starts to unfold itself. Here, f(t) is the function based on time and F(s) is the function based on frequency. I used the following links to understand the concepts. Hope you also find them useful.

Khan Academy‘s video lectures on Laplace transforms further enforced the ideas [Link].

The wikipedia page for had a table of Laplace transforms which i found quite useful [Link].

There is a better table available at Interactive mathematics [Link].

The next topic which I found difficult was Laplace transforms on unit step functions [Link].

And this site helped me with Laplace transform of derivatives.

Why use Laplace transform?

I still haven’t figured out this part completely. If you have figured out this part, please comment. I think understanding this part will help develop more interest in the subject. A lil googling told me that they are used in analysing circuits. But I still didn’t understand the how part. I think Laplace transforms are used to study the effect of change in frequency of voltage or current source in circuits where current or voltage varies with time.