Save info   Get password
Home Submit your blog Edit Account Rules RSS-Archive Contact


Overloading Increment/Decrement Operators
2007-08-15 01:42:00
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(); --ob; ob.show(
Read more: Increment

What is 'this' Pointer?
2007-08-15 01:39:00
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; ob2.show()
Read more: Pointer

Adding Flexibility to Operators while Overloading them
2007-08-26 01:29:00
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 &


Using Friends to Overload all the Overloaded Arithmetic Operators
2007-08-26 01:22:00
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); fri
Read more: Friends

Problems on Operator Overloading II
2007-08-24 08:58:00
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


Problems on Operator Overloading I
2007-08-24 08:56:00
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


Overloading the Short-Hand Operators (+= and -=) using Friend Functions
2007-08-23 08:25:00
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 fir
Read more: Short , Functions

Overloading Post-Fix Forms of ++ and -- Operators using Friend Functions
2007-08-23 08:23:00
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
Read more: Functions

Overloading Increment/Decrement Operators using Friend Functions
2007-08-23 08:19:00
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 cla


Overloading [] Operator
2007-08-29 02:11:00
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
Read more: Operator

100 and Counting...
2007-08-26 01:37:00
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
Read more: Counting

Overloading [] Operator II
2007-09-02 00:55:00
In the previous article Overloading [] Operator , we overloaded the [] operator in a class to access data within the class by indexing method. The operator [] function was defined as below: int myclass::operator[](int index) { // if not out of bound if(index<num) return a[index]; } As you can see, the above operator function is returning values, hence it could only be used on the right hand side of a statement. It’s a limitation! You very well know that a statement like below is very common with respect to arrays: a[1]=10; But as I said, the way we overloaded the [] operator, statement like the one above is not possible. The good news is, it is very easy to achieve this. For this we need to overload the [] operator like this: int &myclass::operator[](int index) { // if not out of bound if(index<num) return a[index]; } By returning a reference to the particular element, it is possible to use the index express


Some Operations on Matrix
2007-09-11 07:41:00
A few days back someone asked me a question via email which I thought might be useful to others too. So I’m listing that question along with its answer below. Q. I want to write a program such that users enter the value of matrix and each operation (listed below) is performed by functions. I want to use switch structure to call the functions. 1. Rotate the matrix around the diagonal. Example: 1 2 3 ---> 1 4 7 4 5 6 2 5 8 7 8 9 3 6 9 2. Rotate the matrix around the middle row. Example: 1 2 3 ---> 7 8 9 4 5 6 4 5 6 7 8 9 1 2 3 3. Rotate the matrix around the middle column. Example: 1 2 3 ---> 3 2 1 4 5 6 6 5 4 7 8 9 9 8 7 4. Set the upper triangle to zero. Example: 1 2 3 ---> 1 0 0 4 5 6 4 5 0 7 8 9 7 8 9 Ans. The following program does it. Please note that the matrix is declared as global so as to reduce complications in the program. Better way should have been to pass the
Read more: Matrix

Overloading the Parenthesis () Operator
2007-10-06 03:36:00
First, I want to apologize to my readers for not being able to post lately, partly due to me being very busy these days;-) As we all know, there are certain ways by which we can pass data to objects (of class). We can pass values during the time of construction as below: class-name ob-name(values); Or we may define a member function to accept data which can be called as below: class-name ob-name; ob-name.set(values); Where set is the member function of the class. But actually there is one more way to do so, yeah you guessed it right!, by overloading the parenthesis () operator. Parenthesis () operator like other operators is overloaded with the following prototype: ret-type operator()(arg1, arg2,...); It is a unary operator hence the only argument passed to this function (implicitly) is this pointer. The argument list may contain any number of arguments as you wish to pass. The following example program illustrates the overloading of parenthesis () op
Read more: Operator

Easy Freeware Downloads - My New Blog
2007-10-28 01:11:00
Q. What is this? A. This is the screenshot of my new blog Easy Freeware Downloads . Q. What was it about? A. It is pretty much a freeware (software) archive. It is a blog hence new freeware are added with brief description and feature list. Q. Why is it named so? A. For each freeware we list, a direct download link makes downloading easy with just ONE CLICK, hence the name. Q. Is it worth visiting now? A. I guess so, because I am announcing it after having working on it for more than a month. It has 60+ freeware listed (as of 28-Oct-07). Click Easy Freeware Downloads to visit.


Something about Local Classes
2007-11-03 03:42:00
We all know that identifiers (variables, objects, functions etc.) may have two scopes in C++. They may be declared as global or local to a block. We have seen identifiers like variables and functions to be defined locally and globally quite often but there is one identifier which is not that commonly declared as local, yeah you guessed right, its classes. You might have noticed the fact that classes are almost always declared as global even when they are to be used only in one block. It is so because of some reasons that we’ll discuss later. First let’s have a look at a class declared locally: // this code contains a local class #include <iostream.h> void func(); void main() { // myclass unknown here } void func() { class myclass { ... ... }; } While classes may also be defined as local, there are some restrictions of what can be done and what cannot be, they are listed below: Member functio
Read more: Local , Classes

Classes and Structures in C++
2007-12-04 04:02:00
In C, a structure (struct) gives us the ability to organize similar data together. You may wonder what I said. It is so in C, this is because structure is one of the few things which is more or less entirely different in the two languages (C and C++). In C++, the role of structures is elevated so much as to be same as that of a class. In C, structure could only include data as variables and arrays but in C++ they can also include functions, constructors, destructors etc. and in fact everything else that a class can. Knowing this, it wouldn’t be wrong to say that in C++, structures are an alternate way of defining a class. However there are some differences. Look at the following code: // First difference between a class // and a structure in C++ // define a structure struct mystruct { char name[25]; int id_no; }; void main() { mystruct a; // in C, it is necessary to // include the struct keyword // Example:
Read more: Classes , Structures

Inline Functions and their Uses
2007-12-11 03:11:00
It’s a good practice to divide the program into several functions such that parts of the program don’t get repeated a lot and to make the code easily understandable. We all know that calling and returning from a function generates some overhead. The overhead is sometimes to such an extent that it makes significant effect on the overall speed of certain complex and function-oriented programs. In most cases, we have only a few functions that have extensive use and make significant impact on the performance of the whole program. Not using functions is not an option, using function-like macros is an option, but there is a better solution, to use Inline Functions . Yes, like it sounds, inline functions are expanded at the place of calling rather than being “really called” thus reducing the overhead. It means wherever we call an inline function, compiler will expand the code there and no actual calling will be done. Member functions of cl


Using a Stack to Reverse Numbers
2007-12-07 03:32:00
Yeah, I heard many of you saying this and I know it’s no big deal to reverse a number and neither is it using stack to do so. I am writing this just to give you an example of how certain things in a program can be done using stacks. So, let’s move on… As many of you already know, a stack is a Data Structure in which data can be added and retrieved both from only one end (same end). Data is stored linearly and the last data added is the first one to be retrieved, due to this fact it is also known as Last-In-First-Out data structure. For more info please read Data Structures: Introduction to Stack s. Now, let’s talk about reversing a number, well reversing means to rearrange a number in the opposite order from one end to the other. Suppose we have a number 12345 then its reverse will be 54321 Ok, now let’s have a look at the example program which does this: // Program in C++ to reverse // a number using a Stack // PUSH ->
Read more: Reverse , Numbers

Merry Christmas to Everyone
2007-12-22 05:08:00

Read more: Christmas , Merry , Merry Christmas

Let Us Grow Our Community
2008-03-11 05:56:48
Click Here to Submit Your Article I guess many sites/blogs don’t talk about it too often but I am ;-) These are the number of articles that I’ve posted for the respective months, it’s clear that for 4-5 months I’ve not been able to post much. Although there is a thing about quality over quantity but I don’t think it makes as an excuse. Does it? Amazingly though, the number of visitors and page views have increased months after month. In the span of about 9 months with 112 articles, we have somewhat formed our own community. I know that many of our visitors are good programmers and have the ability to share their knowledge and expertise. If you feel like you have/know something that you’d like to share with thousands of other peoples on our community, then please submit your


Electronics Hobby Shop Launched
2008-03-11 05:46:31
You may skip this post if you’re not a resident of India Microcontrollers such as AVR, PIC from Atmel and Microchip are fast becoming the choice of hobbyist for their projects. Now instead of many conventional ICs hobbyists are rather using a single MCU (MicroController Unit) for their projects due to many advantages they have. In the recent years MCUs have become cheap too. Now you may easily get a fully functional MCU from Atmel at under Rs. 70! Sadly that is only one dimension of its popularity if you don’t reside in those big cities, leaving some big cities MCUs are not that easily available let alone the remote areas. There are many resellers but they either deal in large quantities or at high prices both not being suitable for average hobbyists. Seein
Read more: Hobby , Launched

Operation on Bits and Bitwise Operators
2008-01-16 00:46:19
OK guys, this is my first post in the New Year 2008, I thought of posting it earlier but at last I didn’t. It’s already been so long since I posted so let’s keep everything aside and talk just about what we have for today. ;-) I was sitting the other day thinking about what to write for a post here. Suddenly I realized that we have discussed operations on matrices, arrays, and what not but we haven’t had the chance to talk anything about the most fundamental thing a computer understands. Yeah, Operation on Bits. Bits can have only two values either ON (1) or OFF (0). In this article, we’ll be discussing about the different operations which can be performed on bits. One thing to note here is, we don’t perform these operation on


!!Happy New Year - 2008!!
2007-12-30 00:31:18

Read more: Happy , Happy New Year

Decimal Number to Binary Conversion Program
2008-03-14 02:01:51
Please read Operation on Bits and Bitwise Operators and Right/Left Bit Shift Operators if you haven’t already. This post is based on those articles. We’ll be using the following operators and the respective properties for decimal to binary conversion: AND (&) Operator from the article Operation on Bits and Bitwise Operators: Its property to be able to check whether a particular bit is ON (1) or OFF (0). Left Bit Shift Operator (<<) from the article Right/Left Bit Shift Operators: Its property to shift bits (of byte(s)) to desired number of places to the left. After making you guys familiar with the two things above, the program will be easier to understand. // Example Program to convert decimal num
Read more: Number , Binary , Conversion

Right/Left Bit Shift Operators
2008-03-13 07:54:48
This is the continuation of the article Operation on Bits and Bitwise Operators. If you haven’t read that, it is strongly recommended that you do, before proceeding with this article. Bit shifting, as the name signifies, does shifting of bits in byte(s). There are basically two ways, in which bits (of a byte) can be shifted, either to the right, or to the left. Thus we have two types of bit shifting operator. If you think logically, its pretty clear that for bit shifting in a byte, we need to have two data. We need the byte(s) to shift bits on and the number of bits to be shifted. Guess what, the two operators need these to data as operands! Right Bit Shift ing Operator (>>) Syntax: res = var >> num; This would shift all bits in the variable var, num


How Bitwise Operators are Used, an Example Program
2008-03-15 01:53:27
Well, one-by-one we’ve discussed each of the Bitwise Operator. Starting from Operation on Bits and Bitwise Operators, we moved on to Right/Left Bit Shift Operators then discussed Decimal Number to Binary Conversion Program . and at last One's Complement and XOR Operators. After having so much theoretical it’s time now for a nice Example Program, which is the topic of today’s post. The code here is basically to show how these bitwise operator are used rather than what they are used for. // Example Program to demonstrate how // One's Complement (~) and XOR (^) // Opeartors are used. #include<stdio.h> // prototype void showbits(short int); // defined void showbits(short int dec_num) { short int loop, bit, and_mask; f


One's Complement and XOR Operators
2008-03-15 01:45:55
Talking about Bit Operators we are left with two of them, which we’ll be discussing in this article. One’s Complement Operator (~) It takes and works only on one operand. On taking one’s complement of any variable, the 0s are changed to 1 and vice-versa from the bit structure (binary representation) of that variable. The following example will make it easier to understand: Suppose we have a short int a short int a = 16; its binary representation will be 0000000000010000 (decimal 16) on taking one’s complement like below res = ~a; res will contain 1111111111101111 (decimal 65519) It can be used as a part of algorithm to encrypt data. XOR (eXclusive OR) (^) It is derived from the OR Operator and takes two operands to work on. It compares bits like


Introduction to Microcontrollers
2008-04-06 01:42:31
This is a guest post by Avinash over at Extreme Electronics. Check out his website to learn more about electronics and microcontroller programming. Computer programming is exciting! Isn’t it. It gives you the felling of power in your hands, power to create and innovate. You can create solution for many everyday problems. Many programs you create are simple doesn’t requires much RAMs only few hundred bytes for variables, array etc. And the input outputs are simple. For example a “calculator” application. Your computer has resources many times more than what is required to run that app. The thing I want to say is that you can easily make a small computer at very cheap cost (less than Rs 300/$6.00). For any specific purpose and make i
Read more: Introduction

Introduction To PHP, The Web Programming Language
2008-04-17 09:24:00
PHP is a recursive acronym for PHP Hypertext Preprocessor, though it originally stood for Personal Home Page. It is designed specifically for the web, hence a web programming language. PHP is a server-side scripting language which can be either embedded into HTML documents or used alone. Since PHP is a interpreted language, when someone requests a page containing PHP, the code is interpreted on the server and the output (often HTML) is returned to the client web browser. As you might have guessed, PHP can help you generate different outputs depending on the conditions and generate different pages depending on conditions programmed, hence able to create what wee call “Dynamic Pages”. For example, suppose we want to put the current date and time on our web pa
Read more: Introduction , Programming

Page 4 of 5 « < 2 3 4 5 > »
eXTReMe Tracker