C++ Interview Questions

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

    What is importance of const. pointer in copy constructor?

    Krishna Kumar

    • Sep 25th, 2018

    There is three query for the above question, 1) Why reference object passed as parameter to the copy constructor? 2) Why pointer object is not passed as parameter to the copy constructor? 2) What r...

  •  

    What is the difference between macro and inline()?

    Venkataramakrishna

    • Mar 22nd, 2018

    Macro does not check for the data type. The inline function will check. You can enable or disable macros. in inline, depending on the requirement you can do this.

    Naman

    • Nov 14th, 2017

    You pass some flags to the compiler to force inline or use always_inline attribute with GCC

  •  

    Why C++ does not have Virtual Constructors?

    Star Read Best Answer

    Editorial / Best Answer

    chethanhb  

    • Member Since Feb-2010 | Feb 25th, 2010


    I am one of the person who has pricked my head in understanding the above question. Let me explain in my way...

    If you have virtual function or (let me take just "virtual" keyword as of now), it means that this member function can be redefined by derived class. When we have a virtual function in a class, it will have vtable (virtual table). This vtable will have the address of the virtual function which is defined in derived.
    Now the point is, how you invoke the virtual function which is defined in derived class with the help of object of the class?
    Once you create an object of class, vptr (virtual pointer) will be created. Which inturn will be pointing to the base address of the vtable. So now it is clear that you can invoke the virtual funtion of derived class only with an object that has created.
    Now coming to virtual constructor,
    If we make constructor as virtual in base, it means that it could be redefined in derived. Keep in mind that constructor is invoked during object creation (object is not created yet. still it is in the status "creating". Object will create only after executing constructor part code). Assume you are trying to create object of the class which has virtual constructor. During this process constructor of the class will be invoked. It looks for virtual keyword. Now it tries to look for virtual constructor in derived. But not possible bcz there is no vptr and no vtable avaibale at this point of time. So, when object is not created, then there is no vptr. If no vptr for this object, then how the consturtor of derived is invoked?. No address of this construtor will available in vtable.
    Hence there is no point in having virtual constructor.

    I believe that i have cleared your doubt.

    Venkataramakrishna

    • Mar 22nd, 2018

    Whenever a class object is created this pointer is created just before initializing class constructor member values. If the constructor is virtual, you cannot create this pointer.

    vikash tiwary

    • May 11th, 2017

    Virtual functions are used in order to invoke functions based on the type of object pointed by the pointer. But is not "invoked", it is called only once when it is declared. so, a constructor cannot be virtual.

  •  

    What is a class?

    Class is a user-defined data type in C++. It can be created to solve a particular kind of problem. After creation the user need not know the specifics of the working of a class.  

    Star Read Best Answer

    Editorial / Best Answer

    Answered by: hirma

    • Sep 18th, 2005


    class is a userdefined datatype which consists of attributes(datamembers) and behavior(member functions).Bydefault the members of a class is private.

    Nikita

    • Jan 23rd, 2018

    Class is a user define data type which contains the variable and function which r of two type private and public where the public class members can be accessed from any other class but the private cla...

    Atul

    • Dec 18th, 2017

    Class is same as structure in C, but in Class we have functions

  •  

    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 is the difference between class and structure?

    Structure: Initially (in C) a structure was used to bundle different type of data types together to perform a particular functionality. But C++ extended the structure to contain functions also. The major difference is that all declarations inside a structure are by default public. Class: Class is a successor of Structure. By default all the members inside the class are private.

    Star Read Best Answer

    Editorial / Best Answer

    sumitv  

    • Member Since Sep-2008 | Sep 4th, 2008


    Structure does support inheritance.

    try out the following code.

    #include<iostream>

    using namespace std;

    struct Base
    {
        int A;
    };

    struct Derived:public Base
    {
        int B;
        void display();
    };

    void Derived::display()
    {
        cout<<endl<<"A = "<<A<<endl;
    }

    int main()
    {
        Derived D;
        D.A = 111;
        D.display();
        getchar();
        return 0;
    }

    Try out private and protected inheritance as well. It works. :)

    Regards,
    Sumit

    Sajeed Ullah

    • Dec 2nd, 2016

    All data members of the class is by default private,
    Whereas all data members of structure is by default public.

    Ashish

    • Mar 24th, 2016

    Beware, whatever is supported by a class is also supported by struct whether its inheritance, polymorphism, encapsulation, data hiding etc. etc. as of C++ 11. Only difference is, for a class default access specifier is private whereas for struct its public.

  •  

    Can we use static variables in file2 if they are defined in file1 ? If yes, then how ?

    Star Read Best Answer

    Editorial / Best Answer

    Answered by: Kranthi Kiran

    • Dec 2nd, 2006


    We cannot use a 'static variable' declared in one file and use it in file 2.Reason: The purpose of 'static' keyword is to retain the value betwwen the function and to restrict thescope of the variable to the file.When a variable is declared as static it will be given memory in global area portion of the process segments with the variable mname prefixed with the file namefor Example: static int k; implies it is stored as "filename.k" in global segment.so if you try to use it in another file the compiler finds the variable prefix and flags out an error.

    Vineet Srivastava

    • Nov 13th, 2015

    Syntatically you cannot as static is meant for one file access only. But still if you want - you can do it by passing it by reference to another file through some function.

    shreekant

    • Nov 5th, 2015

    Nice explained by Kalayama "We cant have two storage specifiers for a single Variable declaration. If one needs to use the variable in multiple files, then he needs to declare it has extern." Its simple thing.

  •  

    What is public, protected, private?

    Public, protected and private are three access specifiers in C++. 1. Public data members and member functions are accessible outside the class.2. Protected data members and member functions are only available to derived classes.3. Private data members and member functions can’t be accessed outside the class. However there is an exception can be using friend classes.

    ankit

    • Jun 26th, 2015

    Can we use protected specifier first than public than private in a class definition in c++ by making class

  •  

    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.  

  •  

    Static Polymorphism

    What is Static Polymorphism? Give its syntax and simple example.

    rdl

    • Dec 28th, 2014

    The call to a function is resolved at compile time. Eg., function overloading and templates.

    Youssef Arbach

    • Dec 12th, 2014

    Static polymorphism could be seen also with Templates, since the selection of the write function is done at compile time. Usually with inheritance (and call-by ref sure) polymorphism is dynamic, unless it is a call by-value, then there would be no morphism at all.

  •  

    Inheritance in C++

    While doing inheritance in C++, one thing that is not inherited is what?

    rohit suri

    • Sep 11th, 2014

    There are four things which r not inherited in c++
    1>Constructor
    2>Destructor
    3>Assignment operator
    4>copy constructor
    5>Friend function

    Johnny

    • Jan 7th, 2014

    Friend declarations.

  •  

    What is out put of C++ code?

    cclass base
    {
    public:
    virtual void display(int i = 10)
    {
    cout

    rohit suri

    • Sep 11th, 2014

    Base class display method will called

    reddy-

    • Apr 10th, 2013

    Overriding function default argument takes more precedence ,Hence based class default argument 10 will be initialized to i.So i value holds 10 will be printed for this call with derived count statement.

  •  

    What are the disadvantages of C++?

  •  

    What is const int *const ptr?

    harshitha

    • Feb 8th, 2014

    Ptr is a constant pointer to constant integer. Both the values cant be changed.
    eg:
    const int a=10;
    const int *const ptr=&a;
    int b=20;
    const int *const ptr=&b;//error
    a=25;//error

    Jagadeesh Arikuti

    • Jul 5th, 2012

    Both are Constant
    const int i=10;
    const int * const ptr=&i;

    You are not allowed to change int i are pointer ptr

  •  

    What is abstraction?

    Abstraction is of the process of hiding unwanted details from the user. 

    Ramya

    • May 31st, 2013

    Mechanism of exposing only the interface and hiding the implementation data from user.

    mukrram ur rahman

    • Dec 10th, 2012

    Abstraction exactly involve the process of involving relevant things while ignoring the irrelevant things.

  •  

    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. }

  •  

    How is static variable stored in the memory?( if there are 2 functions in a file,and the static variable name is same (ex var) in both the function. how is it keep seperately in the memory).

    Bishoy

    • Oct 20th, 2012

    Local variables are stored in stack - each method have a stack frame (a fixed size of memory in the stack) that is allocated for local variables each time the method is called, and deallocated when th...

    Ramanuj

    • Oct 20th, 2012

    Hi,

    I have a query. The lifetime of the static variable is the entire program - if it is a local auto variable (i.e., not dynamically allocated), will it contribute to the peak memory ?

    Regards,
    Ramanu

  •  

    Write a simple program for finding factorial in C++ ?


    sakshi

    • Oct 4th, 2012

    Code
    1. class factorial
    2. {
    3. public static void main(String args[])
    4. int f=1,i,n;
    5. n=Convert.ToInt32(Console.ReadLine());
    6. for(i=1;i<=n;i++)
    7. {
    8. f=f*i;
    9. }
    10. Console.WriteLine("factorial of the no :"+f);
    11.  
    12. Console.ReadKey();
    13. }

    Anugya Singh

    • Sep 4th, 2012

    Code
    1. #include<iostream.h>
    2. #include<conio.h>
    3. void main()
    4. {
    5. clrscr();
    6. int a,i,f=1;
    7. cout<<"Enter the Number whose factorial value is to be find
    8. ";
    9. cin>>a;
    10. for(i=a;i>0;i--)
    11. {
    12. f=f*i;
    13. }
    14. cout<<"Fctorial is "<<f;
    15. getch();
    16. }

  •  

    Can destructor be private?

  •  

    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;

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