Add css styles for gwt widgets

Adding Cascading styles (CSS) to Google Web Toolkit (GWT) widgets is much simpler and involves only two steps.
  1. Style name
    • set style name
      for a widget using the $widgetInstance$.setStyleName() method or
    • stick with the default
      style name of the widget (use for setting global values)
      default style name examples:
      • for buttons: .gwt-Button
      • for Check Boxs: .gwt-CheckBox

  2. CSS style rule
    • Add CSS style rules to a .css file and import that into the html page or

    • write those inside the html page itself. (not recommended)

Let us provide you with an example which would create buttons shown below.


Coding in your java class:
Button cancelButton = new Button("Cancel");
Button loginButton = new Button("Login");
loginButton.setStyleName("buttons");

CSS rules:
.gwt-button {
background: #EEEEFF;
color: #0000CC;
font-size: 12px;
}

.buttons {
background: #CCCCCC;
color: #333333;
font-size: 12px;
}

GWT releases 1.4 and is no longer beta

Google Web Toolkit (GWT) has released a new version, 1.4. With over 1 million downloads GWT is moving forward well; and it's no longer beta. The user group is really active and well established; so if you haven't tried GWT yet, it's time to have a look.

GWT LogoRead more about GWT and news release at GWT blog.

For any issues visit the GWT user group here.

Overloading [] Operator

Have a look at the following code fragment:


  myclass a(3);

cout<<a[0];

Doesn’t it look awkward!


Yes it does, because we have overloaded he [] operator and given it some special
meaning.


In C++, it is possible to overload the [] operator
and give it a different meaning
rather then the usual object indexing.


The general form for overloading [] operator is:


  ret-type operator[](int);

It is considered a binary operator hence when declared as a member, it accepts
one explicit argument (usually int).


Although you are free to accept any type of argument but sticking to the original
concept of indexing, it would always be an integer.


ob[i];


When the compiler encounters the above expression (with the [] operator overloaded)
the [] operator
function
is called as below:


ob.operator[] (1)


The argument ‘1’ is passed explicitly while ‘ob’ is
passed implicitly using the ‘this’
pointer.


Enough discussion, now lets get on to some action!


Below is the example program illustrating the overloading of [] operator:



// Example Program illustrating
// the overloading of [] operator
#include <iostream.h>

class myclass
{
// stores the number of element
int num;
// stores the elements
int a[10];

public:
myclass(int num);

int operator[](int);
};

// takes the number of element
// wished to be entered.(<=10)
myclass::myclass(int n)
{
num=n;
for(int i=0;i<num;i++)
{
cout<<"Enter value for element "<<i+1<<":";
cin>>a[i];
}
}

int myclass::operator[](int index)
{
// if not out of bound
if(index<num)
return a[index];

// else
return NULL;
}

void main()
{
myclass a(3);

cout<<a[0]<<endl;
cout<<a[2]<<endl;

// out of bound
cout<<a[3]<<endl;
}


Related Articles:


Would Google Adsense let a website earn $100,000 per month?

With Google Adsense earning $100,000 per month!!! Feels like day dreaming? Are there web sites earning around 100k dollars per month; shockingly and interestingly the answer is yes. There are some few sites that earn this much; even more than that.

Believe me

A social site named "PlentryOfFish" owned by "Markus" has earned more than $900k in two months while "ShoeMoney" blog earned more than $100k in one month. See the proofs here and here. So the good news is we should try now and reach there since it is possible.

Analysis of sites earning with Adsense

  • Monthly visitors
    Number of visitors is a huge factor considering the Adsense revenue as visitors help revenue increase; so having a healthy visitors base is a must.

  • Adsense ads placement
    Many sites have been using text/image advertisements rather than going for text only ads or image only ads to change what user sees at first. Adsense text only ads are placed before/inside/after articles; but 250x250 / 300x250 ad formats are mainly used inside articles which would attract reads attention easily and quickly. Also one more point to note is the placement of 160x600 wide Skyscraper advertisements on to left or right bar running parallel to article content. Google Adsense Ad units are not much seen. One important point to note is the color matching of the ads with site content.

How can you reach there

Since every website or blog is unique, suitable adsense ad type, colors, sizes and places differ from each other; also whether to choose image only/text only or text and image also differ. Must choose between rounded corners(LINK) or not; depending on used web template. Best approach is to experiment many combinations of these and decide the best matching format. Adsense channels become handy in testing ads; as each ad placement can be assigned a unique channel which ease the experimenting and lead to accurate decisions. As an example; one channel may be named "leftBar.468x60.Pal3.21092007"; as the name suggests all features of used format, analysis would be really easy. Between one to two months will be enough to judge one advertisement format.

While experimenting for best Adsense ad formats, one important point to note is " publishing valuable contents frequently is a must".

100 and Counting...

This is to gladly inform all my readers that the number of articles
on Learning
Computer Programming
has reached the three figures.



When I first started this blog, I used to look at how other
blogs reached so many posts and I used to dream that I could, one day, be able
to reach that.



Today is that day!



Having written a hundred articles might not be very big achievement
considering the fact that a few blogs have over a thousand posts, but to me
its a great pleasure. All due to the love and response I have been getting from
you all. (Thanks guys)



There is nothing much left to say except a BIG ‘Thank
You’ to all of you reading this!



-Arvind Gupta

Adding Flexibility to Operators while Overloading them

  class_name class_name::operator+(int x)
{
class_name temp;

temp.a = a + x;
temp.b = b + x;

return temp;
}


With reference to the above operator
function
and supposing that ‘ob’ is an object of the class
to which this function belongs; Is the statement below legal:


ob2 = ob1 + 100;


Yes, but what about the following statement:


ob2 = 100 + ob1;


Surely this won’t work!


100 is an integer constant and has no ‘+’ operator that could add
it with user-defined types (object ‘ob’).


This certainly is a shortcoming, since often we don’t really care which
operand is where (as in addition and multiplication) and we order the operands
as necessary (as in subtraction and division).


To overcome this we can overload two-two versions of these operators
as friend
, one for ‘integer + object’ type and the other
for ‘object + integer’ type.


So, for example for addition we have to overload the ‘+’ operator
twice as below:


  friend class_name operator+(class_name,int);
friend class_name operator+(int,class_name);

Similarly we have to overload other operators.


The program below illustrates this concept:



// Program to illustrate
// the overloading of
// flexible addition
// and subtraction
// operators
#include <iostream.h>

class myclass
{
int a;
int b;

public:
myclass(){}
myclass(int x,int y){a=x;b=y;}
void show()
{
cout<<a<<endl<<b<<endl;
}

// object + int form
friend myclass operator+(myclass,int);
friend myclass operator-(myclass,int);

// int + object form
friend myclass operator+(int,myclass);
friend myclass operator-(int,myclass);
};

myclass operator+(myclass ob,int x)
{
myclass temp;

temp.a = ob.a + x;
temp.b = ob.b + x;

return temp;
}

myclass operator-(myclass ob, int x)
{
myclass temp;

temp.a = ob.a - x;
temp.b = ob.b - x;

return temp;
}

myclass operator+(int x, myclass ob)
{
// does the same thing
// because in addition
// it doesn't matters
// which operand is where
myclass temp;

temp.a = x + ob.a;
temp.b = x + ob.b;

return temp;
}

myclass operator-(int x, myclass ob)
{
myclass temp;

temp.a = x - ob.a;
temp.b = x - ob.b;

return temp;
}

void main()
{
myclass a(10,20);
myclass b(100,200);

a=a + 10;
a.show();

b=100 + b;
b.show();
}


Related Articles:


Using Friends to Overload all the Overloaded Arithmetic Operators

In the article Class
with all the Overloaded Arithmetic Operators
, we overloaded (almost)
all the arithmetic operators in one program, similarly in this article we’ll
be overloading them once again but now using friend functions.


We have already overloaded similar operators before (using friend
functions
), so you won’t be having any troubles in understanding
the program below:



// Program that Overloads
// all the arithmetic operators
// using friend functions
#include <iostream.h>

class myclass
{
int a;
int b;

public:
myclass(){}
myclass(int x,int y){a=x;b=y;}
void show()
{
cout<<a<<endl<<b<<endl;
}

// declared as friend
friend myclass operator+=(myclass&, myclass);
friend myclass operator-=(myclass&, myclass);

friend myclass operator++(myclass&);
friend myclass operator--(myclass&);

friend myclass operator++(myclass&, int);
friend myclass operator--(myclass&, int);

friend myclass operator+(myclass,myclass);
friend myclass operator-(myclass,myclass);

friend myclass operator/(myclass, myclass);
friend myclass operator*(myclass, myclass);
friend myclass operator%(myclass, myclass);
};

myclass operator+=(myclass &ob1, myclass ob2 )
{
ob1.a+=ob2.a;
ob1.b+=ob2.b;

return ob1;
}

myclass operator-=(myclass &ob1, myclass ob2 )
{
ob1.a-=ob2.a;
ob1.b-=ob2.b;

return ob1;
}

myclass operator++(myclass &ob)
{
ob.a++;
ob.b++;

return ob;
}

myclass operator--(myclass &ob)
{
ob.a--;
ob.b--;

return ob;
}

myclass operator++(myclass &ob, int x)
{
myclass temp;
temp=ob;

ob.a++;
ob.b++;

return temp;
}

myclass operator--(myclass &ob, int x)
{
myclass temp;
temp=ob;

ob.a--;
ob.b--;

return temp;
}

myclass operator+(myclass ob1,myclass ob2)
{
myclass temp;

temp.a = ob1.a + ob2.a;
temp.b = ob1.b + ob2.b;

return temp;
}

myclass operator-(myclass ob1,myclass ob2)
{
myclass temp;

temp.a = ob1.a - ob2.a;
temp.b = ob1.b - ob2.b;

return temp;
}

myclass operator/(myclass ob1,myclass ob2)
{
myclass temp;

temp.a = ob1.a / ob2.a;
temp.b = ob1.b / ob2.b;

return temp;
}

myclass operator*(myclass ob1,myclass ob2)
{
myclass temp;

temp.a = ob1.a * ob2.a;
temp.b = ob1.b * ob2.b;

return temp;
}

myclass operator%(myclass ob1,myclass ob2)
{
myclass temp;

temp.a = ob1.a % ob2.a;
temp.b = ob1.b % ob2.b;

return temp;
}

void main()
{
myclass a(10,20);
myclass b(100,200);

a+=b;
a.show();

a++;
a.show();

a=a*a;
a.show();

a=a%b;
a.show();
}


Related Articles:


Problems on Operator Overloading II

This is the second part of the artcile Problems
on Operator Overloading I
.


Problem #4: What would be the
output of the following code:



1 // Problem #4:
2 // Problem related to
3 // Operators Overloading
4 #include <iostream.h>
5
6 class myclass
7 {
8 int a;
9 int b;
10
11 public:
12 myclass(){}
13 myclass(int x,int y){a=x;b=y;}
14 void show()
15 {
16 cout<<a<<endl<<b<<endl;
17 }
18
19 friend myclass operator++(myclass);
20 };
21
22 myclass operator++(myclass ob)
23 {
24 ob.a++;
25 ob.b++;
26
27 return ob;
28 }
29
30 void main()
31 {
32 myclass a(10,20);
33
34 ++a;
35
36 a.show();
37 }

Problem #5: What would be the
output of the following code:



1 // Problem #5:
2 // Problem related to
3 // Operators Overloading
4 #include <iostream.h>
5
6 class myclass
7 {
8 int a;
9 int b;
10
11 public:
12 myclass(){}
13 myclass(int x,int y){a=x;b=y;}
14 void show()
15 {
16 cout<<a<<endl<<b<<endl;
17 }
18
19 friend myclass operator+=(myclass,
20
21 myclass);
22 };
23
24 myclass operator+=(myclass ob1, myclass ob2 )
25 {
26 ob1.a+=ob2.a;
27 ob1.b+=ob2.b;
28
29 return ob1;
30 }
31
32 void main()
33 {
34 myclass a(10,20);
35 myclass b(100,200);
36
37 a+=b;
38 b+=a;
39
40 a.show();
41 b.show();
42 }

Problem #6: Is there any error(s)
in the following code:



1 // Problem #6:
2 // Problem related to
3 // Operators Overloading
4 #include <iostream.h>
5
6 class myclass
7 {
8 int a;
9 int b;
10
11 public:
12 myclass(){}
13 myclass(int x,int y){a=x;b=y;}
14 void show()
15 {
16 cout<<a<<endl<<b<<endl;
17 }
18
19 myclass operator++(myclass &);
20 };
21
22 myclass myclass::operator++(myclass &ob)
23 {
24 ob.a++;
25 ob.b++;
26
27 return ob;
28 }
29
30 void main()
31 {
32 myclass a(10,20);
33
34 ++a;
35
36 a.show();
37 }

ANSWERS:




  1. 10 20, Refer to this
    article
    .




  2. 10 20 100 200. Same reason as above.



  3. Has errors inline no. 19, '++' is a unary operator and hence doesn't need
    any other argument.


Related Articles:


Problems on Operator Overloading I

Here I'm listing some problems related to Operator Overloading to spice up
the discussion a bit. This is a TWO part series so read the next
article
after solving each of the problems listed here.



Problems
on Operator Overloading I


Problems
on Operator Overloading II



Problem #1: Point out the error(s)
if any in the following code:



1 // Problem #1:
2 // Problem related to
3 // Operators Overloading
4 #include <iostream.h>
5
6 class myclass
7 {
8 int a;
9 int b;
10
11 public:
12 myclass(){}
13 myclass(int x,int y){a=x;b=y;}
14 void show()
15 {
16 cout<<a<<endl<<b<<endl;
17 }
18
19 myclass operator+(int);
20 };
21
22 myclass myclass::operator+(int x)
23 {
24 myclass temp;
25
26 temp.a=a + x;
27 temp.b=b + x;
28
29 return temp;
30 }
31
32 void main()
33 {
34 myclass a(10,20);
35
36 a=a+10;
37 a.show();
38 }

Problem #2: Point out the error(s)
if any in the following code:



1 // Problem #2:
2 // Problem related to
3 // Operators Overloading
4 #include <iostream.h>
5
6 class myclass
7 {
8 int a;
9 int b;
10
11 public:
12 myclass(){}
13 myclass(int x,int y){a=x;b=y;}
14 void show()
15 {
16 cout<<a<<endl<<b<<endl;
17 }
18
19 int operator=(int);
20 };
21
22 int myclass::operator=(int x)
23 {
24 a=x;
25 b=x;
26
27 return x;
28 }
29
30 void main()
31 {
32 myclass a(10,20);
33 myclass b,c;
34
35 c=b=a;
36
37 c.show();
38 }

Problem #3: Point out the error(s)
if any in the following code:



1 // Problem #6:
2 // Problem related to
3 // Operators Overloading
4 #include <iostream.h>
5
6 class myclass
7 {
8 int a;
9 int b;
10
11 public:
12 myclass(){}
13 myclass(int x,int y){a=x;b=y;}
14 void show()
15 {
16 cout<<a<<endl<<b<<endl;
17 }
18
19 myclass operator++();
20 };
21
22 myclass myclass::operator++()
23 {
24 a++;
25 b++;
26
27 return this;
28 }
29
30 void main()
31 {
32 myclass a(10,20);
33
34 ++a;
35
36 a.show();
37 }

ANSWERS:




  1. No Errors




  2. No Errors



  3. We need to return the object and not the pointer
    (this)
    . The statement should have been, return *this;


Related Articles:


Overloading the Short-Hand Operators (+= and -=) using Friend Functions

In this article we’re going to overload the shorthand
addition (+=) and subtraction (-=) operators
using friend
functions.


As you can observe in the program below, the operator functions are taking
the first argument (operand) as a reference
(call by reference). This is due the fact that these operators need to alter
the data of the actual operand itself.


This is similar to the case of increment/decrement
operators
(click for detailed information).



// Overloading the shorthand
// += and -= operators using
// friend functions
#include <iostream.h>

class myclass
{
int a;
int b;

public:
myclass(){}
myclass(int x,int y){a=x;b=y;}
void show()
{
cout<<a<<endl<<b<<endl;
}

// declared as friend
friend myclass operator+=(myclass&, myclass);
friend myclass operator-=(myclass&, myclass);
};


myclass operator+=(myclass &ob1, myclass ob2 )
{
// data of the first operand
// are to be changed, so accepting
// it as a reference
ob1.a+=ob2.a;
ob1.b+=ob2.b;

return ob1;
}

myclass operator-=(myclass &ob1, myclass ob2 )
{
ob1.a-=ob2.a;
ob1.b-=ob2.b;

return ob1;
}

void main()
{
myclass a(10,20);
myclass b(100,200);

a+=b;
b+=a;

a.show();
b.show();
}


Related Articles:


Overloading Post-Fix Forms of ++ and -- Operators using Friend Functions

From the article Overloading
Post-Fix Forms of ++ and -- Operators
, we know that the postfix form
of the increment/decrement operator function takes two arguments, one is passed
implicitly
and the other as usual.


Its general form is:


  ret-type operator++(int);

As we know that when we overload operators as friends,
all the operands (arguments) are passed explicitly.


So, the general form for overloading postfix form of
increment/decrement operators
using friend functions should be (and
it really is) like this:


  ret-type operator++(class-name&, int);

Where the second int(eger) argument, as you know is a dummy variable and has
no use.


The following program illustrates this:



// Program to illustrate the overloading
// of increment / decrement operators
// as friends
// Overloads both prefix and postfix
// form
#include <iostream.h>

class myclass
{
int a;
int b;

public:
myclass(){}
myclass(int x,int y){a=x;b=y;}
void show()
{
cout<<a<<endl<<b<<endl;
}

// prefix form
friend myclass operator++(myclass&);
friend myclass operator--(myclass&);

// postfix form
friend myclass operator++(myclass&, int);
friend myclass operator--(myclass&, int);

};

// ---- PREFIX FORM ----
myclass operator++(myclass &ob)
{
ob.a++;
ob.b++;

return ob;
}

myclass operator--(myclass &ob)
{
ob.a--;
ob.b--;

return ob;
}
// ---- PREFIX FORM ENDS ----

// ---- POSTFIX FORM ----
myclass operator++(myclass &ob, int x)
{
myclass temp;
temp=ob;

ob.a++;
ob.b++;

return temp;
}

myclass operator--(myclass &ob, int x)
{
myclass temp;
temp=ob;

ob.a--;
ob.b--;

return temp;
}
// ---- POSTFIX FORM ENDS ----

void main()
{
myclass a(10,20);
myclass b(100,200);

++a;
(b++).show();
b.show();

a.show();
b.show();

a=b++;

a.show();
b.show();
}


Related Articles:


Overloading Increment/Decrement Operators using Friend Functions

In the article Operator
Overloading using Friend Functions
, we saw how we can overload simple
operators using friend functions, in the other article Overloading
Increment/Decrement Operators
, we saw the method of overloading increment/decrement
operators as member functions. Combining both these, we’ll try to overload
the increment/decrement operators using friend
functions
, in this article.


As we know there are some differences in overloading operators as a friend.


Increment/decrement are the type of operators that need to change the operands
itself. In the case of operator overloading as member functions, ‘this’
pointer
was passed so any change done would result in the operand itself
getting changed. But in the case of friend functions, operands are passed explicitly
and that also as call by value, hence it is impossible to change the operand
that way.


Let’s take an example, suppose we have an object ‘ob’of a
class say 'myclass' which overloads the increment operator,


  ob++;

This statement would result in a call to the overloaded increment (++) operator
function, which could be something like the one below:


  myclass operator++()
{

a++;
b++;

return *this;
}

Supposing the data of the class we need to alter are a and b, the above statement
would increment the data of the object that generated the call, hence the desired
result is achieved.


Had the function been overloaded as a friend something like this:


  myclass operator++(myclass ob)
{
ob.a++;
ob.b++;

return ob;
}


Then the operand’s copy (not the actual object that generated the call)
would have been passed to the function and any change (a++ and b++) would only
have incremented the data of the copy of the object passed (call by value) and
the data of the operand would have remained unchanged.


To overcome this we need to accept the reference
of the object (call by reference) as the argument to overload the operators
as a friend.


The following program illustrates this method:



// Program to illustrate the overloading
// of increment / decrement operators
// as friends
// NOTE: Prefix form is overloaded here
#include <iostream.h>

class myclass
{
int a;
int b;

public:
myclass(){}
myclass(int x,int y){a=x;b=y;}
void show()
{
cout<<a<<endl<<b<<endl;
}

// accepting references
friend myclass operator++(myclass&);
friend myclass operator--(myclass&);

};

myclass operator++(myclass &ob)
{
ob.a++;
ob.b++;

return ob;
}

myclass operator--(myclass &ob)
{
ob.a--;
ob.b--;

return ob;
}

void main()
{
myclass a(10,20);
myclass b(100,200);

++a;
++b;

a.show();
b.show();

a=--b;

a.show();
b.show();
}


Related Articles:


Adsense Ads between blogger posts

If you have switched your blog from older blogger to new blogger platform (which uses layouts) and looking for adding adsense advertisements in between posts; Blogger has created a new widget for this purpose. And they have made it such that any non-tech blogger even can use this. This widget lets you specify number of posts between an adsense advertisement. You can see the full guide here at Get inline.

If you haven't switched to new blogger yet, don't worry. You still can put adsense ads between your posts. You have to put the adsense code inside the tags that shows up the blog post. My home page has done like that. If you need any assistance on that, just drop me a mail or write below as a comment. I would be happy to help you.

Operator Overloading using Friend Functions

In the article Introduction
to Operator Overloading in C++
, we discussed that there are two methods
by which operators can be overloaded, one using the member function and the
other by using friend
functions.


There are some differences between the two methods though, as well as there
are advantages for using friend functions to overload operators over member
functions.


In this article we’ll be overloading the simplest operators – and
+ using friend function. Previously we have seen that we need to accept only
one argument explicitly for binary operators and the other is passed implicitly
using the ‘this’
pointer.


From the article Friend
Functions of a Class
, we know that as friend functions are not members
of a class, they don’t have a ‘this’ pointer. So how the operands
are are passed in this case?


Simple, all the operands are passed explicitly to the friend operator functions.


There are other differences also but for this article (to overload + and –
operators) this is enough.


Here is the program:



// Using friend functions to
// overload addition and subtarction
// operators
#include <iostream.h>

class myclass
{
int a;
int b;

public:
myclass(){}
myclass(int x,int y){a=x;b=y;}
void show()
{
cout<<a<<endl<<b<<endl;
}

// these are friend operator functions
// NOTE: Both the operans will be be
// passed explicitely.
// operand to the left of the operator
// will be passed as the first argument
// and operand to the right as the second
// argument
friend myclass operator+(myclass,myclass);
friend myclass operator-(myclass,myclass);

};

myclass operator+(myclass ob1,myclass ob2)
{
myclass temp;

temp.a = ob1.a + ob2.a;
temp.b = ob1.b + ob2.b;

return temp;
}

myclass operator-(myclass ob1,myclass ob2)
{
myclass temp;

temp.a = ob1.a - ob2.a;
temp.b = ob1.b - ob2.b;

return temp;
}

void main()
{
myclass a(10,20);
myclass b(100,200);

a=a+b;

a.show();
}


Related Articles:


Problems Related to Operator Overloading

To make our ongoing discussion on Operator Overloading more interesting, here
I have listed some problems related to operator overloading.


Problem #1: Point out the errors(s)
if any, in the following program:



1 // Problem related to Operator
2 // overloading
3 #include <iostream.h>
4
5 class myclass
6 {
7 int a;
8
9 public:
10 myclass(int);
11 void show();
12
13 myclass operator ++();
14 myclass operator --();
15 };
16
17 myclass::myclass(int x)
18 {
19 a=x;
20 }
21
22 void myclass::show()
23 {
24 cout<<a<<endl;
25 }
26
27 myclass myclass::operator ++()
28 {
29 a++;
30
31 return *this;
32 }
33
34 myclass myclass::operator --()
35 {
36 a--;
37
38 return *this;
39 }
40
41 // main
42 void main()
43 {
44 myclass ob(10);
45
46 ob.show();
47
48 ob++;
49 ob.show();
50 }


Problem #2: Point out the errors(s)
if any, in the following program:



1 // Problem related to Operator
2 // overloading
3 #include <iostream.h>
4
5 class myclass
6 {
7 int a;
8 int b;
9
10 public:
11 myclass(int, int);
12 void show();
13
14 myclass operator=(myclass);
15 };
16
17 myclass myclass::operator=(myclass ob)
18 {
19 a=ob.a;
20 b=ob.b;
21 };
22
23 myclass::myclass(int x,int y)
24 {
25 a=x;
26 b=y;
27 }
28
29 void myclass::show()
30 {
31 cout<<a<<endl<<b<<endl;
32 }
33
34 // main
35 void main()
36 {
37 myclass ob(10,11);
38 myclass ob2(20,21);
39 myclass ob3(30,41);
40
41 int x,y,z;
42
43 x=y=z=10;
44 cout<<z<<endl;
45
46 ob=ob2=ob3;
47 ob.show();
48 }


Problem #3: Point out the errors(s)
if any, in the following program:



1 #include <iostream.h>
2
3 // class
4 class myclass
5 {
6 int a;
7
8 public:
9 myclass(int);
10 void show();
11
12 void operator ++();
13 void operator --();
14 };
15
16 myclass::myclass(int x)
17 {
18 a=x;
19 }
20
21 void myclass::show()
22 {
23 cout<<a<<endl;
24 }
25
26 void myclass::operator ++()
27 {
28 a++;
29 }
30
31 void myclass::operator --()
32 {
33 a--;
34 }
35
36 // main
37 void main()
38 {
39 myclass ob(10);
40 myclass ob2(100);
41
42 ob.show();
43 ob2.show();
44
45 ob2=++ob;
46 ob.show();
47 ob2.show();
48 }


Answers:




  1. Although no error is reported (MS Visual C++) but line number 48 has a
    problem because postfix version of the operators have not been overloaded.
    This way the compile uses prefix form instead besides giving warning.




  2. ERROR in line number 46. Here ob2=ob3 is first evaluated by the overloaded
    assignment operator as defined but it is not returning any value which can
    be further assigned to ob.



  3. ERROR in line number 45. Operator function is not returning anything which
    can be assigned to ob2.


Related Articles:


Class with all the Overloaded Arithmetic Operators

So far we have learnt to overload +, -, +=, -= etc. operators and we know what
is the basic theory behind operator overloading.


In this article we are going to design a program with a class that overloads
almost all the arithmetic operators (+, -, +=, -=, /, *, ++, --)


This is a program centric article, so we straightway have a look at the example
program.


Since nothing new has been introduced, I leave it up to you to understand everything
yourself.



// Example Program with a
// a class having almost
// all the arithmetic
// operators overloaded
#include <iostream.h>

class myclass
{
int a;
int b;

public:
myclass(){}
myclass(int,int);
void show();

myclass operator+(myclass);
myclass operator-(myclass);

// prefix
myclass operator++();
myclass operator--();

// postfix
myclass operator++(int);
myclass operator--(int);

myclass operator+=(myclass);
myclass operator-=(myclass);

myclass operator/(myclass);
myclass operator*(myclass);

};

myclass::myclass(int x,int y)
{
a=x;
b=y;
};

void myclass::show()
{
cout<<a<<endl<<b<<endl;
}

myclass myclass::operator+(myclass ob)
{
myclass temp;

temp.a=a + ob.a;
temp.b=b + ob.b;

return temp;
}

myclass myclass::operator-(myclass ob)
{
myclass temp;

temp.a=a - ob.a;
temp.b=b - ob.b;

return temp;
}

myclass myclass::operator++()
{
a++;
b++;

return *this;
}

myclass myclass::operator--()
{
a--;
b--;

return *this;
}

myclass myclass::operator++(int x)
{
myclass old;
old=*this;

a++;
b++;

return old;
}

myclass myclass::operator--(int x)
{
myclass old;
old=*this;

a--;
b--;

return old;
}

myclass myclass::operator+=(myclass ob)
{
a+=ob.a;
b+=ob.b;

return *this;
}

myclass myclass::operator-=(myclass ob)
{
a-=ob.a;
b-=ob.b;

return *this;
}

myclass myclass::operator/(myclass ob)
{
myclass temp;

temp.a=a / ob.a;
temp.b=b / ob.b;

return temp;
}

myclass myclass::operator*(myclass ob)
{
myclass temp;

temp.a=a * ob.a;
temp.b=b * ob.b;

return temp;
}

void main()
{
myclass ob1(10,20);
myclass ob2(100,200);

ob1+=ob2;

ob1.show();
ob2.show();

ob1=ob1/ob2;
ob1.show();

ob1=ob1*ob2;
ob1.show();

ob2.show();
}


Related Articles:


Overloading the Short-Hand Operators (+= and -=)

The short-hand addition (+=) and subtraction (-=) operators are very commonly
used and thus it would be nice if we overload them in classes. You would be
glad to know that there is nothing new or special that needs to be done to achieve
this, both the operators are overloaded as usual like other binary operators.


The short-hand operators combine the operation performed by two operators one
the addition or subtraction and the other the assignment. This is all it does
and this is all you need to know!


As nothing new has been introduced so we end the theory here and look at the
example:



// Program to illustrate the
// overloading of shorthand
// operators (+=, -=)
#include <iostream.h>

class myclass
{
int a;
int b;

public:
myclass(int,int);
void show();

myclass operator+=(myclass);
myclass operator-=(myclass);
};

myclass::myclass(int x,int y)
{
a=x;
b=y;
};

void myclass::show()
{
cout<<a<<endl<<b<<endl;
}

myclass myclass::operator+=(myclass ob)
{
a+=ob.a;
b+=ob.b;

// note this
return *this;
}

myclass myclass::operator-=(myclass ob)
{
a-=ob.a;
b-=ob.b;

// note this
return *this;
}

void main()
{
myclass ob1(10,20);
myclass ob2(100,200);

ob1+=ob2;

ob1.show();

// even this is possible
// because operator functions
// are retuning *this
ob2=ob1+=ob1;

ob2.show();
}


Related Articles:


GWT not working on Internet explorer 7 (IE7) giving "Element not found" javascript error

GWT web application started to give "Element not found" javascript error message on Internet Explorer 7 (IE7)? This application worked fine on Internet Explorer 6 and Firefox 2. Now your best guess would be; GWT not working on IE 7 properly. Wasn't it?

But the scenario became confusing and unbelievable because your application worked fine on IE7 in some machines while not on some others. Have you faced this issue? Then the below solution is for you.

This issue can be fixed by a making a change on windows registry.

Steps to follow are;
1. Open up the Registry editor - type regedit on command prompt.

2. Look for the key shown below
HKEY_CLASSES_ROOT\TypeLib\{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}\1.1\0\win32

3. Click on the "Default" row and see the value there. If it's value is "C:\WINDOWS\system32\shdocvw.dll", then that is what causes the above mentioned issue. If you have installed Windows in a different drive; C:\ must be replaced with the that letter.

4. Replace that value with "C:\WINDOWS\system32\ieframe.dll".

5. Now restart IE7, and load your GWT application.

Overloading the Assignment Operator (=)

We know that if we want objects of a class to be operated by common operators
then we need to overload
them
. But there is one operator whose operation is automatically crested
by C++ for every class we define, it is the assignment operator ‘=’.


Actually we have been using similar statements like the one below previously


  ob1=ob2;

where ob1 and ob2 are objects of a class.


This is because even if we don’t overload the ‘=’ operator,
the above statement is valid.


As I said C++ automatically creates a default assignment operator. The default
operator created, does a member-by-member copy, but if we want to do something
specific we may overload it.


The simple program below illustrates how it can be done. Here we are defining
two similar classes, one with the default assignment operator (created automatically)
and the other with the overloaded one. Notice how we could control the way assignments
are done in that case.



// Program to illustrate the
// overloading of assignment
// operator '='
#include <iostream.h>

// class not overloading the
// assignment operator
class myclass
{
int a;
int b;

public:
myclass(int, int);
void show();
};

myclass::myclass(int x,int y)
{
a=x;
b=y;
}

void myclass::show()
{
cout<<a<<endl<<b<<endl;
}

// class having overloaded
// assignment operator
class myclass2
{
int a;
int b;

public:
myclass2(int, int);
void show();

myclass2 operator=(myclass2);
};

myclass2 myclass2::operator=(myclass2 ob)
{
// -- do something specific --
// this is just to illustrate
// that when overloading '='
// we can define our own way
// of assignment
b=ob.b;

return *this;
};

myclass2::myclass2(int x,int y)
{
a=x;
b=y;
}

void myclass2::show()
{
cout<<a<<endl<<b<<endl;
}

// main
void main()
{
myclass ob(10,11);
myclass ob2(20,21);

myclass2 ob3(100,110);
myclass2 ob4(200,210);

// does a member-by-member copy
// '=' operator is not overloaded
ob=ob2;
ob.show();


// does specific assignment as
// defined in the overloaded
// operator definition
ob3=ob4;
ob3.show();
}


Related Articles:


Overloading Post-Fix Forms of ++ and -- Operators

In the article Overloading
Increment/Decrement Operators
, we overloaded the prefix from of the
increment and decrement operators. For that we defined the operator function
with the following general form:


  ret-type operator++(); 
ret-type operator--();

As there are two-two forms (prefix, postfix) of each of these operators while
operator
function
related with each can be only one, so to C++ has devised a
way to distinguish between the two forms.


The operator++() or operator—() functions are called as usual when prefix
form of operators are used but when postfix form is used then an integer argument
0 is passed to that function.


To catch those calls we must overload one more function for each, accepting
an integer as argument as below:


  ret-type operator++(int); 
ret-type operator--(int);

We don’t need to use the argument (0) passed since it is passed just
because we can define two overloaded
functions
for each operator and the compiler can call the respective function.


So whenever ++ob will be found operator++() will be called while when ob—is
found operator—(int) will be called.


The following program illustrates this:



// Overloading postfix and prefix
// forms of increment / decrement
// operators
#include <iostream.h>

// class
class myclass
{
int a;

public:
myclass(){}
myclass(int);

void show();

// prefix form
myclass operator++();
myclass operator--();

// postfix form
myclass operator++(int);
myclass operator--(int);
};

myclass::myclass(int x)
{
a=x;
};

void myclass::show()
{
cout<<a<<endl;
}

myclass myclass::operator++()
{
a++;

return *this;
}

myclass myclass::operator--()
{
a--;

return *this;
}

// postfix form
myclass myclass::operator++(int x)
{
// store the object that
// generated the call
myclass old;
old=*this;

a++;

// return object with old
// values
return old;

}

// postfix form
myclass myclass::operator--(int x)
{
// store the object that
// generated the call
myclass old;
old=*this;

a--;

// return object with old
// values
return old;
}

// main
void main()
{
myclass ob(10);
myclass ob2(100);

ob.show();

++ob;
ob.show();

ob2=ob++;
ob2.show();
ob.show();
}


Related Articles:


Check out this stream