DirectorySoftwareBlog Details for "Learning Computer Programming"

Learning Computer Programming

Learning Computer Programming
I post articles mostly on Learning Computer Programming in general and specifically using C plus plus. This blogsite is created as a part of my hobby of computer programming, I also share my experiences in a teaching manner.
Articles: 1, 2, 3, 4, 5, 6

Articles

Let Us Grow Our Community
2008-02-18 07:08:00
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 month 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 articles to us. We’ll post it here and give you the platform. Along with the articles, a short note about the author (includ...
More About: Personal , Community , Grow
Operation on Bits and Bitwise Operators
2008-01-16 07:00:00
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 single bits but rather on a group of bits (byte(s)). So even though Bitwise operators operate on bits its almost always a part of a group (byte, which makes up each data type), it means we...
More About: Operators , Introduction , Data Structures
Inline Functions and their Uses
2007-12-11 10: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...
Using a Stack to Reverse Numbers
2007-12-07 10: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 Stacks . 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 -&...
More About: Numbers , Class , Data Structures , Number
Classes and Structures in C++
2007-12-04 11: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:...
More About: Classes , Class , Structures
Something about Local Classes
2007-11-03 09: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 func...
More About: Classes , Local , Some , Loca
Easy Freeware Downloads - My New Blog
2007-10-28 07: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.
More About: Personal , Blog
Overloading the Parenthesis () Operator
2007-10-06 10: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. Pare nt hesis () 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 () ...
More About: Operator , Loading
Some Operations on Matrix
2007-09-11 14: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 ...
More About: Tricks , Matrix , Methods , Algorithms , Matrices
Overloading [] Operator II
2007-09-02 07: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 expr...
More About: Loading
Overloading [] Operator
2007-08-29 09: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 ...
More About: Operator , Loading
100 and Counting...
2007-08-26 08: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
More About: Counting
Adding Flexibility to Operators while Overloading them
2007-08-26 08: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 ...
More About: Operators , Flexibility , Lexi , Loading
Using Friends to Overload all the Overloaded Arithmetic Operators
2007-08-26 08: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); ...
More About: Friends , The O , Loaded
Problems on Operator Overloading II
2007-08-24 15: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;...
More About: Loading
Problems on Operator Overloading I
2007-08-24 15: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(); 3...
More About: Loading
Overloading the Short-Hand Operators (+= and -=) using Friend Functions
2007-08-23 15: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 o...
More About: Friend , Functions , Operators , Hand , Short
Overloading Post-Fix Forms of ++ and -- Operators using Friend Functions
2007-08-23 15: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;} v...
More About: Friend , Functions
Overloading Increment/Decrement Operators using Friend Functions
2007-08-23 15:19:00
In the article Operator Overloading using Friend Function s , 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 ...
More About: Creme
Operator Overloading using Friend Functions
2007-08-18 09:55:00
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 Function s 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 differen...
More About: Loading
Problems Related to Operator Overloading
2007-08-18 09:53:00
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...
More About: Problems , Related , Loading
Class with all the Overloaded Arithmetic Operators
2007-08-17 15:24:00
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 ...
More About: Operators , Class , The O , Loaded
Overloading the Short-Hand Operators (+= and -=)
2007-08-17 15:22:00
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&l...
More About: Operators , Hand , Short , Loading
Overloading the Assignment Operator (=)
2007-08-15 15:44:00
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 ...
More About: Operator , The A , Loading
Overloading Post-Fix Forms of ++ and -- Operators
2007-08-15 15:40:00
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 com...
More About: Post , Forms , Loading
Overloading Increment/Decrement Operators
2007-08-15 08: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.sh...
More About: Operators , Creme , Loading
What is 'this' Pointer?
2007-08-15 08: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.sho...
More About: Functions , Class , Pointers
Introduction to Operator Overloading in C++ II
2007-08-13 15:16:00
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(){} // constructo...
More About: Loading
Introduction to Operator Overloading in C++
2007-08-13 09:23:00
a1 = a2 + a3; The above operation is valid, as you know if a1, a2 and a3 are instances of in-built Data Types. But what if those are, say objects of a Class; is the operation valid? Yes, it is, if you overload the ‘+’ Operator in the class, to which a1, a2 and a3 belong. Operator overloading is used to give special meaning to the commonly used operators (such as +, -, * etc.) with respect to a class. By overloading operators, we can control or define how an operator should operate on data with respect to a class. Operators are overloaded in c++ by creating operator functions either as a member or a s a Friend Function of a class. Since creating member operator functions are easier, we’ll be using that method in this article. As I said operator functions are declared using the following general form: ret-type operator#(arg-list); and then defining it as a normal member function. Here, ret-type is commonly the name of the class itself a...
More About: Introduction , Loading
Practical Example of Using Virtual Functions II
2007-08-13 09:16:00
From the past few articles we have been discussing about Virtual Functions . Before taking up another topic for discussion I thought of providing one more example of how and when virtual functions may be used. So, here it is, a practical example of virtual function. As virtual functions and Run-Time Polymorphism goes hand-in-hand so the example here may also serve as an example of the use of run-time polymorphism. // Example to illustrate // the use of virtual functions // and run-time polymorphism #include <iostream.h> // -- SORT CLASS -- class sort { protected: int *arr; int num_elmnt; public: sort(int); ~sort(); void get_elmnt(); void show_elmnt(); virtual void do_sorting()=0; }; // takes an argument // which is the number of // elements we want sort::sort(int x) { num_elmnt=x; arr=new int[num_elmnt]; } sort::~sort() { // free up he allocated memory delete []arr; } vo...
More About: Class , Sorting
More articles from this author:
1, 2, 3, 4, 5, 6
111691 blogs in the directory.
Statistics resets every week.


Contact | About
© Blog Toplist 2012 - Supported by Web Catalog - SEO by FeWorks
eXTReMe Tracker