Purpose of run time Polymorphism in C++/CPP.

What is the main purpose of run time polymorphism? Why run time polymorphism is used in object oriented programming like C++? What is the main reason to use polymorphism in C++? Why late binding is used in C++?
These are the most frequently asked questions in interviews for C++ walkins or face to face interviews. If you are fresher then telling the definition alone is enough but by experienced professionals it is not acceptable. So what should be the answer?
If you are able to mention a real time scenario where it is used in proper manner then your work is done.
One such example is to create array of base class pointer which holds addresses of related derived class objects. I.e. Suppose an employee management system. Employee is base class and derived classes are employees like trainee, junior software engineer, senior. Software engineer, Lead engineer, manager etc.
Employee base class has virtual/pure virtual function like salaryIncrement, empPromotion.
Each derived class has its own implementation of these function. If management wants to use salaryIncrement then run time polymorphism allows to create array of base class pointers each having address of different derived class object and in a loop with same call syntax it can increment all employees salary at once without knowing the derived class object type at compile time.

C++ program to implement:Default constructor, One argument constructor, Copy constructor, overloaded assignment operator, destructor and overload + operator to concatenate two strings in a single program.


C/C++ Linux Interview Questions asked in real IT companies in India

Watch this space for coming soon C++ Class object data members constructor destructor abstraction encapsulation inheritance operator over loading polymormophism run time and static morphism virtual base class virtual function virtual keyword private protected public access specified template exception handling STL default constructor copy constructor destructor overloaded assignment operator shallow vs deep constructor malloc vs new delete vs free heap and static memory dangling pointer wild pointer crash hang static dynamic reinterpret cast pointers array reference.

C++ program to implement:Default constructor, One argument constructor, Copy constructor, overloaded assignment operator, destructor and overload + operator to concatenate two strings in a single program.

How to use array of structure in C Linux Code.

How to enter details of 10 (multiple) employee ? Answer is array of employee structures.
#include<stdio.h>
#include<memory.h>
struct employee
{
int id;
char name[20];
//int age;
};

int main()
{
int i;
struct employee arr[3];
for(i=0;i<3;i++)
{
printf("Enter details of employess:");
scanf("%d%s",&arr[i],arr[i].name);
}
printf("Employee details:\n");
for(i=0;i<3;i++)
{
printf("id=%d, name=%s\n",arr[i].id, arr[i].name);
}
return 0;
}
/*******OUTPUT
Enter details of employess:1
Leal
Enter details of employess:2
Mitra
Enter details of employess:333
Jesus
Employee details:
id=1, name=Leal
id=2, name=Mitra
id=333, name=Jesus
*****************/
//Display structure details based on search conditions
//students's name and roll number should be stored in structure
//array of 10 student structure
//take input and display
//displaye the student detail whose roll number is entered
#include<stdio.h>
#define SIZE 2
struct student
{
char name[20];
int roll;
};
struct student arr[SIZE];//needs global
int i;
int rollSearch;

void add();
void disp();
void search();
int main()
{
//Array of 5 students
//struct student arr[5];//needs global
//enter student details
add();
//display students details
disp();
//display details of student whose roll number is entered
search();
return 0;
}
void add()
{
for(i=0;i<SIZE;i++)
{
printf("Enter name of student %d:",i+1);
scanf("%s",arr[i].name);
printf("Enter roll of student %d:",i+1);
scanf("%d",&arr[i].roll);
}
}
void disp()
{
printf("*************Student Details***************\n");
for(i=0;i<SIZE;i++)
{
printf("Name of student %d is:%s\n",i+1,arr[i].name);
printf("Roll of student %d is:%d\n",i+1,arr[i].roll);
}
}
void search()
{
int flag=0;
printf("*************Search Result***************\n");
printf("Enter Roll of whose details you want:");
scanf("%d",&rollSearch);
for(i=0;i<SIZE;i++)
{
if(arr[i].roll==rollSearch)//mind the syntax roll[i].roll
{
flag=1;
printf("Name of student %d is:%s\n",i+1,arr[i].name);
printf("Roll of student %d is:%d\n",i+1,arr[i].roll);
}
}
if(flag==0)
printf("Roll Number does not exist.\n");
}
/*Output:
Enter name of student 1:Leal
Enter roll of student 1:100
Enter name of student 2:Mitra
Enter roll of student 2:200
Enter name of student 3:Ram
Enter roll of student 3:300
Enter name of student 4:Raheem
Enter roll of student 4:400
Enter name of student 5:yeshu
Enter roll of student 5:500
*************Student Details***************
Name of student 1 is:Leal
Roll of student 1 is:100
Name of student 2 is:Mitra
Roll of student 2 is:200
Name of student 3 is:Ram
Roll of student 3 is:300
Name of student 4 is:Raheem
Roll of student 4 is:400
Name of student 5 is:yeshu
Roll of student 5 is:500
*************Search Result***************
Enter Roll of whose details you want:200
Name of student 2 is:Mitra
Roll of student 2 is:200
*/

How to access nested structure members in C C++ Linux

#include<stdio.h>
#include<memory.h>
struct employee
{
int id;
char name[10];
struct dob
{
int day;
int month;
int year;
}d;
};

int main()
{
struct employee e1;
//struct dob dd;//can decalre but cant use
e1.id=1000;
strcpy(e1.name,"LealMitra");
e1.d.day=10;
e1.d.month=7;
e1.d.year=2014;

printf("Employee details:\n");
printf("id=%d, name=%s, dob=%d-%d-%d \n",e1.id, e1.name, e1.d.day,e1.d.month,e1.d.year);

return 0;
}
/****************OUTPUT
Employee details:
id=1000, name=LealMitra, dob=10-7-2014
*****************/

How to Initialize structure member variables in C C++

Ways of initializing a structure in C and C++. How not to do mistake in initializing structures.
#include<stdio.h>
#include<memory.h>
struct employee
{
int id;
char name[20];
//Note that below ways of initializing structure is error.
//int id=10;//erro:because memory is not allocated for structure here.
//strcpy(name,"Ram");//error for same reason.
};

int main()
{
//one way of initializing structure.
struct employee e1={1000,"Leal"};//one way of initializing structure.
printf("Employee details:\n");
printf("id=%d, name=%s\n",e1.id,e1.name);


//Another way of initializing structure.
struct employee e2;
e2.id=1001;
strcpy(e2.name,"Mitra");
printf("\nEmployee details:\n");
printf("id=%d, name=%s\n",e2.id,e2.name);

//Yet another way of initializing  structure.
struct employee e3;
printf("Enter id :\n");
scanf("%d",&e3.id);
printf("Enter name :\n");
scanf("%s",&e3.name);
printf("\nEmployee details:\n");
printf("id=%d, name=%s\n",e3.id,e3.name);
return 0;
}
/******* OUTPUT
@ubuntu:~/headHunt$ gcc structureInitialization.c 
structureInitialization.c: In function ‘main’:
structureInitialization.c:30:1: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[20]’ [-Wformat]
@ubuntu:~/headHunt$ gcc structureInitialization.c 
structureInitialization.c: In function ‘main’:
structureInitialization.c:30:1: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[20]’ [-Wformat]
@ubuntu:~/headHunt$ ./a.out 
Employee details:
id=1000, name=Leal

Employee details:
id=1001, name=Mitra
Enter id :
200
Enter name :
blog

Employee details:
id=200, name=blog
@ubuntu:~/headHunt$ 
*****************/

Writing and Reading File through C Linux Code

/* Writing to a file. Reading from a file */
#include<stdio.h>
int main()
{
    //writing to file
    FILE *fpw;
    fpw=fopen("1.txt","w+");
    fputs("Hello India\n",fpw);
    fclose(fpw);

    //Reading from file
    char buff[20];
    FILE *fpr;
    fpr=fopen("1.txt","r");
    fgets(buff,20,(FILE*)fpr);
    printf("%s\n",buff);
    fclose(fpr);
    return 0;
}
/*
 Hello India
 */

---------
/*
 Writing to a file
 Reading from a file
 Writing to another file
 */
#include<stdio.h>
int main()
{
    //writing to file
    FILE *fpw;
    fpw=fopen("1.txt","w+");
    fputs("Hello India\n",fpw);
    fclose(fpw);

    //Reading from file
    char buff[20];
    FILE *fpr;
    fpr=fopen("1.txt","r");
    fgets(buff,20,(FILE*)fpr);
    printf("%s\n",buff);

        //Writing to another file
    FILE *fpo;
    fpo=fopen("22.txt","w+");
    fputs(buff,fpo);
        //fprintf(fpo,"%s",buff);
    fclose(fpr);
    fclose(fpr);
    return 0;
}
/*
 Hello India
 */

---------

/*
 Writing and reading to and from file
 */
#include <stdio.h>
int main()
{
    /*Writing to a file*/
    FILE *fp;
    fp = fopen("test.txt", "w+");
    fprintf(fp, "This is testing for fprintf...\n");
    fputs("This is testing for fputs...\n", fp);
    fclose(fp);

    /*Reading from a file*/
    FILE *fp1;
    char buff[255];
    fp1 = fopen("test.txt", "r");
    fscanf(fp1, "%s", buff);//upto space
    printf("1 : %s\n", buff );

    fgets(buff, 255, (FILE*)fp1);//upto new line
    printf("2: %s\n", buff );

    fgets(buff, 255, (FILE*)fp1);//last line
    printf("3: %s\n", buff );
    fclose(fp1);

    return 0;
    }
/*
1 : This
2:  is testing for fprintf...
3: This is testing for fputs...
 */
/*
 The functions fgets() reads up to n - 1 characters from the input stream referenced by fp. 
It copies the read string into the buffer buf, appending a null character to terminate the string.
We can also use int fscanf(FILE *fp, const char *format, ...) function to read strings froma file but it stops reading after the first space character encounters.

Sorting Numbers in File and Writing to Other File Through C Linux Code

/*Code for taking numbers from an input file sort it and write it to another output file.
  Input file contains 216543 
 Output file contains 123456 */
#include<stdio.h>
int main()
{
        FILE *f1,*f2;
        int i,j,temp,v[6],ch;
        f1=fopen("data.in","r");
        f2=fopen("data.out","w+");

        for(i=0;i<6;i++)
                fscanf(f1,"%d",&v[i]);

        for(i=0;i<6;i++)
        {
                for(j=i+1;j<6;j++)
                {
                        if(v[i]>v[j])
                        {
                                temp=v[i];
                                v[i]=v[j];
                                v[j]=temp;
                        }
                }
        }
        for(i=0;i<6;i++)
                fprintf(f2,"%d\n",v[i]);

        close(f1);
        close(f2);
        return 0;
}

------------
/*
Code for taking numbers from an input file sort it and write it to an output file.
   Input file 216543
   Output file 123456
by counting the number of lines  
*/
#include<stdio.h>
int main()
{
        FILE *f1,*f2;
        int i,j,temp,v[10],ch,n=0;
        f1=fopen("data.in","r");
        f2=fopen("data.out","w");

char line[1024];

n=0;
while(fgets(line,sizeof(line),f1)!=NULL)
n++;
/*set the file pointer to beginning*/
fseek(f1,0,SEEK_SET);


        for(i=0;i<n;i++)
                fscanf(f1,"%d",&v[i]);

        for(i=0;i<n;i++)
        {
                for(j=i+1;j<n;j++)
                {
                        if(v[i]>v[j])
                        {
                                temp=v[i];
                                v[i]=v[j];
                                v[j]=temp;
                        }
                }
        }
        for(i=0;i<n;i++)
                fprintf(f2,"%d\n",v[i]);

        close(f1);
        close(f2);
        return 0;
}