Posts

Showing posts from January, 2013

Interesting pattern in Fibonacci Series

Hi everyone, in this blog post I will be describing an interesting pattern formed by fibonacci numbers. Fibonacci number or fibonacci sequence is a sequence of numbers such that a given number is the sum of the previous 2 numbers. In mathematical terms, F(n) = F(n-1) + F(n-2). Fibonacci sequence is as follows - 0,1,1,2,3,5,8,13,21,34,55,89,144,233, 377, 610, 987....etc. There are plenty of unknown sequence and patterns in fibonacci sequence. If we carefully look the series, we ourself can figure out many unknown patterns. Here, I am specifying one of those unknown patterns in fibonacci sequence.  Suppose we took the number 610. It can be represented in the following ways:- 610 = 610 * 1 + 377 * 0  610 = 377 * 1 + 233 * 1 610 =  233 * 2 + 144 * 1 610 = 144 * 3 + 89 * 2 610 = 89 * 5 + 55 * 3 610 = 55 * 8 + 34 * 5 610 = 34 * 13 + 21 * 8 610 = 21 * 21 + 13 * 13 610 = 13 * 34 + 8 * 21 610 = 8 * 55 + 5 * 34 610 = 5 * 89 + 3 * 55 610 = 3 * 144 + 2 * 89 610 = 2 * 233 +

Anonymous classes in C++

Anonymous Classes in C++ Anonymous class is a class with no name. The main advantage of using an anonymous class is that it provides reusability of the class. Also anonymous class helps in encapsulation of data. Anonymous class is basically used when only one instance of the class is needed and it has only a short body. By defining an anonymous class, it does the same steps of defining a local class and instantiating it. The anonymous class is rightly used after it is defined. Following specifies an example of anonymous class in C++. class { int a; } c1; Here, c1 is an object of the anonymous class. Usually, anonymous classes in C++ are defined using "typedef" ( a keyword to assign alternative names to existing types). Typedef helps in giving another name to the class. It is shown as below: typedef class { int a; } newclass; newclass c2; Here newclass is the object of the anonymous class and c2 is its instance. Member variables can also be initialized in ano

Shift operators in bit level programming

Why bit level programming ? Hi everyone, this blog post is about shift operators in bit level programming. The first question which comes in most of the readers mind is why we are doing bit level programming? The answer is as simple as that, bit level programming is the lowest level programming. Here we will be using only bits of memory. It will help in saving a lot of memory space. Please remember that 1 byte = 8 bits. An example of bit level programming is given. Find whether a number is a power of 2.  This question can be answered in many ways.  We can find the number modulus 2 and check whether it is equal to 0. Then we can divide the number by 2 till it is possible. Simultaneously check the modulus of that number. If the mode of that number = 0, it is power of 2. Program to find whether the number is power of 2. Here, we are doing integer programming. The size of 'int' is 4 bytes or 32 bits. But, the same can also be done using bits. Take

Basic Concepts in Oops - 4

Hi everyone, this is the continuation of previous blog. Here, I am discussing some more topics on object oriented programming. 16) Polymorphism Polymorphism in simple term means same name, but different functions. Polymorphism makes possible same functions to act different on different classes. It can happen in a same class or between different classes. In same class, sometimes there will be same function names with different parameters. Or if a class extends other class, there can be same function names with different implementation in both the classes. 17) Method Overloading Method overloading occurs in the same class. Here, there will be functions with same name, but different arguments and return type. Following gives an example of method overloading. class Vehicle { int vehilceId; int registrationNum; Vehicle(int a) { vehicleId=a; registrationNum=a; } Vehicle(int a, int b) { vehicleId=a; registrationNum=b; } } Here, the constructor Vehicle is defined twice with

Basic Concepts in Oops - 3

Hi everyone, this is the continuation of the previous post. Here i am discussing some more concepts in oops. 11) Encapsulation The combining of data and functions into a single unit is known as encapsulation. The data is not reachable from outside functions. Encapsulation helps in data hiding. 12) Multiple Inheritance Multiple inheritance is the type of inheritance in which one subclass inherits from 2 or more base class. In C++, we can do multiple inheritance directly. But in java, we can't do multiple inheritance directly. For that we are using interfaces. An example of multiple inheritance in C++ is shown below. class Vehicle { private: int vehicleId; int milage; public: Vehicle(int a, int b): vehicleId(a),milage(b) { } }; Class Production { private: int yearOfProduction; public: Production(int c): yearOfProduction(c) { } }; Class Car: public Vehicle, public Production { private: int cost; public: Car(int a, int b, int c, int d) : Vehicle(a,b),Production(c),c