Like Us On Facebook

Follow Us On Twitter

Drop Down Menu

Wednesday 24 April 2013

Inheritance And Its Types In C++

The basic meaning of Inheritance is using a pre-defined code. This is the main advantage in Object oriented Programming (OOPS), where one can use a code that has been previously written and defined but with the condition that the code is used as-is or in other words, we are not changing the code.
All the pre-defined code is used resides into classes. To use those codes, we use a feature called inheritance, whereby, we "inherit" those classes.
The class from which other class inherits is called as Parent or Base Class and the class that inherits is called as Child or Derived Class.
 

Types Of Inheritances:
There are 5 types of Inheritances used in C++:
1) Single Inheritance
2) Multilevel Inheritance
3) Hierarchial Inheritance
4) Hybrid Inheritance
5) Multiple Inheritance

As Shown in Figure:

Types Of Inheritance C++

Accessibility Modes in Inheritance in C++:
We can use the following table to understand the accessibility of the members in different modes of Inheritance:

Accessibility Modes in Inheritance C++

Here, by 'X' we mean that, that object is not inheritable. Remember, Private objects and functions are never inherited. I'll be discussing about these types of inheritances individually in my next post.



Saturday 6 April 2013

C Program To Generate Fibonacci Series

Hi Friends,
Today, we'll learn how to generate a Fibonacci Series using C. The program and concept are pretty simple. But before that, let me tell you what exactly is a Fibonacci series.  Fibonacci series is a special series in which the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two. By going this way, the series generated will be:


0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144 and so on...

The program is very simple.

First input is taken from the user that tells how many terms of the Fibonacci series will be generated and then as described above, a function is called in a loop to add the previous two numbers and generate the next number in the series.


Here is the C Code for Fibonacci Series:


/* Double-Click To Select Code */

#include<stdio.h>
#include<conio.h>

int fib(int n)
{
 if(n==1||n==2)
 return 1;

 else
 return(fib(n-1)+fib(n-2));
}

void main()
{
 int i,n;
 clrscr();
 printf("Enter the number of terms: ");
 scanf("%d",&n);

 printf("\nThe Series is : \n\n0");

 for(i=1;i<n;i++)
 {
  printf("%5d",fib(i)); // fib Function Called
 }
 getch();
}


Output:



C Program To Generate Fibonacci Series



Please Comment If You Liked This Post !!