Thursday, 26 February 2015

Operator Overloading in C++ : PART 2 ([ ] Operator)



  Array subscript operator [ ] used for array indexing can also be overloaded in C++. In this post I have added a program in which the array subscript operator [ ] is overloaded. The program illustrates an overloaded array subscript operator.

The program also offers a solution to the C++ puzzle question given below.

Floating Point Number in Array Subscript

Write a C++ program in which the following statement compiles without an error.

cout << a[0.123];

The difficulty in solving this puzzle arises from the fact that array subscript should be an integer.  

If you pass a float value to a normal array subscript you will get the following error "Invalid type for array subscript". 

One possible solution is to overload the array subscript operator.


There might be some other solutions to this puzzle. If you know any other solution please share them here. 
   

Wednesday, 25 February 2015

Operator Overloading in C++ : PART 1 (++ Operator)



 I am now teaching under graduate students C++ programming. I thought I will share the programs I have prepared so that it benefits everyone.

Overloading ++ Operator

In this post I have added sample programs overloading the ++ Operator. In C++ we have preincrement and postincrement operators. For example ++a is preincrement whereas a++ is postincrement. So in order to handle these two situations there should be two versions of the operator++( ) function. The two versions are shown below. 

    void operator++(int y);  // for postincrement
   // The call a++; will invoke this version
   // The call a++; is same as the call a.operator++();
   
   void operator++( );        // for preincrement
   // The call ++a; will invoke this version
   // The call ++a; is same as the call a.operator++(0);


Click here to download the C++ program. 


Overloading ++ Operator with Friend Function
In C++ we can overload the ++ operator by using friend function.  Here also we need two versions to manage the preincrement and postincrement versions of the ++ operator. The two versions are given below.

   void operator++(A, int);  // for postincrement
   // The name of the Class is A
   // The call a++; will invoke this version
   // The call a++; is same as the call operator++(a);
   
   void operator++(A);        // for preincrement
   //The name of the class is A 
   // The call ++a; will invoke this version
   // The call ++a; is same as the call operator++(a,0);

Click here to download the C++ program. 


Monday, 29 December 2014

The \nocite Command in BibTeX


  The normal behavior of BibTeX is set in such a way that unreferenced entries in the .bib file will not be displayed in the bibliography section. But what if we need to print even the unreferenced entries in the .bib file also made part of the bibliographic section? For that we have a command called \nocite{*}. This command will override the default behavior of BibTeX such that even an referenced entries in the .bib file will be added in the bibliographic section in the DVI or PDF file produced. The script below shows the working of \nocite{*} command. The TeX file uses a .bib file called bib1.bib. 

\documentclass{article}

\usepackage{natbib}

\title{Sample Article}

\author{D}

\begin{document}

\maketitle

\nocite{*}

Here the entries in the Bibfile are not referenced. 

But they are shown in the reference section because we 

use \textbackslash cite\{*\} command.

\bibliographystyle{plain}

\bibliography{bib1.bib}

\end{document}
 

Saturday, 9 August 2014

Client Server Program in C to Simulate TCP Multicast Server


  This program simulates a multicast server in C. The following line of code sets the number of clients accepted by the server program. 

#define NC 4

Here the number of clients is set as 4. First run the server process, then run the client processes.

Click here to download the Client program.

Clcik here to download the Server Program.

Sunday, 27 July 2014

Client Server Program in C to Simulate Go-Back-N-ARQ


  This program was also written some 5 years before. This again is a simulation program trying to imitate Go-Back-N ARQ. It is a client server program. Click here to view the Wikipedia article on Go-Back-N_ARQ. Here also the server side acts as the receiver and hence you should run this program first. Afterwards you should also run the client program which acts as the sender side. 

Figure Below Shows the Receiver

Figure Below Shows the Sender


The line given below sets the window size for the sender and the receiver.

#define W 5 
If you want you can change the window size to some other value. Currently the window size is 5.

The following lines in the receiver side decides the probability with which a frame fails to reach the destination.

#define P1 50
#define P2 10

You can increase the probability of a frame being corrupted by increasing the value of P2 and decrease the probability by decreasing the value of P2. Currently there is a 20% probability for frame corruption.

Click here to download the Receiver Side program.

Click here to download the Sender Side program.


Click here to view the article on Selective Repeat ARQ.

Client Server Program in C to Simulate Selective Repeat ARQ


  I wrote these programs 5 years ago. It is a simulation program trying to imitate the working of Selective Repeat ARQ. Click here to view the Wikipedia article on Selective Repeat ARQ. A client server C program is used to simulate the working of Selective Repeat ARQ. The server acts as the receiver. The client acts as the sender. Because of this reason first run the receiver program and afterwards run the sender program. 

Figure Below Shows theReceiver



Figure Below Shows the Sender
  

The line given below sets the window size for the sender and the receiver.

#define W 5 
If you want you can change the window size to some other value. Currently the window size is 5.

The following lines in the receiver side decides the probability with which a frame fails to reach the destination.

#define P1 50
#define P2 10

You can increase the probability of a frame being corrupted by increasing the value of P2 and decrease the probability by decreasing the value of P2. Currently there is a 20% probability for frame corruption.




In C and C++ typedef is a Storage Class Specifier


  Consider the program given below.

#include<stdio.h>
int main()
{
   typedef static int i;
   i a = 0;
   printf("%d", a);
   return 0;
}


The program when compiled as C or C++ will give you the following error "multiple storage classes in declaration specifiers". The error tells us that typedef in C and C++ is treated as a storage class specifier and this leads to the error since it is not allowed to use two storage class specifiers with a variable.

The C Reference Manual says "The typedef specifier does not reserve storage and is called a storage class specifier only for syntactic convenience".