C++ Interview Questions

Showing Questions 21 - 29 of 29 Questions
First | Prev | Next | Last Page
Sort by: 
 | 
Jump to Page:
  •  

    What is Namespace?

    Girish

    • Jul 20th, 2017

    We cannot have two variables with the same name in the same scope. To resolve this namespace is introduced. Namespace is a declarative region that provides a scope to the identifiers inside it. Acces...

    dinu

    • Oct 3rd, 2016

    Suppose we have two libraries lib1 and lib2. let there is a function add() present in both of them, this function can have different meaning or functionality in both libraries but they have same name...

  •  

    What are the different way of creating object in c++ ?

    NikunjSingh

    • Feb 23rd, 2012

    1. using normal object creation: Shape obj; 2. copy ctor: Shape obj2=obj;// or Shape obj3(obj); 3. using new operator;

    Code
    1. #include <iostream>
    2.  
    3. using namespace std;
    4.  
    5. class Shape{
    6. public:
    7.         Shape(){cout<<"Shape..."<<endl;}
    8.         Shape(const Shape& obj)
    9.         {
    10.                 cout<<"Copy called"<<endl;
    11.         }
    12.         void* operator new(size_t size)
    13.         {
    14.                 cout<<"new...."<<endl;
    15.                 void* storage=malloc(size);
    16.                 if(NULL == storage) {
    17.                         throw "allocation fail : no free memory";
    18.                 }
    19.                 return storage;
    20.         }
    21.  
    22.         void operator delete (void*){
    23.  
    24.         }
    25. };
    26.  
    27. int main()
    28. {
    29.         Shape obj;
    30.         Shape obj2=obj;
    31.         Shape obj3(obj);
    32.         Shape* ptrShape=new Shape();
    33.         Shape* ptrShapeOver= new Shape();
    34.         return 0;
    35. }

    Isaac

    • Feb 21st, 2012

    1. Create on stack. i.e. ClassA varA;
    2. Create on heap. i.e. ClassA *ptrA = new ClassA;

  •  

    What is memory leaking in c++ ?

    ajrobb

    • Sep 22nd, 2010

    A memory leak in C++ is generally caused by not understanding the language.The std::auto_ptr<class> or similar should be used whenever allocating objects on the heap using new. This is essential...

  •  

    Matrix Operation using Operator Overloading

    Write a program for four atrithmetic operations in matrix using operator overloading.

    JoolsL

    • Nov 19th, 2012

    In both the above answers the op overloads should be using a const ref as input. You should have a (private) ctor that makes the matrix from the data, and should use RVO in the return.

    binoy

    • Aug 23rd, 2012

    Code
    1. #include<iostream.h>
    2. #include<conio.h>
    3. class matrix
    4. {
    5.   private:long m[5][5];
    6.   int row;int col;
    7.   public:void getdata();
    8.   int operator ==(matrix);
    9.   matrix operator+(matrix);
    10.   matrix operator-(matrix);
    11.   friend ostream & operator << (ostream &,matrix &);
    12. };
    13. /* function to check whether the order of matrix are same or not */
    14. int matrix::operator==(matrix cm)
    15. {
    16.   if(row==cm.row && col==cm.col)
    17.   {
    18.     return 1;
    19.   }
    20.   return 0;
    21. }
    22. /* function to read data for matrix*/
    23. void matrix::getdata()
    24. {
    25.   cout<<"enter the number of rows
    26. ";
    27.   cin>>row;
    28.   cout<<"enter the number of columns
    29. ";
    30.   cin>>col;
    31.   cout<<"enter the elements of the matrix
    32. ";
    33.   for(int i=0;i<row;i++)
    34.   {
    35.     for(int j=0;j<col;j++)
    36.     {
    37.        cin>>m[i][j];
    38.     }
    39.   }
    40. }
    41. /* function to add two matrix */
    42. matrix matrix::operator+(matrix am)
    43. {
    44.   matrix temp;
    45.   for(int i=0;i<row;i++)
    46.   {
    47.     for(int j=0;j<col;j++)
    48.     {
    49.       temp.m[i][j]=m[i][j]+am.m[i][j];
    50.     }
    51.     temp.row=row;
    52.     temp.col=col;
    53.   }
    54.   return temp;
    55. }
    56. /* function to subtract two matrix */
    57. matrix matrix::operator-(matrix sm)
    58. {
    59.   matrix temp;
    60.   for(int i=0;i<row;i++)
    61.   {
    62.     for(int j=0;j<col;j++)
    63.     {
    64.       temp.m[i][j]=m[i][j]-sm.m[i][j];
    65.     }
    66.     temp.row=row;
    67.     temp.col=col;
    68.   }
    69.   return temp;
    70. }
    71. /* function to display the contents of the matrix */
    72. ostream & operator <<(ostream &fout,matrix &d)
    73. {
    74.   for(int i=0;i<d.col;i++)
    75.   {
    76.     for(int j=0;j<d.col;j++)
    77.     {
    78.       fout<<d.m[i][j];
    79.       cout<<" ";
    80.     }
    81.     cout<<endl;
    82.   }
    83. return fout;
    84. }
    85. /* main function */
    86. void main()
    87. {
    88.   matrix m1,m2,m3,m4;
    89.   clrscr();
    90.   m1.getdata();
    91.   m2.getdata();
    92.   if(m1==m2)
    93.   {
    94.     m3=m1+m2;
    95.     m4=m1-m2;
    96.     cout<<"Addition of matrices
    97. ";
    98.     cout<<"the result is
    99. ";
    100.     cout<<m3;
    101.     cout<<"subtraction of matrices
    102. ";
    103.     cout<<"The result is
    104. ";
    105.     cout<<m4;
    106.   }
    107.   else
    108.   {
    109.     cout<<"order of the input matrices is not identical
    110. ";
    111.   }
    112.   getch();
    113. }

  •  

    Constructor initialization lists

    What is advantage of using Constructor initialization lists?

    abc

    • Feb 5th, 2012

    --> initializer list is used with constructor when any const datas are used in a class. --> bcoz, as we known const var should be initialized while declaring. --> the class gets memory allocatio...

  •  

    What is proper and improper inheritance? Explain with an example?

    Star Read Best Answer

    Editorial / Best Answer

    michaelpyles  

    • Member Since Dec-2007 | Dec 29th, 2007


    Proper inheritance occurs when the derived class "IS A" specialized type of the base class.  Example Cat IS A Animal.
    Improper inheritance occurs when a class is inherited from merely for code reuse, without having any other relationship.  Example Cat Inherits from Engine.  A Cat is not an engine however both an engine and a cat purr. 

    yzesong

    • Jul 31st, 2009

    As long as no diamond problems (improper inheritance),  single or multiple inheritance can all be proper inheritance. When usng multiple inheritance with diamond problem, you can use virtual inheritance to make the multiple inheritance a proper one.

  •  

    Difference between "vector" and "array"?

    bhradwj

    • Apr 4th, 2006

    The main difference between vector and array isAlways vector is represented as uni-dimensional (ie., one dimensional) but whereas Array can be represented by both one-dimensional as well as two dimensional.Thanks....

  •  

    How do you write a program which produces its own source code as its output?

    Ashish

    • Jan 7th, 2007

    include<iostream>#include<fstream>using namespace std;int main(){             fstream *obj = new fstream();    &n...

    Jayashree

    • Jul 26th, 2006

    #include "stdafx.h"#include "string.h"#include "process.h"int main(int argc, char* argv[]){ char cmd[125]; strcpy(cmd, "type "); strcat(cmd, __FILE__); system(cmd); return 0;}

  •  

    Binary

    Write a program that continues to ask the user to enter any decimal number and calculate it’s binary,octal and hexadecimal equivalent.

    Badugu

    • Mar 11th, 2015

    Please try following:

    Code
    1. #include<iostream>
    2. #include<fstream>
    3. #include <bitset>
    4.  
    5. using namespace std;
    6.  
    7. int main()
    8. {
    9.     int i,j;
    10.     cout<<"Enter a decimal Number:";
    11.     while (cin>>i) {
    12.                  
    13.           cout<<"Hexa Decimal:"<<std::hex<<i<<"     Octal:"<<std::oct<<i<<endl;
    14.           std::bitset<8> x(i);
    15.           cout<<"Binary:"<<x<<endl;
    16.           cout<<"Enter a decimal Number:";
    17.           }
    18.          
    19.           return 0;
    20.           }
    21.  

    rohit suri

    • Sep 11th, 2014

    Code
    1.  
    2. int binary_function(int num)
    3. {
    4.    if(num==1)
    5.       return 1;
    6.   else
    7.     return binary_function(num/2)*10+(num%2);
    8. }
    9.  

Showing Questions 21 - 29 of 29 Questions
First | Prev | Next | Last Page
Sort by: 
 | 
Jump to Page: