C++ Viva Questions By:yatendra Kashyap
C++ Viva Questions By:yatendra Kashyap
C++ Viva Questions By:yatendra Kashyap
1. What is a class?
Class is concrete representation of an entity. It represents a group of objects, which hold similar attributes and
behavior. It provides Abstraction and Encapsulations. Classes are generally declared using the keyword class.
Object represents/resembles a Physical/real entity. An object is simply something you can give a name.
3. What is Encapsulation?
Encapsulation is binding of attributes and behaviors. Hiding the actual implementation and exposing the
functionality of any object. Encapsulation is the first step towards OOPS, is the procedure of covering
up of data and functions into a single unit (called class). Its main aim is to protect the data from out
side world
4. What is Abstraction?
Hiding the complexity. It is a process of defining communication interface for the functionality and
5. What is Overloading?
Adding a new method with the same name in same/derived class but with different number/types of
6. What is Inheritance?
It is a process in which properties of object of one class acquire the properties of object of another
class.
An abstract class is a special kind of class that cannot be instantiated. It normally contains one or more
1. Public: The data members and methods having public as access outside the class.
2. Protected: The data members and methods declared as protected will be accessible to the
3. Private: These data members and methods will be accessibl not from objects created outside
the class.
The extra time needed and the process of saving is valid for larger functions. If the function is short,
the programmer may wish to place the code of the function in the calling program in order for it to
be executed. This type of function is best handled by the inline function. In this situation, the
programmer may be wondering “why not write the short code repeatedly inside the program
wherever needed instead of going for inline function?” Although this could accomplish the task, the
problem lies in the loss of clarity of the program. If the programmer repeats the same code many
times, there will be a loss of clarity in the program. The alternative approach is to allow inline
functions to achieve the same purpose, with the concept of functions.
17 What is preprocessor?
The preprocessor is used to modify your program according to the preprocessor directives
in your source code. Preprocessor directives (such as #define) give the preprocessor specific
instructions on how to modify your source code. The preprocessor reads in all of your
include files and the source code you are compiling and creates a preprocessed version of
your source code. This preprocessed version has all of its macros and constant symbols
replaced by their corresponding code and value assignments. If your source code contains
any conditional preprocessor directives (such as #if), the preprocessor evaluates the
condition and modifies your source code accordingly.
The preprocessor contains many features that are powerful to use, such as creating macros,
performing conditional compilation, inserting predefined environment variables into your
code, and turning compiler features on and off. For the professional programmer, in-depth
knowledge of the features of the preprocessor can be one of the keys to creating fast,
efficient programs.
18 What is difference between visual c++ & ANSI c++?
Don't get confused with the words visual and ANSI. Follow this.
Visual C++ is an IDE for developing GUI(graphical user interface) real time applications
using++. Visual C++ deals with more complex applicationsANSI C++ is the developed
version of C++'s earlier versions. The thing is that the libraries are updated with new
functions compared to previous versions
19 What is the difference between overloading and overriding?
Overloading - Two functions having same name and return
Type, but with different type and/or number of arguments.
Overriding - When a function of base class is re-defined in
the derived class.
20 C++ Memory Management operators
The concept of arrays has a block of memory reserved. The disadvantage with the concept of
arrays is that the programmer must know, while programming, the size of memory to be
allocated in addition to the array size remaining constant. In programming there may be
scenarios where programmers may not know the memory needed until run time. In this case,
the programmer can opt to reserve as much memory as possible, assigning the maximum
memory space needed to tackle this situation. This would result in wastage of unused memory
spaces. Memory management operators are used to handle this situation in C++ programming
language.
21 What are memory management operators?
• No return type
• class Test
int a,b;
Test()
a=9;
b=8;
};
Constructor which initializes it's object member variables ( by shallow copying) with
another object of the same class. If you don't implement one in your class then compiler
for example:
○ Test t1(10); // calling Test constructor
• when you construct an object based on another object of the same class
Constructor with no arguments or all the arguments has default values. In Above Question Test() is a
default constructor
As the name suggests, the function acts as a friend to a class. As a friend of a class, it can access its
private and protected members. A friend function is not a member of the class. But it must be listed in
A scope resolution operator (::), can be used to define the member functions of a class outside the
class.
A pure virtual member function is a member function that the base class forces derived classes to
provide. Normally these member functions have no implementation. Pure virtual functions are equated
{
public: virtual void draw() = 0;
};
It permits code reusability. Reusability saves time in program development. It encourages the reuse of
proven and debugged high-quality software, thus reducing problem after a system becomes functional
30 What are virtual functions? Describe a circumstance in which virtual functions would be appropriate
Virtual functions are functions with the same function prototype that are defined throughout a class
hierarchy. At least the base class occurrence of the function is preceded by the keyword virtual. Virtual
functions are used to enable generic processing of an entire class hierarchy of objects through a base
class pointer. For example, in a shape hierarchy, all shapes can be drawn. If all shapes are derived from
a base class Shape which contains a virtual draw function, then generic processing of the hierarchy can
be performed by calling every shape’s draw generically through a base class Shape pointer.
A virtual function must have a definition in the class in which it is declared. A pure virtual function does
not provide a definition. Classes derived directly from the abstract class must provide definitions for the
inherited pure virtual functions in order to avoid becoming an abstract base class