C++ Interview Questions

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

    What is the use of constructor?

    Avinash Patil

    • Oct 30th, 2018

    Constructor is a member function of a class which is used to initialize the object.
    Types:
    1) Copy Constructor
    2) Parameterless constructor
    3) Parameterrized constructor
    4) Default constructor

    Anshu Shrivastava

    • Aug 27th, 2018

    When we want to assign some values for the data member of the class then we make our own constructor. like-> class Abc { int i; }; int main() { Abc b1;//object of Abc class,it will call by def...

  •  

    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 the difference between Function and Member function?

    KAIRAV

    • Feb 26th, 2018

    NO BRO!! A member function can be defined outside the class

  •  

    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

  •  

    Write Object into a File

    How will you write object into file using file concepts in C++?

    Arman

    • Jan 25th, 2018

    You can write the object into file after serializing it. It is important to note that there are two types of objects: "persistence" and "non persistence". persistence objects are those which can be serialized and saved in file system.

    hi

    • Nov 9th, 2016

    I think the final fobj.display_detail(); in your code needs to change to eobj.display_detail();

  •  

    What is the difference between function overloading and operator overloading?

    Krishna Agrawal

    • Dec 7th, 2017

    In order to overload function ex: void add(int x=5, int y=8) now just call in mains add(), then call add(5), then call add(3,3) in three instances function will work accordingly but in operator overlo...

    Zebo Li

    • Jan 26th, 2017

    Is the following example a function overloading?
    double max(double, double)
    int max(int, int)
    They have different return types.

  •  

    Abstract class - can we create pointer to abstract class?

    Sushant Narayan

    • Sep 7th, 2017

    Yes, we can create pointer to abstract class.

    matthew

    • Sep 27th, 2016

    No you can make a pointer to an instance of a concrete class that derives from the abstract class, but you cant have a pointer to a class itself unless you mean some sort of metaprogramming type logic.

  •  

    What is difference between copy constructor and constructor?

    Amiya Pani Kjr Odisa

    • Aug 16th, 2017

    Constructor create object. From scratch but Copy constructor create from existing object. Constructor creates new object. Copy constructor creates clone of existing object.

    Anurag Verma

    • Aug 7th, 2006

    Constructor is called when an object is created.Copy constructor is called when the copy of an object is made. For e.g. passing parameter to function by value, function returning by value. Copy constructor takes the parameter as const reference to the object.

  •  

    What is polymorphism? Explain with an example?  

    "Poly" means "many" and "morph" means "form". Polymorphism is the ability of an object (or reference) to assume (be replaced by) or become many different forms of object. Example: function overloading, function overriding, virtual functions. Another example can be a plus ‘+’ sign, used for adding two integers or for using it to concatenate two strings.  

    Tech4 u

    • Jul 22nd, 2017

    The ability to define more than one function with the same name is called polymorphism.

    This provides the facility of sending the same message to objects of parent class and objects of the sub classes.

    sunil

    • Feb 26th, 2017

    Several forms having same name is called polymorphism.
    We have two types of polymorphism in C++.
    1) Compile time Polymorphism or function overloading
    2) Runtime Polymorphism or function overriding

  •  

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

  •  

    C++ Macros Functionality

    What is the main thing that C++ macros can do that cannot be done with functions? Give a realistically useful example of something of this kind.

    Macros have the distinct advantage of being more efficient than functions, because their corresponding code is inserted directly into your source code at the point where the macro is called. There is no overhead involved in using a macro like...

    Joe Fitzgerald

    • Jun 5th, 2017

    Use macros for an "include guard" only to avoid duplicate definition of types. Macros should be avoided in most other cases in C++ since they are not type safe, fail to respect scoping rules, and the...

    EigenCritic@gmail.com

    • Apr 6th, 2016

    People usually say that using macros in C++ should be avoided. But there are exceptions, especially regarding meta programming, debugging, profiling and for cross-platform code switches. You can use...

  •  

    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.

  •  

    Flowchart

    an electricity board charges the following rates to domestic users to discourage large consumption of energy:
    for the first 100 units - 10 paise per unit 
     for next 200 units - 20 paisa per unit
     beyond 300 units - 30 paisa per unit . if the total cost is more than 10 OMR then an additional surcharge of 15% is added.draw the flowchart and write the algorithm to calculate the...

    Ted

    • Oct 17th, 2016

    1 start
    2 Input U for unit
    3 If U

  •  

    Array in Function

    Write C++ program tell the user
    Press 1 to change Row to Row and change (using Function ) output like that :
    1 1 1
    2 2 2
    3 3 3
    and press 2 to change Row to Col and change (using function) output like that :
    1 2 3
    1 2 3
    1 2 3
    and press 0 to End the Program

    mayank

    • Sep 7th, 2016

    Code
    1. #include <iostream>
    2.  
    3. using namespace std;
    4.  
    5. void rowtorow()
    6. {
    7.     for(int i=0;i<3;++i)
    8.     {
    9.         for(int j=0;j<3;++j)
    10.         {
    11.             cout<<i+1<<" ";
    12.         }
    13.         cout<<"
    14. ";
    15.     }
    16. }
    17. void rowtocol()
    18. {
    19.     for(int i=0;i<3;++i)
    20.     {
    21.         for(int j=0;j<3;++j)
    22.         {
    23.             cout<<j+1<<" ";
    24.         }
    25.         cout<<"
    26. ";
    27.     }
    28. }
    29. void close(){
    30.  
    31. }
    32.  
    33. typedef void (*fp)();
    34. int main()
    35. {
    36.     const fp arr[3]={close,rowtorow,rowtocol};
    37.     int i;
    38.     cout<< " enter the number 0,1,or 2:";
    39.     cin>>i;
    40.     arr[i]();
    41.  
    42.     cout << "
    43. Hello world!" << endl;
    44.     return 0;
    45. }
    46.  

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