Learn C++ From Interview Questions

Why take this course?
-
Initializing data members when the class is created: It is logical and common practice to initialize data members at the point of class creation or within a constructor, as this ensures that the object is in a consistent state from the beginning.
-
Initializing public const data member from outside the class: No, you cannot directly initialize a
const
data member from outside the class becauseconst
data members are typically initialized within the class itself, usually in the constructor. However, you can set aconst
data member once through a mutator function after object creation. -
Class containing another class's object in its private section: Yes, a class can declare an object of another class as a private member.
-
Creating an object of an anonymous/nameless class: No, you cannot directly create an object of an anonymous class (a class defined without a name). You can only create objects of named classes. Anonymous classes are used in specific contexts like lambda expressions or local class definitions in C++.
-
Default assignment operator and overloading: Yes, the default assignment operator is automatically generated by the compiler for each class that defines the default constructor. It does not perform deep copying; it simply checks for self-assignment (
this == &other
) and does nothing if that's the case. Overloaded assignment operators need to be explicitly defined by the user if they want custom behavior during assignment. -
Default overloading of '+': No, the default behavior for the
+
operator is not automatically overloaded for user-defined types. You need to explicitly override the+
operator (as a non-member or as a member function) for your classes if you want it to behave in a custom way. -
Assigning an integer value to an object: No, you cannot directly assign an integer value to an object of a user-defined class unless the class has an overloaded assignment operator or a constructor that accepts an integer as a parameter. Classes are reference types (except for built-in types like
int
,float
, etc.), so they can't be assigned primitive values directly. -
Concept of this pointer: The
this
pointer in C++ is a keyword representing the current instance of the class. It is used to differentiate between class member and non-member functions, and to access class members within member functions. -
Member functions accessing data members: Member functions can only access the data members of their own class directly. However, they can access data members of other objects if those are passed as arguments or if they have been granted access through mechanisms like pointers-to-members, friend declarations, or inheritance.
-
Forward referencing: Yes, forward referencing allows a class to be defined before it is fully declared, provided that neither the forward declaration nor the full definition of the class is complete in such a way that would require a member of the other to be incomplete (e.g., as a friend).
-
Friend functions and classes: A friend function is not a member of a class but has access to its private and protected members, similar to a member function. A friend class can access all members of another class as if they were friends of the friend class.
-
Dynamic cast operator: The
dynamic_cast
operator in C++ is used at runtime to safely convert a pointer or reference to a different type and check its type against a typeid object. It does not provide information about an rvalue or non-pointer object. -
Converting a const integer variable to non-const: In C++,
const
is a qualifier that indicates that the value of a variable should not be modified after it has been initialized. Once a variable is declared asconst
, it cannot be redeclared or modified. -
Composition vs. inheritance: Composition and inheritance are two different mechanisms for code reuse, with composition generally being preferred over inheritance for better design encapsulation. With composition, you include objects of other classes rather than deriving from them.
-
Friend function accessing public/private data members: A friend function is associated with a class and has access to the class's public and private members alike. There is no restriction on what a friend function can access within the class it is a friend of.
-
Templates: Templates are indeed used for both user-defined types and built-in types. Function templates and template classes are distinct but related concepts. Templates do not save memory at compile time because they are instantiated at link time or runtime, and the code is generated specifically for each data type.
-
Template default values: Template template parameters (template class instantiations with another template as a parameter) cannot be given default values. Function templates can have default arguments, but these are evaluated at compile time, not at link time or runtime.
-
Overloading function templates: Yes, function templates can be overloaded in the same way regular functions can, by having different signatures. Overloaded templates allow for a more specific version of a template to be chosen at compile time based on the types and number of arguments passed.
Course Gallery




Loading charts...