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. "Enter a decimal Number:""Hexa Decimal:"<<std::hex<<i<<"     Octal:""Binary:""Enter a decimal Number:"

    rohit suri

    • Sep 11th, 2014

    Code
    1.  

  •  

    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. /* function to check whether the order of matrix are same or not *//* function to read data for matrix*/"enter the number of rows
    4. ""enter the number of columns
    5. ""enter the elements of the matrix
    6. "/* function to add two matrix *//* function to subtract two matrix *//* function to display the contents of the matrix */" "/* main function */"Addition of matrices
    7. ""the result is
    8. ""subtraction of matrices
    9. ""The result is
    10. ""order of the input matrices is not identical
    11. "

  •  

    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

  •  

    Can destructor be private?

  •  

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

    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: