Tuesday, November 29, 2011

Find nth Prime Number in C++

c++ program to find prime numbers:


The problem of finding prime number can be solved by checking all numbers, testing them for prime and then moving ahead. If you want to calculate nth prime. Then this can be done in a brutal way by checking the number one by one. This may sound odd, by there is no easy way then this  for prime numbers (Well Actually there are like Pollard's Rho Algorithm, Number Sieves or Shor's Quantum Algorithm, but we are talking about the one that most people may understand easily). There may be way to pre-calculate the prime numbers but that again is not sufficient. So how can we use c++ to create a program to find prime numbers.

Monday, November 28, 2011

Program for Multi Level Inheritance and Function Overriding

Program for Multi Level Inheritance and Function Overriding:
Function overriding is a very key programming feature in OOP system. This allows programmers to create objects which has similar function ( with same name) but having different implementations. Such technique is really helpful in situations where each object has a common function but implementations ( games for example).

Sunday, November 27, 2011

News: All Code now have color highlighting

I have updated my codes to now have color highlighting, This will make code look a little better :)
Cheers

Program for Matrix Multiplication in C++ using Operator Overloading

Program for Matrix Multiplication in C++ using Operator Overloading :
We will now try to multiply two matrix by using the concept of operator overloading. The described way is very very easy to understand. We will be overloading "*" operator for this purpose. Although this can be done by any binary operator.

Monday, November 21, 2011

Stack in C using functions

Stack in C: Stack is a very useful data structure in C, It is used in many situations. Implementing stack can be done using a global array and a global pointer of the stack. although this implementation reveals how good OOP is for such situations. There will be three basic functions of the stack. One will be push() operation, which will push the given element to the stack. Pop() function  will pop out the element from the top. A show function to show the content of the stack.

Saturday, November 5, 2011

Pointers in C for starters

Ask any new computer science student ,"What is the most dreaded topic in C language?". They will reply ,"Still having nightmares about pointers !!!". And I don't blame them. In fact pointers become even more dreaded as the learning curve is really steep for beginner.
So what makes pointers so "evil" ? I personally think its not the pointers but its lack of knowledge about memory that actually kills a new student. Hence I claim "Pointers are innocent ".

Understanding Constructors in C++

What are Contructors in C++?
Ans: Constructor are special function that can help initialize an object.
Do we really need contructors in C++?
Well, Its a good feature and really helps programmer.
I am a good programmer and I don't Need contructors.
LOL !!!!!
Ok enough of chit chat, lets get to the point. Why contructors were included in C++. The answer lies in very nature of programmers. Programmer frequently get messy with code and allow mistakes to happen ( LOL :P). How? Lets have a look on following code :



#include<iostream>

using namespace std;

int main()
{
int k;
cout<<k;
}
This is a classical example of programmer forgetting to initialize the variable. Hence causing unexpected errors. This problem gets more complicated as we start creating objects. Since objects may have many data, its a very common mistake to forget to initialize the data portions of objects. This is where construcors come handy. The contructors have a property that they get called automatically when objects gets created.
#include<iostream>

using namespace std;
class Me
{
     int x,y;
    public:
   Me()
   {
      x=y=0;
   }
   void show()
   {
       cout<<x<<","y;
   }
};
int main()
{
  Me x;
x.show();
}
Declaring a Constructors will ensure that whatever initialization are put into the contructors to execute always whenever object gets created. Hence objects data will always will be in initialize state.
How to declare Contructors in C++?
Its really easy, just follow the following rules :
  • Contructors are public.
  • Constructors don't have any return type.
  • Constructors name is always as that of the Class.
  • Constructors with no arguments are called default constructors.
  • Constructors can be overloaded.
So next time you put some initialization code in a contructors, your object won't have garbage values in them.
Happy coding.