C++ Interview Questions

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

    What are Polymorphic Classes?

    Khansnaina

    • Sep 20th, 2019

    I want implications of polymorphic use of classes

  •  

    C++ program to expand the given string

    C++ program to expand the following string
    aa2b4c3d
    o/p should be :
    aabbccccddd

    Sai Vamshi

    • Nov 25th, 2018

    Code
    1. #include <iostream>
    2. #include<string.h>
    3. using namespace std;
    4.  
    5. int main()
    6. {
    7.     char *str;
    8.     cin>>str;
    9.     int len = strlen(str);
    10.     for(int i=0;i<len;i+=2){
    11.             for(int j=0;j<(str[i+1]-0);j++){
    12.                 cout<<str[i];
    13.             }
    14.     }
    15.     return 0;
    16. }
    17.  

    Jin Hyuk Cho

    • Feb 3rd, 2016

    A working example"cpp #include #include #include #include using namespace std; string expand (string compressed) { locale loc; stringstream ss; // g...

  •  

    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 27th, 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 17th, 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...

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