Skip to main content

Posts

Showing posts from August, 2007

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

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 oper

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&

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

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

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 ) {

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( i

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' whic

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.   If you're new to C++, read this article on the basic structure of a CSS program . 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 frie

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++

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 operat

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() {

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 // o

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 c

Overloading Increment/Decrement Operators

In this article we are going to overload the increment (++) and decrement (--) operators by using operator overloading . As increment (++) and decrement (--) are unary operators , therefore the operator functions that we need to define won’t take any arguments. These operators are overloaded as usual so further discussion is not required and we straightaway look at the example program: // overloading the increment // and decrement operators #include <iostream.h> // class class myclass { int a; public : myclass( int ); void show(); void operator ++(); void operator --(); }; myclass::myclass( int x) { a=x; }; void myclass::show() { cout<<a<<endl; } void myclass:: operator ++() { // increment a a++; } void myclass:: operator --() { // decrement a a--; } // main void main() { myclass ob( 10 ); ob.show(); ++ob; ob.show

What is 'this' Pointer?

Have a look at the following code: // program to illustrate 'this' // pointer #include <iostream.h> // class class myclass { int a; public : myclass( int ); void show(); }; myclass::myclass( int x) { // same as writing a=10 this ->a=x; }; void myclass::show() { // same as writing cout<<a; cout<< this ->a; } // main void main() { myclass ob( 10 ); ob.show(); } Now look at the awkward looking lines: this ->a=x; and cout<< this ->a; As you can see ‘this’ looks like a pointer to an object which is neither declared neither as a local nor as a global variable. So how the member function is using it? Actually ‘this’ is a special argument which is passed implicitly to every member function. It points to the specific object of the class that generated the call to that function. So if we have the following line of code: myclass ob1, ob2;

Introduction to Operator Overloading in C++ II

From the previous article Introduction to Operator Overloading in C++ , we know that we can overload operators by defining member functions having special names. The general form, as you know, for overloading operators is: ret-type operator#(arg-list); Although, you’re free to use any return type as ret-type but commonly it’ll be the name of the class itself; in this article we’ll be closely examining why is it so and what would happen if we use other return types. You may not expect further theories in this article as all the further discussion is in the program as comments. I request you to read the program thoroughly so you may understand what we’re discussing. // Example program to illustrate // operator overloading with diff rent // return-types used in the overload // function #include <iostream.h> // class class myclass { int a; public : // default constructor myclass(){} // constructor myclass(