Friday, 24 March 2017

What is a virtual function in derived class in cpp programming

A virtual function is a part function that you hope to be reclassified in derived classes. When you allude to a derived class object utilizing a pointer or a reference to the base class, you can call a virtual function for that protest and execute the derived class' adaptation of the capacity.

A virtual function is a complicated beast and we are not through with it as yet. We know that when we perform inheritance and redefine some of the virtual functions the compiler creates a new VTABLE for the derived class and inserts into it the new function addresses. If in the derived class we do not redefine the virtual functions, then it inserts the base-class function addresses in the derived class's VTABLE. This means there's  always a full set of function address in the VTABLE. Thus a possibility of you calling a function and its address not existing in the VTABLE is ruled out.
Let us now consider a case when we inherit and new virtual functions in the derived class. The following program shows this possibility.

#include <iostream>
using namespace std;

class base
{
public :

virtual void fun1( )
{

cout<<endl<<"in base::fun1";

}
};

class derived : public base
{
public:
void fun1( )
{
cout << endl<< "in derived::fun1";
}
virtual void fun2( )
{
cout << endl<<"in derived::fun2";
}

};

int main( )
{
base*ptr1, *ptr2;
base b;
derived d;
ptr1 = &b;
ptr2 = &d;
ptr1->fun1( );
ptr2->fun1( );
ptr2->fun2( ); //error
return 0;
}

Here the class base contains a single virtual function fun1( ). The derived redefines fun1( ) and adds another virtual function called fun2( ). The VTABLEs created by the compiler for base and derived are shown in the below figure

The compiler would pass the calls  to fun1( ), but it would report an error for the call

ptr2-> fun( ) ;

In this case, the compiler is working only with a pointer to a base class object. The base class doesn't have the fun2( ) function so the compiler cannot allow a call to fun2( ). The compiler doesn't know that you are working with a derived object if it has only a pointer to a base class
object.

The conclusion is that the compiler prevents you from making calls to virtual functions that exist only in derived classes.

Monday, 6 March 2017

Fibonacci-series-In-JavaScript

Fibonacci numbers are the numbers in the accompanying whole number succession, called the Fibonacci series. In this Post, we will try to explain Fibonacci series in the different method. Hope You will Understand this program. any query you can comment below.  save all the files with (.HTML) extension.


 Fibonacci Series (1-10)


<html>
<body>
<script type="text/javascript">
var a=0,b=1,c;
document.write("Fibonacci");
while (b<=10)
{
document.write(c);
document.write("<br/>");
c=a+b;
a=b;
b=c;
}
</script>
</body>
</html>

Fibonacci series (input from user) 

<html>
 <head>
 <title>FibonacciSeries Using JavaScript</title>
 <script>
 function on_btn_gen_click() {
 count = parseInt(document.getElementById("in1").value);
 a = 0, b = 1, sum = 0;
 output = "<b>The first " + count + " elements in the Fibonacci series:</b> ";

 for(i = 0; i < count; i ++) {
 output += a + " ";
 sum = a + b;
 a = b;
 b = sum;
 }

 document.getElementById("p1").innerHTML = output;
 }
 </script>
 </head>
 <body bgcolor="LightGreen">
 <h1>Fibonacci Series</h1>
 <h2>Helps2code</h2>
 Enter the count of terms: <input id="in1" />
 <button id="btnGen" onclick="on_btn_gen_click()">ClickHere</button>
 <p id="p1" />
 </body>
</html>



Wednesday, 1 March 2017

Three-Dimensional Array in c

We wouldn't demonstrate a programming illustration that uses a three-dimensional array. This is on account of, practically speaking, one seldom utilizes this array. In any case, a case of instating a three-dimensional
an array will merge your comprehension of subscripts:

int arr[3][4][2] = {
{
{ 2, 4 },
{ 7, 8 },
{ 3, 4 },
{ 5, 6 }
},
{
{ 7, 6 },
{ 3, 4 },
{ 5, 3 },
{ 2, 3 }
},
{
{ 8, 9 },
{ 7, 2 },
{ 3, 4 },
{ 5, 1 },
}
} ;


A three-dimensional array can be considered as a variety of array 

of arrays. The external array has three components, each of which is a 

two-dimensional array of four one-dimensional arrays, each of 

which contains two whole numbers. As it were, a one-dimensional 

an array of two components is built first. At that point, four such one-dimensional arrays are put one beneath the other to give a two-dimensional array containing four lines. At that point, three such two-dimensional arrays are set one behind the other to yield a three-dimensional array containing three 2-dimensional arrays. In the 

array statement takes note of how the commas have been given. Figure 


8.9 would potentially help you in envisioning the circumstance better.



Three-Dimensional Array in c





Again recollect that the game plan appeared above is as it were 

reasonably genuine. In memory, similar array components are put away 


directly as appeared in Figure 8.10.




Three-Dimensional Array in c




How would you refer to the array element 1 in the above array?
The first subscript should be [2], since the element is in third twodimensional array; the second subscript should be [3] since the
element is in fourth row of the two-dimensional array; and the
third subscript should be [1] since the element is in second position
in the one-dimensional array. We can therefore say that the
element 1 can be referred as arr[2][3][1]. It may be noted here that
the counting of array elements even for a 3-D array begins with
zero. Can we not refer to this element using pointer notation? Of
course, yes. For example, the following two expressions refer to

the same element in the 3-D array:

arr[2][3][1]

*( *( *( arr + 2 ) + 3 ) + 1 )


Summary

1)   An array is like a customary variable aside from that it can 

store various components of comparable sort.

2)   Compiler doesn’t perform bounds checking on an array.

3)   The array variable goes about as a pointer to the zeroth component of 

4)   the exhibit. In a 1-D exhibit, zeroth component is a solitary esteem, 

while, in a 2-D cluster this component is a 1-D exhibit.


5)   On incrementing a pointer it points to the next location of its

type

6)   array components are put away in touching memory areas 

thus they can be gotten to utilizing pointers.

7)   Only limited arithmetic can be done on pointers.

Java Program For Linear Search

Linear Search is a search method, in which the way to be searched is contrasted and every component in the rundown until it is found. This strategy works regardless of the possibility that the rundown is not sorted. It is thought to be slower than double hunt as the measure of the information (list) develops. Java Program For Linear Search


Here is the code for Linear Search in java/Java Program For Linear Search


import java.io.*;
class LinearSearch{
public static void main(String args[])throws IOException{
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
int a[]={92, 34, 78, 12, 45, 7, 81, 60, 51, 10};
int key, i;
System.out.print("Enter the integer to search:");
key=Integer.parseInt(br.readLine());
for(i=0;i<a.length;i++){
if(key==a[i])
break;
}
if(i==10)
System.out.println(key+" not found");
else
System.out.println(key+" found at index "+i);
}
}

Another Program For Linear Search/Java Program For Linear Search

import java.io.*;

public class LinearSearch
{
  public static void main(String args[]) throws Exception
  {
    int values[] = {5, 16, 10, 17, 8, 25};
    int key;
    int i;
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Array values are:");
    for(int n:values)
    {
      System.out.print(n+"\t");
    }
    System.out.println("\nPlease enter a key (search) value:");
    key = Integer.parseInt(br.readLine());
    for(i=0; i< values.length; i++)
    {
      if(values[i] == key)
      {
        break;
      }
    }
    if(i==values.length)
    {
      System.out.println(key+" not found...!");
    }
    else
    {
      System.out.println(key+" found at position "+(i+1));
    }
  }

}


Java Program to Search an Element in an Array using Linear Search/Java Program For Linear Search


package com.tcc.java.programs;

import java.util.*;

public class ArrayLinearSearch {
    public static void main(String args[]) {
        int count, num, i;
        int[] inputArray = new int[500];
 
        Scanner in = new Scanner(System.in);
 
        System.out.println("Enter number of elements");
        count = in.nextInt();
        System.out.println("Enter " + count + " elements");
        for(i = 0; i < count; i++) {
            inputArray[i] = in.nextInt();
        }

        System.out.println("Enter element to search");
        num = in.nextInt();
        // Compare each element of array with num
        for (i = 0; i < count ; i++) {
            if(num == inputArray[i]){
               System.out.println(num+" is present at index "+i);
               break;
            }
        }
 
        if(i == count)
           System.out.println(num + " not present in input array");
    }
}

If you like this program Java Program For Linear Search do a comment and let us know