20130511

How to prepare C++/CPP/C for interview

Frequently asked C++ questions in interviews: 

  1. How to compare two strings using pointer without strcmp

  2. How to copy a string without using strcp with pointer

  3. How to find the length of a string without using library function strlen

  4. How to reverse a string using pointer or array in cpp without using strrev library function

  5. CPP program for concatenating two strings without using inbuilt library function strcat

  6. How to search a substring in C++/Cpp without using library function strstr

  7. How to use make file in CPP C+++ C

  8. How to implement msgq message queue in CPP C C++

  9. How to use callback function pointer in cpp/C/C++

  10. How to use malloc for 2d array dynamic memory allocation

  11. How to use new to allocate memory for 3d array in CPP C++

  12. How to learn stack and flow of function call and control in cpp

  13. How to learn recursive function call in cpp/C++/C

  14. How to distinguish between call by value, call by reference, call by address

  15. How to write cpp program for Circular Queue using Array

  16. How to write cpp program for Circular Queue using Linked List

  17. How to write cpp program for BST Binary Search Tree

  18. How to write cpp program to reverse a linked list

  19. How to write sorted linked list program in cpp with class

  20. How to use template in cpp

  21. Why to use explicit keyword in C++

  22. How to use new and delete in C++

  23. Getter and Setter Methods in C++

  24. C++ Program to Add Two Numbers using Class

  25. C++ Program for Fibonacci series

  26. Simple Program for malloc in C

  27. Is it necessary to accept an object by reference in copy constructor?

  28. Multiplication of two numbers using BITWISE operators

  29. New Features in ANSI C++ Standard

  30. Bitwise operation for division

  31. How to pass a object and return a object in C++/Cpp

  32. How to acces private member function in C++/CPP

  33. Why destructor should be virtual in C++ ?

  34. How to use tricky while loop in CPP,C++,C

  35. How to use for loop in C++ CPP C

  36. Program in C++ to learn virtual function

  37. How to use Virtual function C++

  38. Program to create a table in C
                                                                  Next Page


20130509

How to compare two strings using pointer without strcmp


/*C++/CPP/C program to compare two strings without using  library function strcmp*/
#include<stdio.h>
#include<string.h>
int strCmp(char *p1, char *p2);

int main()
{
char arr1[20];
printf("Enter a String:\n");
scanf("%s",&arr1);
char arr2[20];
printf("Enter a String:\n");
scanf("%s",&arr2);

printf("You entered:%s and %s\n",arr1,arr2);

20130505

How to copy a string without using strcp with pointer

/*How to copy a string without using library function strcpy.
How to use pointer/array to copy a string/sentence. C++/CPP/C program/code to copy a string/word/sentence without using inbuilt function strcpy.How copy a string without using library function strlen. cpp program to copy a string using pointer.cpp program to copy a string without using library function strcpy*/
#include<stdio.h>
#include<string.h>

void strCpy(char *p2,char *p1);

int main()
{
char arr1[20];
char arr2[20];
printf("Enter a String:\n");
scanf("%s",&arr1);

strCpy(arr2, arr1);

printf("You entered and copied:%s\n",arr2);

20130504

How to find the length of a string without using library function strlen


/*How to find length of a string without using library function strstr.
How to use pointer/array to find length of string sentence. C++/CPP/C program/code to find length a string/word/sentence without using inbuilt function strlen.How to find the length of a string without using library function strlen*/
#include<stdio.h>
#include<string.h>

int strLen(char *p);

int main()
{
char arr1[20];
printf("Enter a String:\n");
scanf("%s",&arr1);

printf("You entered:%s\n",arr1);
int n;
n=strLen(arr1);
printf("Strlen of entered string is:%d\n",n);

20130503

How to reverse a string using pointer or array in cpp without using strrev library function

How to reverse a string using pointer or array. CPP program for reversing a strings without using inbuilt library function strrev. How to reverse a sentence in cpp/C++/C without using library function strrev.Program to reverse a string using pointer.
#include<stdio.h>
#include<string.h>
int main()
{
char arr1[20];
printf("Enter a String:\n");
scanf("%s",&arr1);
printf("You entered:%s\n",arr1);

20130428

CPP program for concatenating two strings without using inbuilt library function strcat


How to concatenate two strings using pointer or array. CPP program for concatenating two strings without using inbuilt library function strcat. How to concatenate two strings in cpp/C++/C without using inbuilt in function strcat.Program to concatenate two strings using pointer.

#include<stdio.h>
#include<string.h>
int main()
{
char arr1[20];
printf("Enter a String:\n");
scanf("%s",&arr1);

20130426

How to search a substring in C++/Cpp without using library function strstr


/*How to search a sub string without using library function strstr. 
How to use pointer/array to search substring in a string sentence.
C++/CPP/C program/code to search a word in a sentence without using inbuilt function strstr*/
#include<stdio.h>
#include<string.h>
int main()
{
char arr1[50];
printf("Enter a String:\n");
scanf("%s",&arr1);

char arr2[20];
printf("Enter a String:\n");
scanf("%s",&arr2);
printf("Searching for:%s\n",arr2);

20130425

How to use make file in CPP C+++ C

/*What is makefile  ?
---------------------------------------
makefile is a script/utility  that contains target, dependencies and actions to compile modified file/files in a project of multple files.  */

/*SYNTAX
-----------------------------------
target:dependencies
[tab]  actions
.
.

20130422

How to implement msgq message queue in CPP C C++


//Program to implement msgq
/*CPP Program for msg queue in Linux
pipes work with related process parent-child only.
Fifo can make unrelated process to communicate but
fifo created does not persists for use in future.
Shared memory is similar to fifo but over comes this and can be used later also.
Program to implement IPC Message Queues IPC mechanism.
msgqSendkirk.c adds the message on the message queue.
msgqRecvSpock.c reads and removes the message from the message
queue.To use this program first compile and run msgqSendkirk.c
to add a message to the message queue. To see the
Message Queue in other terminal run msgqRecvSpock.c
ipcs -q shows the msg ques.
ipcrm -q 131073 removes the msgq created say 131071 */
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
//1. declaring the msg structure
struct my_msgbuf
{

20130421

How to use callback function pointer in cpp/C/C++

Program for callback /function pointer in CPP/C/C++

What is function pointers?As we know a pointer is a special variable that folds the address of another variable.
Function pointer is also  a pointer that hold the address of a function.
Int array[10]; // here array is a constant pointer to a 10 element array. That is constant pointer to first element of array.
Can we declare a non-constant pointer to a function? Yes we can .
Int (*funcPtr)();//funcPtr is a pointer to a function that takes no arguments and returns an integers.//parenthesis is must here as we are not assigning the return type.
Int (*funcPtr)(int,int);//funcPtr is a pointer to a function that takes two arguments and returns an integers.

20130413

How to use malloc for 2d array dynamic memory allocation


/*How to use malloc for 2d array dynamic memory allocation. How to write 2-d array with dynamic memory allocation */
#include<stdio.h>
#include<malloc.h>
int main()
{
int r,c,i,j;
printf("Enter No of rows and columns:");
scanf("%d%d",&r,&c);

int **array;
array=(int**)malloc(r*sizeof(int));

for(i=0;i<r;i++)
{
array[i]=malloc(c*sizeof(int));
}

How to use new to allocate memory for 3d array in CPP C++


/*How to use new to allocate memory for 3d array. How to use malloc for 3 dimensional array. CPP program for dynamically allocating memory for 3d array. How to write three dimensional array with malloc with new */

#include<iostream>
using namespace std;
int main()
{
// 3 Dimensions Array
// two 3x4 array
int x = 2, y = 3, z = 4;

int i, j, k;
// memory allocation for 3D

int ***array3D = new int**[x];
for(i = 0; i < x; i++)
{
array3D[i] = new int*[y];
for(j = 0; j < y; j++)
{
array3D[i][j] = new int[z];
}
}

20130411

How to learn stack and flow of function call and control in cpp

/*How to learn stack. Stack is memory in which Last in first out is followed.
When function call is encountered the address of the next statement is stored or pushed in the stack. When the control returns back from the definition to calling function the address is poped out which is nothing but the statement next to the function call and executed.*/
#include<stdio.h>
#include<iostream>
using namespace std;
void function1();
void function2();

int main()
{
printf("1\n");
printf("2\n");
function1();
printf("3\n");
printf("4\n");

return 0;
}
void function1()
{
printf("1.1\n");
printf("1.2\n");
function2();
printf("1.3\n");
printf("1.4\n");
}
void function2()
{
printf("2.1\n");
printf("2.2\n");
}

How to learn recursive function call in cpp/C++/C

/*How to learn recursive function call in cpp/C++/C
 Cpp Program to learn recursive function */
#include<iostream>
using namespace std;
int sum(int m, int k);
int main()
{
int i,m,k;
m=3;
k=2;
i=sum(m,k);
cout<<"sum="<<i<<endl;
//return 0;
}
int sum(int rm, int rk)
{
cout<<rm<<" "<<rk<<endl;
if(rm==0) return(rk);//termination condition
else return(1+sum(rk, rm-1));//recursive condition//if not +1 then sum=0
}

How to distinguish between call by value, call by reference, call by address

/*How to distinguish between call by value, call by reference, call by address.
Program to distinguish between pass by value,pass by reference,pass by address. Call by reference and call by address affects the calling value while call by value doe not. Most important difference is that in call by value new object is created which is some times such as when size of the variable is too large is not desirable so call by reference is preferred over call by value*/

#include<stdio.h>
#include<iostream>
using namespace std;
void callbyreference(int * x);//pass by address
int main()
{
int x;
x=10;
printf("Before x=%d",x);//10
callbyvalue(&x);
printf("\nAfter x=%d",x);//15
printf("\n");
return 0;
}

void callbyreference(int * x)
{
*x=*x+5;//mind the*
}


//CALL BY REFERNCE, PASS BY REFERNCE
/*

20130330

How to write cpp program for Circular Queue using Array


How to write C++ program for Circular Queue using Array
C++ program to implement Circular Queue using Array
Circular Queue implementation using Array
CPP program for implementing Circular Queue using Array


#include<iostream>
using namespace std;
#define MAXSIZE 4

class queue
{
private:
int cq[MAXSIZE];
int front, rear;
public:
queue()
{
int cq[MAXSIZE]={0};//error: array must be initialized with a brace-enclosed initializer
front= -1;
rear = -1;
}


void insert(int item)
{
cout<<"\n in insert.\t";
if( ( (front==0)&&(rear==MAXSIZE-1)%MAXSIZE ) || front==(rear+1)%MAXSIZE )
{
cout<<"Q is full.\n";
}
else
{
if(front==-1)
front=rear=0;
else
rear=(rear+1)%MAXSIZE;

cq[rear]=item;

cout<<"\n front = "<<front<<" rear = "<<rear<<endl;

}

}


void display()
{
int i;
for(i=0;i<MAXSIZE;i++)
{
cout<<cq[i]<<" ";
}
cout<<endl;
}


void deletee()
{
if(front==-1)
{
cout<<"Q is empty.\n";
}
else
{
int temp;
temp=cq[front];
cq[front]=0;
if(front==rear)//only 1 elt was there
front=rear=-1;//delete the single so q is empty
else
front=(front+1)%MAXSIZE;//if more than 1 elt then delete frm front

cout<<"\n Deleted elt is:"<<temp<<endl;
cout<<"\n front = "<<front<<" rear = "<<rear<<endl;
}
}

};
int main()
{
queue q;
q.insert(10);
q.insert(20);
q.insert(30);
q.insert(40);
q.display();

q.insert(111);//Q is full so make same place by deleting fronts
q.display();

q.deletee();
q.display();
q.insert(100);
q.display();



return 0;
}
/*
in insert.
front = 0 rear = 0

in insert.
front = 0 rear = 1

in insert.
front = 0 rear = 2

in insert.
front = 0 rear = 3
10 20 30 40

in insert. Q is full.
10 20 30 40

Deleted elt is 10.

front = 1 rear = 3
0 20 30 40

in insert.
front = 1 rear = 0
100 20 30 40
*/
Simples Way to learn Data Structure in C++ CPP or C

How to write cpp program for Circular Queue using Linked List



 /*C++ Program for Circular Queue implementation using linked list
Cpp program to implement Circular Queue using Linked List*/
C++ Program for implementing Circular Queue with Linked List

#include<iostream>
using namespace std;

class queue
{
private:
struct node
{
int data;
node* link;
};
node *front;
node *rear;

public:
void insert(int);
void display();
void del();
queue()
{

How to write cpp program for BST Binary Search Tree


/*BST-Binary Search Tree Program for  insertion and traversal
How to write C++ Program for BST-Binary Search Tree
Cpp program for BST Binary Search Tree implementation with class*/
-->
//bst-Binary Search tree
#include<iostream>
using namespace std;

class BST
{
private:
struct node
{
int data;
node *left;
node *right;
};
node* start;

public:
BST(){start=NULL;}
void insert(int data);
void traverse();
void inorder(node *);
void preorder(node *);
void postorder(node *);
};

void BST:: insert(int d)
{
node *newnode;
newnode=new node;
newnode->data=d;
newnode->left=NULL;
newnode->right=NULL;

node *ptr;
ptr=start;
if(start==NULL)
{
start=newnode;
}
else
{
while(ptr!=NULL)
{
if(d<ptr->data)
{
if(ptr->left==NULL)
{
ptr->left=newnode;
ptr=NULL;
}
else
{
ptr=ptr->left;
}
}
else
{
//if(d>ptr->data)
//{
if(ptr->right==NULL)
{
ptr->right=newnode;
ptr=NULL;
}
else
{
ptr=ptr->right;
}
// }
}
}
}
}

void BST::traverse()
{
cout<<"\n Inorder:\n";
inorder(start);
cout<<"\n Preorder:\n";
preorder(start);
cout<<"\n Postorder:\n";
postorder(start);

}
void BST::inorder(node *ptr)
{
if(ptr!=NULL)
{
inorder(ptr->left);
cout<<ptr->data<<"\t";
inorder(ptr->right);
}
}
void BST::preorder(node *ptr)
{
if(ptr!=NULL)
{
cout<<ptr->data<<"\t";
preorder(ptr->left);
preorder(ptr->right);
}
}
void BST::postorder(node *ptr)
{
if(ptr!=NULL)
{
postorder(ptr->left);
postorder(ptr->right);
cout<<ptr->data<<"\t";

}
}


int main()
{
BST b;
b.insert(8);
b.insert(3);
b.insert(10);
b.insert(1);
b.insert(6);
b.insert(7);
b.insert(4);
b.insert(10);
b.insert(14);
b.insert(13);

b.traverse();




return 0;
}
/*
$ g++ bst_revision.cxx
$ ./a.out

Inorder:
1 3 4 6 7 8 10 10 13 14
Preorder:
8 3 1 6 4 7 10 10 14 13
Postorder:
1 4 7 6 3 13 14 10 10 8
*


Depth-first
  • Pre-order traversal sequence: F, B, A, D, C, E, G, I, H (root, left, right)
  • In-order traversal sequence: A, B, C, D, E, F, G, H ,I (left, root, right)
  • Post-order traversal sequence: A, C, E, D, B, H, I, G, F (left, right, root)
Breadth-first
  • Level-order traversal sequence: F, B, G, A, D, I, C, E, H
Inorder:
1 2 3 4 5 6 7 8 9
Pre order:
6 2 14 3 5 7 9 8
Post order:
1 3 5 4 2 8 9 7 6
Simples Way to learn Data Structure in C++ CPP or C

How to write cpp program to reverse a linked list



//C++ program for reversing single Linked list nodes 
Cpp program for implement reversing Single Linked List
Single linked list program for reversing the nodes
How to reverse linked list 
#include<iostream>
using namespace std;

struct node
{
node *next;
int data;
};
class ll
{
private:
node *start;
public:
ll(){start=NULL;}

void insert(int a)
{
cout<<"Inside Insert."<<endl;
node *newnode=new node;
newnode->data=a;
newnode->next=start;
start=newnode;
//return;
}
void display();
void del();
void reverse();
};

How to write sorted linked list program in cpp with class


How to write sorted linked list program in cpp with class
C++ Program for sorted single linked list with class
//sorted linked list latest
#include<iostream>
using namespace std;
struct node
{
int data;
node *next;
};
node *start=NULL;

void insert(int d)
{
node *newnode=new node;
newnode->data=d;
newnode->next=NULL;
//insert as first elt
if(start==NULL)
{

How to see vtable virtual table for virtual functions in cpp


Virtual table is look up table for virtual functions. It contains the function pointer to the virtual functions. Virtual table is created for each class having one or more virtual function and classes derived from that class.virtual pointer better known as vptr containing the address of vtable is created for the most base class thats why size of empty class with virtual function is equal to the size of a pointer(8 bytes for a 64 bit computer)while size of a empty class without any virtual functions is only one byte.
Command to see vtable/virtual table of a class.
Here if the name of the program is "classWithVf.cxx" so the command to see the vtable of the class is:

g++ -fdump-class-hierarchy classWithVf.cxx


#include<iostream>
using namespace std;

class M //empty class
{
public:
void f() {}
};
class A //empty class
{