Tuesday, 30 April 2013

Stack Overflow

  Three months ago I have joined Stack Overflow. Now I am very passionate about it. SO is one of the best Computer Science forums I have ever seen. If you are serious about Computer Science then I believe it is a must for you to have membership in Stack Overflow. Membership is free and easy. 



A Humble Beginning


I have proposed a Site for Q & A on Special Education. But in order to set up this site a lot of followers are required. I truly believe that such a Q & A forum will help a lot of children with special needs. If you are interested please follow this proposal. It will be a great help. 

Here is the Link to the proposed Special Education Q & A Site.

Please Click the image below to follow the Proposed Site.
  
Stack Exchange Q&A site proposal: Special EducationEducation


Overloading Increment/Decrement Operators Using Friend Functions

  Friend functions can be used to overload Prefix and Postfix forms of Increment/Decremnt operators in C++. Sample output and code are shown below. 

Figure Below Shows a Sample Output


#include<iostream>

using namespace std;

class over_load
{
    int n1,n2;
    public:
    void set(int x, int y)
    {
        n1=x;
        n2=y;
    }
    void show()
    {
        cout<<"\nNumber 1 is "<<n1<<"\n";
        cout<<"\nNumber 2 is "<<n2<<"\n";
    }
    friend over_load operator++(over_load &ob)
    {
        ob.n1++;
        ob.n2++;
        cout<<"\nPost Incremnt Operator Overloaded\n";
    }
    friend over_load operator++(over_load &ob, int x)
    {
        ++ob.n1;
        ++ob.n2;
        cout<<"\nPre Increment Operator Overloaded\n";
    }
};

int main()
{
  over_load ob1;
  ob1.set(100,200);
  cout<<"\nObejct ob1 Initially:\n";
  ob1.show();
  ob1++;
  ob1.show();
  ++ob1;
  ob1.show();
  return 0;
}


Click here to download the C++ Program.

Friday, 26 April 2013

Overloading Prefix and Postfix Increment/Decrement Operators in C++

  In C++ operators can be overloaded. Problems can arise when we have to overload Increment/Decrement Operators of C++. We need to distinguish between Prefix and Postfix forms of Increment/Decrement Operators. The program given below shows the prototype for Prefix and Postfix forms of Increment/Decrement Operators.

#include<iostream>

using namespace std;

class over_load
{
    int n1,n2;
    public:
    void set(int x, int y)
    {
        n1=x;
        n2=y;
    }
    void show()
    {
        cout<<"\nNumber 1 is "<<n1<<"\n";
        cout<<"\nNumber 2 is "<<n2<<"\n";
    }
    over_load operator++()
    {
        n1++;
        n2++;
        cout<<"\nPost Incremnt Operator Overloaded\n";
        return *this;
    }
    over_load operator++(int x)
    {
        ++n1;
        ++n2;
        cout<<"\nPre Increment Operator Overloaded\n";
        return *this;
    }
};

int main()
{
  over_load ob1;
  ob1.set(100,200);
  cout<<"\nObejct ob1 Initially:\n";
  ob1.show();
  ob1++;
  ob1.show();
  ++ob1;
  ob1.show();
  return 0;
}



Figure Below Shows a Sample Output

Tuesday, 23 April 2013

C/C++ Program to Print Triangular, Pentagonal, and Heptagonal Numbers

  The C/C++ Program provided with this post prints the Triangular, Pentagonal, and Heptagonal Numbers.
Figure Below Shows a Sample Output



C/C++ Puzzles: PART - 33

C Program Showing Errors When Compiled as C++ Program 

  Write a C Program without any errors, but the same when compiled as a C++ Program should show errors? This was a question I had to answer recently. There are many different answers I suppose. The program given below is the one which comes to my mind first. 

#include<stdio.h>

void func();

int main()
{
  int x;
  x=333;
  func(x);
  return 0;
}

void func(int x)
{
    printf("\nValue is %d\n",x);
}

The program gives an error when compiled as a C++ program because in C a function with an empty parameter list can have any number of parameters including zero parameters where as in C++ it means the function can't receive any parameter.

Sunday, 21 April 2013

C Program to Print Pan Digital Prime Numbers

  An n-digit number is pan digital if it makes use of all the digits from 1 to n exactly once. For example, 2143 is a 4-digit pan digital number and is also prime. The C Program in this post prints the Pan digital prime numbers less than a given limit.

Figure Below Shows a Sample Output

Friday, 19 April 2013

Different Standards of C

  Many often beginners to C Programming are unaware of the fact that there are different standards for C Programming language. I had a few posts about C11 and C99 in this blog. But in this post I am listing all the C standards in a chronological order, from the oldest to the newest. 

1. K&R C
    This is the original standard of C. Some of the features and functions provided by this version is deprecated now.

2. ANSI C & ISO C (C89 & C90)
      This is the most common C standard, supported by most IDEs as the default version for programming. Many of the Universities and Colleges still teach this standard of C to students. Fine with me, but they should make sure that there is at least some discussion about the different standards of C. At least about the C99 standard since it adds a lot of power to the C language and implemented by large number of IDEs and Compilers.
  
3. C99
       C99 is having support from fairly large number of IDEs. C99 increases the power of C programming language.


4. C11
          This is the latest standard of C programming language. The proposals made by C11 are not yet fully implemented by many of the popular IDEs.