Q. What is C++?
Ans:
C++ is an
object-oriented programming language created by Bjarne Stroustrup. It was
released in 1985.
C++ is a
superset of C with the major addition of classes in C language.
Initially,
Stroustrup called the new language "C with classes". However, after
sometime the name was changed to C++. The idea of C++ comes from the C
increment operator ++.
Q. What are
the advantages of C++?
Ans:
C++ doesn't
only maintains all aspects from C language, it also simplifies memory
management and adds several features like:
Ø
C++ is a
highly portable language means that the software developed using C++ language
can run on any platform.
Ø
C++ is an
object-oriented programming language which includes the concepts such as
classes, objects, inheritance, polymorphism, abstraction.
Ø
C++ has the
concept of inheritance. Through inheritance, one can eliminate the redundant
code and can reuse the existing classes.
Ø
Data hiding
helps the programmer to build secure programs so that the program cannot be
attacked by the invaders.
Ø
Message
passing is a technique used for communication between the objects.
Ø
C++ contains a
rich function library.
Q. What is a class?
Ans:
A class is a blueprint for creating objects. A class
contains methods and variables which are a class instance.
Q. Distinguish between constructor and method abstract
class and interface
Ans:
The difference between class and interface is:
Abstract
class |
Interface |
Abstract class does not support inheritance. |
The interface supports multiple inheritances. |
An abstract class would contain constructor. |
The interface does not contain a constructor. |
An abstract class is declared using the “Abstract”
keyword. |
The interface is declared using the “interface”
keyword. |
It can be used with all access modifiers. |
It can be used with only public access modifier. |
Q. What is an
abstract class?
Ans:
A class having an abstract keyword is called an abstract
class.
Q. What is an
array?
Ans:
An array is a container that keeps a specific number of
similar data types.
Q. What is a
constructor?
Ans:
A constructor is a method that is used to create a class
object.
Q. Define Cin and
Cout
Ans:
Cin and Cout are objects used for input and output files,
respectively.
Q. What is the
difference between C and C++?
Ans:
The difference between C and C++ is:
C |
C++ |
It is a Procedural Oriented language. |
It is an Object-Oriented Programming language. |
C language follows Top-Down programming approach |
C++ follows a bottom-up programming approach. |
The file extension of a C program is .c |
The file extension of a c+ + program language is.cpp |
In the C programming language, a big program code is
divided into small pieces, which is called functions. |
In the C++ programming language, a big program code is
divided into Objects and Classes. |
Structure in C does not provide the feature of function
declaration. |
Structure in C++ provides the feature of declaring a
function as a member function of the structure. |
Q. List the types of constructors?
Ans:
There are two types of the constructor:
1) parameterized constructor and 2) default constructor.
Q. What is
Object Oriented Programming (OOP)?
Ans:
OOP is a
methodology or paradigm that provides many concepts. The basic concepts of
Object Oriented Programming are given below:
Classes and
Objects: Classes are used to specify the structure of the data.
They define the data type. You can create any number of objects from a class.
Objects are the instances of classes.
Encapsulation:
Encapsulation is a mechanism which binds the data and associated operations
together and thus hides the data from the outside world. Encapsulation is also
known as data hiding. In C++, It is achieved using the access specifiers, i.e.,
public, private and protected.
Abstraction: Abstraction
is used to hide the internal implementations and show only the necessary
details to the outer world. Data abstraction is implemented using interfaces
and abstract classes in C++.
Some people
confused about Encapsulation and abstraction, but they both are different.
Inheritance: Inheritance
is used to inherit the property of one class into another class. It facilitates
you to define one class in term of another class.
Q. List out different OOPS principles?
Ans:
The basic OOPS principle are:
1) encapsulation
2) abstraction
3) inheritance and
4) polymorphism.
Q. Explain the
various type of access modifiers
Ans:
There are four types of access modifiers:
- Private: Visible to a particular class
- Public: Visible to the world
- Protected: Visible to specific package
as well as subclass
Q. What is the
difference between compiler and interpreter?
Ans:
The difference between compiler and interpreter is:
Compiler |
Interpreter |
Compiled code run faster. |
Interpreted code run slower. |
Generates output program (in the form of exe), which
can be run independently from the original program. |
Do not generate an output program. So the programmer
evaluates the source program at every time during execution. |
The target program executes independently and does not
require the compiler in the memory. |
The interpreter exists in the memory during
interpretation. |
Difficult to implement as compilers cannot predict what
happens at turn time. |
It is best suited for the program and development environment. |
It takes an entire program as an input. |
It takes a single line of coding as an input. |
Display all errors after compilation, all at the same
time. |
Displays all errors of each line one by one. |
Q. What is a
programming language?
Ans:
A programming language is a collection of grammar rules
to instruct computers or computing devices to perform tasks.
Q. What is
inheritance?
Ans:
Inheritance is an object-oriented programming concept in
which one class derives the properties of the rest of the classes.
Q. What is the
difference between C and C++?
Ans:
Following are
the differences between C and C++:
C |
C++ |
C language was developed by Dennis Ritchie. |
C++ language was developed by Bjarne Stroustrup. |
C is a structured programming language. |
C++ supports both structural and object-oriented
programming language. |
C is a subset of C++. |
C++ is a superset of C. |
In C language, data and functions are the free
entities. |
In the C++ language, both data and functions are
encapsulated together in the form of a project. |
C does not support the data hiding. Therefore, the data
can be used by the outside world. |
C++ supports data hiding. Therefore, the data cannot be
accessed by the outside world. |
C supports neither function nor operator overloading. |
C++ supports both function and operator overloading. |
In C, the function cannot be implemented inside the
structures. |
In the C++, the function can be implemented inside the
structures. |
Reference variables are not supported in C language. |
C++ supports the reference variables. |
C language does not support the virtual and friend
functions. |
C++ supports both virtual and friend functions. |
In C, scanf() and printf() are mainly used for
input/output. |
C++ mainly uses stream cin and cout to perform input
and output operations. |
Q. What is the difference between reference
and pointer?
Ans:
Following are
the differences between reference and pointer:
Reference |
Pointer |
Reference behaves like an alias for an existing
variable, i.e., it is a temporary variable. |
The pointer is a variable which stores the address of a
variable. |
Reference variable does not require any indirection
operator to access the value. A reference variable can be used directly to
access the value. |
Pointer variable requires an indirection operator to
access the value of a variable. |
Once the reference variable is assigned, then it cannot
be reassigned with different address values. |
The pointer variable is an independent variable means
that it can be reassigned to point to different objects. |
A null value cannot be assigned to the reference
variable. |
A null value can be assigned to the reference variable. |
It is necessary to initialize the variable at the time
of declaration. |
It is not necessary to initialize the variable at the
time of declaration. |
Q. What is an Interface?
Ans:
The interface is similar to a class in Java, but it is a
collection of abstract methods. A class can have more than one interface.
Q. Distinguish
between constructor and method?
Ans:
The difference between constructor and method is:
Constructor |
Method |
Constructor is used for initializing the instance of
any class. |
Method is used to perform some operation or function. |
It does not have any return type |
It has a return type. |
The constructor name must be the same as a class name. |
The name of the method can be the same or different as
per need. |
It calls automatically when you create a class object. |
You need to call the method explicitly. |
There is a default constructor which is provided by the
compiler. |
There is no method provided by the compiler. |
Q. What is byte
stream?
Ans:
Byte stream is most usually used to perform input and
output for Unicode having 8 bits.
Q. What do you
mean by destructor?
Ans:
Destructor is a class member function that deletes or
destructs an object.
Q. What is a
class.
Ans:
The class is a
user-defined data type. The class is declared with the keyword class. The class
contains the data members, and member functions whose access is defined by the three
modifiers are private, public and protected. The class defines the type
definition of the category of things. It defines a datatype, but it does not
define the data it just specifies the structure of data.
You can create
N number of objects from a class.
Q. What are the various OOPs concepts in C++?
Ans:
The various
OOPS concepts in C++ are:
- Class:
The class is a
user-defined data type which defines its properties and its functions. For
example, Human being is a class. The body parts of a human being are its
properties, and the actions performed by the body parts are known as functions.
The class does not occupy any memory space. Therefore, we can say that the
class is the only logical representation of the data.
The syntax of
declaring the class:
1. class student
2. {
3. //data members;
4. //Member functions
5. }
- Object:
An object is a
run-time entity. An object is the instance of the class. An object can
represent a person, place or any other item. An object can operate on both data
members and member functions. The class does not occupy any memory space. When
an object is created using a new keyword, then space is allocated for the
variable in a heap, and the starting address is stored in the stack memory.
When an object is created without a new keyword, then space is not allocated in
the heap memory, and the object contains the null value in the stack.
1. class Student
2. {
3. //data members;
4. //Member functions
5. }
The syntax for
declaring the object:
1. Student s = new Student();
- Inheritance:
Inheritance
provides reusability. Reusability means that one can use the functionalities of
the existing class. It eliminates the redundancy of code. Inheritance is a
technique of deriving a new class from the old class. The old class is known as
the base class, and the new class is known as derived class.
Syntax
1. class derived_class :: visibility-mode base_class;
Note: The visibility-mode can be public, private,
protected.
- Encapsulation:
Encapsulation
is a technique of wrapping the data members and member functions in a single
unit. It binds the data within a class, and no outside method can access the
data. If the data member is private, then the member function can only access
the data.
- Abstraction:
Abstraction is
a technique of showing only essential details without representing the
implementation details. If the members are defined with a public keyword, then
the members are accessible outside also. If the members are defined with a
private keyword, then the members are not accessible by the outside methods.
- Data binding:
Data binding
is a process of binding the application UI and business logic. Any change made
in the business logic will reflect directly to the application UI.
- Polymorphism:
Polymorphism
means multiple forms. Polymorphism means having more than one function with the
same name but with different functionalities. Polymorphism is of two types:
1. Static polymorphism is also known as early binding.
2. Dynamic polymorphism is also known as late binding.
Q. What are the different types of
polymorphism in C++?
Ans:
Polymorphism:
Polymorphism means multiple forms. It means having more than one function with
the same function name but with different functionalities.
Polymorphism
is of two types:
- Runtime polymorphism
Runtime
polymorphism is also known as dynamic polymorphism. Function overriding is an
example of runtime polymorphism. Function overriding means when the child class
contains the method which is already present in the parent class. Hence, the
child class overrides the method of the parent class. In case of function
overriding, parent and child class both contains the same function with the
different definition. The call to the function is determined at runtime is
known as runtime polymorphism.
- Compile time polymorphism
Compile-time
polymorphism is also known as static polymorphism. The polymorphism which is
implemented at the compile time is known as compile-time polymorphism. Method
overloading is an example of compile-time polymorphism.
Method
overloading: Method overloading is a technique which allows you
to have more than one function with the same function name but with different
functionality.
Method
overloading can be possible on the following basis:
- The return type of the
overloaded function.
- The type of the parameters
passed to the function.
- The number of parameters passed
to the function.
Q. Define
namespace in C++.
Ans:
The namespace
is a logical division of the code which is designed to stop the naming
conflict.
Ø
The namespace
defines the scope where the identifiers such as variables, class, functions are
declared.
Ø
The main
purpose of using namespace in C++ is to remove the ambiguity. Ambiquity occurs
when the different task occurs with the same name.
Ø
For example:
if there are two functions exist with the same name such as add(). In order to
prevent this ambiguity, the namespace is used. Functions are declared in
different namespaces.
Ø
C++ consists
of a standard namespace, i.e., std which contains inbuilt classes and
functions. So, by using the statement "using namespace std;" includes
the namespace "std" in our program.
Q. Define
token in C++.
Ans:
A token in C++
can be a keyword, identifier, literal, constant and symbol.
Q. Who was the creator of C++?
Ans:
Bjarne
Stroustrup.
Q. Which operations are permitted on pointers?
Ans:
Following are
the operations that can be performed on pointers:
- Incrementing or decrementing a
pointer:
Incrementing a pointer means that we can increment the pointer by the size
of a data type to which it points.
There are two
types of increment pointers:
1.
Pre-increment pointer: The pre-increment operator increments the operand by 1,
and the value of the expression becomes the resulting value of the incremented.
Suppose ptr is a pointer then pre-increment pointer is represented as ++ptr.
2.
Post-increment pointer: The post-increment operator increments the
operand by 1, but the value of the expression will be the value of the operand
prior to the incremented value of the operand. Suppose ptr is a pointer then
post-increment pointer is represented as ptr++.
- Subtracting a pointer from
another pointer: When
two pointers pointing to the members of an array are subtracted, then the
number of elements present between the two members are returned.
Q. Define 'std'.
Ans:
Std is the default
namespace standard used in C++.
Q. Which programming language's unsatisfactory
performance led to the discovery of C++?
Ans:
C++was
discovered in order to cope with the disadvantages of C.
Q. How delete [] is different from delete?
Ans:
Delete is used
to release a unit of memory, delete[] is used to release an array.
Q. What is the full form of STL in C++?
Ans:
STL stands for
Standard Template Library.
Q. What is an object?
Ans:
The Object is
the instance of a class. A class provides a blueprint for objects. So you can
create an object from a class. The objects of a class are declared with the
same sort of declaration that we declare variables of basic types.
Q. What are the C++ access specifiers?
Ans:
The access
specifiers are used to define how to functions and variables can be accessed
outside the class.
There are
three types of access specifiers:
- Private: Functions and variables
declared as private can be accessed only within the same class, and they
cannot be accessed outside the class they are declared.
- Public: Functions and variables
declared under public can be accessed from anywhere.
- Protected: Functions and variables
declared as protected cannot be accessed outside the class except a child
class. This specifier is generally used in inheritance.
Q. When should we use multiple inheritance?
Ans:
You can answer
this question in three manners:
1. Never
2. Rarely
3. If you find that the problem domain cannot be accurately
modeled any other way.
Q. What is a destructor?
Ans:
A Destructor
is used to delete any extra resources allocated by the object. A destructor
function is called automatically once the object goes out of the scope.
Rules of
destructor:
- Destructors have the same name
as class name and it is preceded by tilde.
- It does not contain any
argument and no return type.
Q. What is an overflow error?
Ans:
It is a type
of arithmetical error. It happens when the result of an arithmetical operation
been greater than the actual space provided by the system.
Q. What is overloading?
Ans:
Ø
When a single
object behaves in many ways is known as overloading. A single object has the
same name, but it provides different versions of the same function.
Ø
C++
facilitates you to specify more than one definition for a function name or an
operator in the same scope. It is called function overloading and operator
overloading respectively.
- Overloading is of two types:
1. Operator
overloading: Operator overloading is a compile-time polymorphism
in which a standard operator is overloaded to provide a user-defined definition
to it. For example, '+' operator is overloaded to perform the addition
operation on data types such as int, float, etc.
Operator
overloading can be implemented in the following functions:
- Member function
- Non-member function
- Friend function
Syntax of
Operator overloading:
1. Return_type classname :: Operator Operator_symbol(argument_list)
2. {
3. // body_statements;
4. }
2. Function
overloading: Function overloading is also a type of compile-time
polymorphism which can define a family of functions with the same name. The
function would perform different operations based on the argument list in the
function call. The function to be invoked depends on the number of arguments
and the type of the arguments in the argument list.
Q. What is function overriding?
Ans:
If you inherit
a class into a derived class and provide a definition for one of the base
class's function again inside the derived class, then this function is called
overridden function, and this mechanism is known as function overriding.
Q. What is virtual inheritance?
Ans:
Virtual
inheritance facilitates you to create only one copy of each object even if the
object appears more than one in the hierarchy.
Q. What is a constructor?
Ans:
A Constructor
is a special method that initializes an object. Its name must be same as class
name.
Q. What is the purpose of the
"delete" operator?
Ans:
The
"delete" operator is used to release the dynamic memory created by
"new" operator.
Q. Explain this pointer?
Ans:
This pointer
holds the address of the current object.
Q. What does Scope Resolution operator do?
Ans:
A scope
resolution operator(::) is used to define the member function outside the
class.
Q. What is the difference between delete and
delete[]?
Ans:
Delete [] is
used to release the array of allocated memory which was allocated using new[]
whereas delete is used to release one chunk of memory which was allocated using
new.
Q. What is a pure virtual function?
Ans:
The pure
virtual function is a virtual function which does not contain any definition.
The normal function is preceded with a keyword virtual. The pure virtual
function ends with 0.
Q. What is the
difference between struct and class?
Structures |
class |
A structure is a user-defined data type which contains
variables of dissimilar data types. |
The class is a user-defined data type which contains
member variables and member functions. |
The variables of a structure are stored in the stack
memory. |
The variables of a class are stored in the heap memory. |
We cannot initialize the variables directly. |
We can initialize the member variables directly. |
If access specifier is not specified, then by default
the access specifier of the variable is "public". |
If access specifier is not specified, then by default
the access specifier of a variable is "private". |
The instance of a structure is a "structure
variable". |
|
Declaration of a structure: struct
structure_name { // body of structure; }
; |
Declaration of class: class
class_name { // body of class; }
|
A structure is declared by using a struct keyword. |
The class is declared by using a class keyword. |
The structure does not support the inheritance. |
The class supports the concept of inheritance. |
The type of a structure is a value type. |
The type of a class is a reference type. |
Q. What is a class template?
Ans:
A class
template is used to create a family of classes and functions. For example, we
can create a template of an array class which will enable us to create an array
of various types such as int, float, char, etc. Similarly, we can create a
template for a function, suppose we have a function add(), then we can create
multiple versions of add().
The syntax of
a class template:
1. template<class T>
2. class classname
3. {
4. // body of class;
5. };
Syntax of a
object of a template class:
1. classname<type> objectname(arglist);
Q. What is the difference between function
overloading and operator overloading?
Ans:
Function
overloading: Function overloading is defined as we can have more
than one version of the same function. The versions of a function will have
different signature means that they have a different set of parameters.
Operator
overloading: Operator overloading is defined as the standard
operator can be redefined so that it has a different meaning when applied to
the instances of a class.
Q. What is a virtual destructor?
Ans:
A virtual
destructor in C++ is used in the base class so that the derived class object
can also be destroyed. A virtual destructor is declared by using the ~ tilde
operator and then virtual keyword before the constructor.
Q. What is
operator overloading?
Ans:
Operator
Overloading is a very essential element to perform the operations on
user-defined data types. By operator overloading we can modify the default
meaning to the operators like +, -, *, /, <=, etc.
Q. What is
polymorphism in C++?
Ans:
Polymorphism
in simple means having many forms. Its behavior is different in different
situations. And this occurs when we have multiple classes that are related to
each other by inheritance.
Q. Tell me
about virtual function
Ans:
Virtual function is a
member function in the base class that you redefine in a derived class. A
virtual function is declared using the virtual keyword. When the function is
made virtual, C++ determines which function is to be invoked at the runtime
based on the type of the object pointed by the base class pointer.
Q. What do
you know about friend class and friend function?
Ans:
A friend
class can access private, protected, and public members of other classes in
which it is declared as friends.
Like friend
class, friend function can also access private, protected, and public members.
But, Friend functions are not member functions.
Q. What are
the C++ access specifiers?
Ans:
In C++ there
are the following access specifiers:
Public: All
data members and member functions are accessible outside the class.
Protected: All
data members and member functions are accessible inside the class and to the
derived class.
Private: All
data members and member functions are not accessible outside the class.
Q. Define
inline function
Ans:
If a function
is inline, the compiler places a copy of the code of that function at each
point where the function is called at compile time. One of the important
advantages of using an inline function is that it eliminates the function
calling overhead of a traditional function.
Q. What is a
reference in C++?
Ans:
A reference
is like a pointer. It is another name of an already existing variable. Once a
reference name is initialized with a variable, that variable can be accessed by
the variable name or reference name both.
Q. What do
you mean by abstraction in C++?
Ans:
Abstraction
is the process of showing the essential details to the user and hiding the
details which we don’t want to show to the user or hiding the details which are
irrelevant to a particular user.
Q. Is
deconstructor overloading possible? If yes then explain and if no then why?
Ans:
No destructor
overloading is not possible. Destructors take no arguments, so there’s only one
way to destroy an object. That’s the reason destructor overloading is not
possible.
Q. What do
you mean by call by value and call by reference?
Ans:
In call by
value method, we pass a copy of the parameter is passed to the functions. For
these copied values a new memory is assigned and changes made to these values
do not reflect the variable in the main function.
In call by
reference method, we pass the address of the variable and the address is used
to access the actual argument used in the function call. So changes made in the
parameter alter the passing argument.
Q. What is an
abstract class and when do you use it?
Ans:
A class is
called an abstract class whose objects can never be created. Such a class
exists as a parent for the derived classes. We can make a class abstract by
placing a pure virtual function in the class.
Q. What are
destructors in C++?
Ans:
A constructor
is automatically called when an object is first created. Similarly when an
object is destroyed a function called destructor automatically gets called. A
destructor has the same name as the constructor (which is the same as the class
name) but is preceded by a tilde.
Q. What are
the static members and static member functions?
Ans:
When a
variable in a class is declared static, space for it is allocated for the
lifetime of the program. No matter how many objects of that class have been
created, there is only one copy of the static member. So same static member can
be accessed by all the objects of that class.
A static
member function can be called even if no objects of the class exist and the
static function are accessed using only the class name and the scope resolution
operator ::
Q. Explain
inheritance
Ans:
Inheritance
is the process of creating new classes, called derived classes, from existing
classes. These existing classes are called base classes. The derived classes
inherit all the capabilities of the base class but can add new features and
refinements of their own.
Q. What is a
copy constructor?
Ans:
A copy
constructor is a member function that initializes an object using another object
of the same class.
Q. What is
the difference between virtual functions and pure virtual functions?
Ans:
A virtual
function is a member function in the base class that you redefine in a derived
class. It is declared using the virtual keyword.
Q. Can we
call a virtual function from a constructor?
Ans:
Yes, we can
call a virtual function from a constructor. But the behavior is a little
different in this case. When a virtual function is called, the virtual call is
resolved at runtime. It is always the member function of the current class that
gets called. That is the virtual machine doesn’t work within the constructor.
Q.
Explain what is the use of void main () in C++ language?
Ans:
To
run the C++ application it involves two steps, the first step is a compilation
where conversion of C++ code to object code take place. While second step
includes linking, where combining of object code from the programmer and from
libraries takes place. This function is operated by main () in C++ language.
Q.
Explain what is Member Functions in Classes?
Ans:
The
member function regulates the behaviour of the class. It provides a definition
for supporting various operations on data held in the form of an object.
Q.
Define basic type of variable used for a different condition in C++?
Ans:
The
variable used for a different condition in C++ are
- Bool:
Variable to store boolean values (true or false)
- Char:
Variable to store character types
- int :
Variable with integral values
- float and
double: Types of variables with large and floating point values
Q.
What is namespace std; and what is consists of?
Ans:
Namespace
std; defines your standard C++ library, it consists of classes, objects and
functions of the standard C++ library. You can specify the library by using
namespace std or std: : throughout the code. Namespace is used to differentiate
the same functions in a library by defining the name.
Q.
Explain what is Loop function? What are different types of Loops?
Ans:
In
any programming language, to execute a set of statements repeatedly until a
particular condition is satisfied Loop function is used. The loop statement is
kept under the curly braces { } referred as Loop body.
In
C++ language, three types of loops are used
- While loop
- For loop
- Do-while
loop
Q.
Explain how functions are classified in C++ ?
Ans:
In
C++ functions are classified as
- Return
type
- Function
Name
- Parameters
- Function
body
Q.
Explain what is a reference variable in C++?
Ans:
A
reference variable is just like a pointer with few differences. It is declared
using & Operator. In other words, reference is another name for an already
existing variable.
Q.
Explain what is Polymorphism in C++?
Ans:
Polymorphism
in C++ is the ability to call different functions by using only one type of the
function call. Polymorphism is referred to codes, operations or objects that
behave differently in a different context.
For
example, the addition function can be used in many contests like
- 5+5
Integer addition
- Medical+Internship
The same ( + ) operator can be used with different meaning with strings
- 3.14 +
2.27 The same ( + ) operator can be used for floating point addition
16)
Explain what is data abstraction in C++?
Ans:
Data
abstraction is a technique to provide essential information to the outside
world while hiding the background details. Here in below example you don’t have
to understand how cout display the text “Hello guru99” on the user screen and
at the same time implementation of cout is free to change.
Q. What is the
difference between an array and a list?
Ans:
Ø
An Array is a
collection of homogeneous elements while a list is a collection of
heterogeneous elements.
Ø
Array memory
allocation is static and continuous while List memory allocation is dynamic and
random.
Ø
In Array,
users don't need to keep in track of next memory allocation while In the list,
the user has to keep in track of next location where memory is allocated.
Q. What is the difference between new() and
malloc()?
Ans:
Ø
new() is a
preprocessor while malloc() is a function.
Ø
There is no
need to allocate the memory while using "new" but in malloc() you
have to use sizeof().
Ø
"new"
initializes the new memory to 0 while malloc() gives random value in the newly
allotted memory location.
Ø
The new()
operator allocates the memory and calls the constructor for the object
initialization and malloc() function allocates the memory but does not call the
constructor for the object initialization.
Ø
The new()
operator is faster than the malloc() function as operator is faster than the
function.
Q. What are the methods of exporting a
function from a DLL?
Ans:
There are two
ways:
- By using the DLL's type
library.
- Taking a reference to the
function from the DLL instance.
Q. Define friend function.
Ans:
Friend
function acts as a friend of the class. It can access the private and protected
members of the class. The friend function is not a member of the class, but it
must be listed in the class definition. The non-member function cannot access
the private data of the class. Sometimes, it is necessary for the non-member
function to access the data. The friend function is a non-member function and
has the ability to access the private data of the class.
Following are
the characteristics of a friend function:
Ø
The friend
function is not in the scope of the class in which it has been declared.
Ø
Since it is
not in the scope of the class, so it cannot be called by using the object of
the class. Therefore, friend function can be invoked like a normal function.
Ø
A friend
function cannot access the private members directly, it has to use an object
name and dot operator with each member name.
Ø
Friend
function uses objects as arguments.
Q. What is a
virtual function?
Ans:
Ø
A virtual function
is used to replace the implementation provided by the base class. The
replacement is always called whenever the object in question is actually of the
derived class, even if the object is accessed by a base pointer rather than a
derived pointer.
Ø
A virtual
function is a member function which is present in the base class and redefined
by the derived class.
Ø
When we use
the same function name in both base and derived class, the function in base
class is declared with a keyword virtual.
Ø
When the
function is made virtual, then C++ determines at run-time which function is to
be called based on the type of the object pointed by the base class pointer.
Thus, by making the base class pointer to point different objects, we can
execute different versions of the virtual functions.
Rules of a
virtual function:
Ø
The virtual
functions should be a member of some class.
Ø
The virtual
function cannot be a static member.
Ø
Virtual
functions are called by using the object pointer.
Ø
It can be a
friend of another class.
Ø
C++ does not
contain virtual constructors but can have a virtual destructor.
Q.
Explain what is C++ exceptional handling?
Ans:
The
problem that arises during execution of a program is referred as exceptional
handling. The exceptional handling in C++ is done by three keywords.
- Try: It
identifies a block of code for which particular exceptions will be
activated
- Catch: The
catch keyword indicates the catching of an exception by an exception
handler at the place in a program
- Throw:
When a problem exists while running the code, the program throws an
exception
Q.
Explain what is data encapsulation in C++?
Ans:
Encapsulation
is an object oriented programming concept (oops) which binds together the data
and functions. It is also referred as data hiding mechanism.
Q. Mention what are the types of
Member Functions?
Ans:
The
types of member functions are
- Simple
functions
- Static
functions
- Const
functions
- Inline
functions
- Friend
functions
Q.
Explain what is multi-threading in C++?
Ans:
To
run two or more programs simultaneously multi-threading is useful. There are
two types of
- Process-based:
It handles the concurrent execution of the program
- Thread-based:
It deals with the concurrent execution of pieces of the same program
Q.
Explain what is upcasting in C++?
Ans:
Upcasting
is the act of converting a sub class references or pointer into its super class
reference or pointer is called upcasting.
Q.
Explain what is pre-processor in C++?
Ans:
Pre-processors
are the directives, which give instruction to the compiler to pre-process the
information before actual compilation starts.
Q.
Explain what is COPY CONSTRUCTOR and what is it used for?
Ans:
COPY
CONSTRUCTOR is a technique that accepts an object of the same class and copies
its data member to an object on the left part of the assignment.
1 Comments
good questions and answers
ReplyDelete