You are on page 1of 30

1. What is Object Oriented Programming Approach?

Object Oriented Programming is an approach that provides a way of


modularizing programs by creating partitioned memory area for data as well as
functions that can be used as templates for creating copies of such modules on
demand. OOP allows decomposition of a problem into a number of entities called
Objects and then builds data and functions around these objects.

2. What is a Class?
An entire set of data and code of an object can be made user-defined data type
using a class. Objects are variables of Class type. Once a Class has been defined, we
can create numerous Objects of its type. A Class is a collection of Objects of similar
type.
3. What is Hierarchical Inheritance?
Hierarchical Inheritance occurs when traits of one Class is inherited by more than one
classes.
4. What are Iterators in C++ STL?
Iterators behave similar to Pointers and are used to access container elements. They
are used to traverse from one element to another. This process is known as ‘iterating
through the container’.
5. What is Polymorphism?

It is the ability to showcase or exhibit different behaviors under different instances. The
process of making and Operator or a Function behave differently in different instances
is known as Operator Overloading.
6. What is iostream?
iostream is a header file in C++. It includes function prototypes for Standard Input
Output Functions which include definitions for cout and cin too. The header file is
declared as #include<iostream>

5) Explain what are the characteristics of Class Members in C++?


• Data and Functions are members in C++,
• Within the class definition, data members and methods must be declared
• Within a class, a member cannot be re-declare
• Other that in the class definition, no member can be added elsewhere

8) What is namespace std; and what is consists of?


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 u
6) Explain what is Member Functions in Classes?
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
sed to differentiate the same functions in a library by defining the name

1) What is C++?
C++ is an object oriented programming language created by Bjarne
Stroustrup. It is released in 1985.

2) What are the advantages of C++?


C++ doesn't only maintains all aspects from C language, it also simplify
memory management and add several features like:

o Includes a new datatype known as a class.

o Allows object oriented programming.

3) What is the difference between C and C++?


4) What is the difference between reference and
pointer?
5) What is a class?
Class is a user-defined data type. Class defines the type definition of
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.


7) What are the C++ access specifiers?
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:

o 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.

o Public: Functions and variables declared under public can be accessed from
anywhere.

o Protected: Functions and variables declared as protected cannot be accessed


outside the class except a child class. This specifier is generally used in inheritance.

6) What is an object?
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.

7) What are the C++ access specifiers?


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:

o 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.

o Public: Functions and variables declared under public can be accessed from
anywhere.

o Protected: Functions and variables declared as protected cannot be accessed


outside the class except a child class. This specifier is generally used in inheritance.
8) What is Object Oriented Programming (OOP)?
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
datatype. 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 hide the data from 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.

9) What is the difference between array and a list?


o Array is a collection of homogeneous elements while list is a collection of
heterogeneous elements.

o Array memory allocation is static and continuous while List memory allocation is
dynamic and random.

o In Array, users don't need to keep in track of next memory allocation while In list
user has to keep in track of next location where memory is allocated.

10) What is the difference between new() and malloc()?


o new() is a preprocessor while malloc() is a function.

o There is no need to allocate the memory while using "new" but in malloc() you have
to use sizeof().

o "new" initializes the new memory to 0 while malloc() gives random value in the
newly allotted memory location.
11) What are the methods of exporting a function from a DLL?
There are two ways:

o By using the DLL's type library.

o Taking a reference to the function from the DLL instance.

12) Define friend function.


Friend function acts as 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.

13) What is virtual function?


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.

14) When should we use multiple inheritance?


You can answer this question in three manners:

 Never

 Rarely

 If you find that the problem domain cannot be accurately modeled any other way.

15) What is the destructor?


Destructor is used to delete any extra resources allocated by the object.
16) What is an overflow error?
It is a type of arithmetical error. It is happen when the result of an arithmetical operation been
greater than the actual space provided by the system.

17) What is overloading?


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.

18) What is function overriding?


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.

19) What is virtual inheritance?


Virtual inheritance facilitates you to create only one copy of each object even if the object
appears more than one in the hierarchy.

20) What is constructor?


Constructor is a special method that initializes object. It name must be same as class name.

21) What is the purpose of "delete" operator?


The "delete" operator is used to release the dynamic memory created by "new" operator.

22) Explain this pointer?


This pointer holds the address of current object.
23) What does Scope Resolution operator?
A scope resolution operator(::) is used to define the member function outside the class.

24) What is the difference between delete and delete[]?


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.

25) Define the private, protected and public in C++?


Private: The data members and functions cannot be accessed from outside the class.

Protected: The data members and functions are accessible to derived class only.

Public: The data members and functions can be accessed from outside the class.

7. What is Dynamic Binding?


Binding is the linking of a procedure call to the code to be executed in response to the
call. Dynamic Binding is also know as Late Binding which means that the code
associated with a given procedure call is unknown until the time of call at run-time.
8. What are the applications of Object Oriented Programming?
The areas of applications for OOP includes:
1. Artificial Intelligence Systems
2. Simulation and Modelling
3. Real-Time Systems
4. Neural networks and Parallel Programming
5. Object Oriented Databases
9. What is a Destructor?
A Destructor is used to destroy the objects that have been created by Constructor. It is
basically used to clean up storage that is no longer accessible. A Destructor is a
member function. Its name is the same as the class name but is preceded by a Tilde.
10. What is a Namespace?
It is mandatory that all the C++ programs must include Namespaces. Namespaces in
C++ defines a scope for the identifiers that are used in the program.
11. What is Enumerated Data Type?
An Enumerated Data Type is another User-Defined Type which provides a way for
attaching numbers, thereby increasing comprehensibility of the program code. The
enum keyword automatically enumerates a list of words by assigning them values
0,1,2,3, and so on.
12. What are Lists in C++ STL?
A List is another very important Container in C++. It supports bidirectional, linear list
and provides and efficient implementation for deletion and insertion operatons. A List
can only be sequentially accessed.
13. Enlist Operators in C++.
new Memory allocation operator
.* Pointer to Member Operator
endl Line feed operator
:: Scope Resolution Operator
::* Pointer to Member Declarator
delete Memory Release Operator
->* Pointer to Member Operator
setw Field width operator
14. Explain catch block in C++ Exception Handling.
A catch block is defined by keyword catch that catches Exceptions thrown in by throw
block and then handles it accordingly. The catch block that intercepts an exception
immediately follow the try block that throws the exception. There can be multiple catch
statements in a single program.
15. What is Function Prototyping?
A Function Prototype depicts the function interface to the compiler by providing the
details regarding Number and Type of arguments and the type of return values. With
function or method prototyping, a template is always used when declaring and defining
a function.
16. What is the ifstream() method?
ifstream() method provides input operations. It contains open() method with default
input mode. It also inherits functions such as get(), getline(), read(), seekg() and
tellg() functions from istream.
17. What is Multilevel Inheritance?
The mechanism of deriving a Class from a previously Derived Class is known as Multiple
Inheritance.
18. What is an Object?
Objects are basic Run-Time entities in an Object oriented system. They may represent
a place, a bank account or a person. Objects are essentially the variables that are of
Class types.
19. What is an Inline Function?
To eliminate the cost of calls and few other overheads to small functions, C++ provides
a new feature called Inline Function. An inline function is a function that is expanded in
line when it is invoked. This helps to save memory space. This is some what similar to
Macro Expansion.
20. What are Visibility Labels in C++?
The keywords Public, Protected and Private are known as Visibility Labels in C++. By
default, the members of any Class are private. A Class which has a Visibility label
private is completely hidden from the external environment and does not serve any
purpose. A Class with Public Label is visible to the other functions and classes.
21. What are Derived Containers in C++ STL?
The Characteristics of a Static Data Member Variable are:
1. It is initialized to Zero when the first object of its Class are created.
2. It is visible only within that class, but its lifetime is the entire program.
3. Only one copy of that member is created for the entire class and is shared by all the
objects of that class irrespective of the number of objects are created.
22. Enlist the characteristics of Friend Functions.
1. It is not in the scope of the class to which it has been declared as a Friend.
2. Since it’s not in the scope of the class, it cannot be called using Object of that Class.
3. It can be invoked like a normal function without the help of any object.
4. Usually , it has the Objects as Arguments.
5. It can be declared either in public or in the private part of a Class without affecting
its meaning.
23. What is a Constructor?
A Constructor is a special member function whose primary task is to initialize objects of
its class. The constructor is invoked whenever an object of its class with which it is
associated to is created. It is named as constructor because it constructs the values of
class data members. It is special because its name is the same as its class name.
24. What is Hybrid Inheritance?
A Hybrid Class is the one that is derived from numerous classes; it can be derived from
a parent as well as a child class. There is no sequence as such. It all depends on the
needs of such a class.
25. What are cout and cin?
cout is the object of ostream class. The cout stream is by default linked to console
output device.It is basically used to output the characters on the console screen. It
similar to printf in C.
cin is the object of istream class. The cin stream is by default linked to console input
device. It is basically used to extract the characters form the User. It similar to scanf in
C.
26. What is a Virtual Function?
When a method is declared as Virtual method, the C++ compiler determines which
function should be used as runtime based on the type of object pointed to by the base
pointer, rather than the type of pointer. When we use the same method name in
derived as well as base class, the method in base class is declared a virtual using the
keyword virtual preceding its normal declaration.
27. What is Data Abstraction?
Data Abstraction is basically representing important features without taking care of the
background details and information. Since the Classes use concept of Data Abstraction,
they are known as Abstract Data Types.
STL povides three derived containers named as Queue, Stack and Priority_Queue.
These are also known as Container Adapters. These can be developed from different
Sequence containers. All of them offer two main functions viz., push() and pop().
28. What are static Data Members in C++?
The Characteristics of a Static Data Member Variable are:
1. It is initialized to Zero when the very first object of its Class are created.
2. It is visible only within that class, but its lifetime is the entire program.
3. Only single copy of that member is created for the whole class and is shared by all
the objects of that class irrespective of the number of objects created.
29. Enlist the characteristics of Friend Functions.
1. Usually, it has the Objects as Arguments.
2. As it’s not in the scope of the class, it cannot be called using Object of that Class.
3. It is not in the scope of the class to which it has been declared as a Friend.
4. It can be declared either in public or in the private part of a Class without affecting
its meaning.
5. It can be invoked like a normal function without the help of any object.
30. What is a Constructor?
A Constructor is a special member function or a method whose task is to initialize
objects of its class. It’s name is the same as its class name. The constructor is invoked
whenever an object of its associated class is created. It is named as constructor
because it constructs the values of data members of the class.
31. What is Hybrid Inheritance?
A Hybrid Class is the one that is derived from numerous classes; it can be derived from
a parent as well as a child class. There is no sequence as such. It all depends on the
needs of such a class.
32. What is STL?
STL stands for Standard Template Library. It is a software library for C++ programming
language that influenced many parts of C++ Standard Library. Using STL can save
considerable time and effort leading to high quality programs. STL components are
defined in namespace std.
33. What is this Pointer?
C++ makes use of a unique keyword called this to represent an object that invokes a
member function. It is a pointer that points to the object for which this function was
called. For example, the function B.min() will set the pointer this to the address of the
object B.
34. What is eof() method used for?
eof() method returns a Non Zero Value if end-of-file is encountered while reading, else
it returns a Zero.
35. What is ofstream() method?
This method provides output operations. It contains open() method with default output
mode. It also inherits functions such as put(), seekp(), tellp(), and write() functions
from istream.
36. What is a Vector in STL?
A Vector is one of the most widely used containers in C++. It stores the elements in
contiguous memory locations and provides direct access to every element using the
subscript operator. A Vector can modify its size dynamically and hence allocates the
memory as needed at run time.
37. How do we access Virtual Functions?
We must access virtual functions through the use of pointer declared as a pointer to the
base class. Run Time Polymorphism is achieved only when a virtual method is accessed
through a pointer to the base address.
38. What is Encapsulation?
Wrapping up data and functions into a single unit or a block called as a Class is know as
Encapsulation. This insulation of data from direct access by the program is called Data
Hiding. The data is therefore inaccessible to the external world; only the functions
wrapped inside class can access it.
39. What is a Stream Class?
The C++ Input/Output consists of a hierarchy of classes that are used to define various
streams to deal with the console files as well as disk files. These classes are called a
Stream Classes and are declared in the header file iostream.
40. What is Multiple Inheritance?
A Derived Class with multiple Base Classes is called Multiple Inheritance.
41. What are Maps?
A map is a sequence of key, value pairs where a single value is associated with each
unique key. Retrieval of values is based on the key and is quick. We need to specify the
key to obtain the associated values .
42. What are command line arguments?
The arguments/parameters which are sent to the main() function while executing the
program from the command prompt or command line. All the arguments sent are in the
form of strings only.
43. What is a Friend Class?
A class members can gain accessibility over other class member by placing the class
declaration prefixed with the keyword ‘friend’ in the destination class.
44. How do you Overload Template Functions?
A Template Function may be overloaded either by template methods or ordinary
methods of its name, It may be accomplished as follows:
1. Call an ordinary method that matches exactly.
2. Call a template method that could be created with an exact match.
3. Try normal overloading resolution to ordinary methods and call the one that
matches.
45. Enlist STL Algorithms.
1. Retrieve or Non-Mutating Algorithms
2. Mutating Algorithms
3. Sorting Algorithms
4. Set Algorithms
5. Relational Algorithms
46. Explain try and throw block?
The keyword try is used to preface a block of statements that may generate exceptions.
This block of statements is known as Try block. When an exception occurs, it is thrown
into the catch block using the throw statement.
47. What is a Virtual Base Class?
A Base Class class that has been qualified as Virtual in the
inheritance definition. In multiple inheritance, a derived class can
inherit the members of a base class via two or more inheritance
paths. For a virtual base class, only a single copy of its members
will be inherited regardless of the number of inheritance paths
between the base class and the derived class.
48. Enlist the Components of Standard Template Library.
STL contains several components but the core components are:
1. Iterators
2. Algorithms
3. Containers
4. Functions
49. What is Operator Overloading?
Operator Overloading basically means to make an Operator behave differently in
different instances. Asterisk * is an example of Operator Overloading as it acts as a
Pointer as well as a Multiplication operator automatically. Operator overloading is a part
of Polymorphism.
50. What is Inheritance?
It is the process by which Objects of one class acquire the properties of objects of
another class. It also supports Hierarchical classification. Inheritance basically provides
the concept of Reusability.
51. What is RTTI?
RTTI represents Runtime Type Information . It determines the type of any variable
during execution i.e., during runtime. The RTTI Mechanism contains:
1. Operator Dynamic_Cast
2. Operator Typeid
3. Struct Type_Info
52. Enlist Iterators in C++ STL.
The Iterators in C++ are as follows:
1. Input
2. Forward
3. Random
4. Output
5. Bidirectional
53. What is the role of a Mutable Storage Class Specifier?
A constant class object’s member variable can be modified by declaring it using a
Specifier of a mutable storage class. It is applicable only for non-static and non-
constant member variable of the class.
54. Does C++ Programming Language support Multiple and Multilevel
Inheritance?
Yes, it does support multiple inheritance and multilevel inheritance.
So this was the list of some important C++ interview questions and answers. If you
found any information incorrect or missing in above list then please mention it by
commenting below.

C++ Interview Questions with Answers:

1. What is C++?

C++ is created by Bjarne Stroustrup of AT&T Bell Labs as an extension of C, C++ is an object-
oriented computer language used in the development of enterprise and commercial applications.
Microsoft’s Visual C++ became the premier language of choice among developers and
programmers.

2. Define inheritance?

The mechanism of deriving a new class (derived) from an old class (base class) is called inheritance.
It allows the extension and reuse of existing code without having to rewrite the code from scratch.
Inheritance is the process by which objects of one class acquire properties of objects of another
class.

3. Define Constructors?

A constructor is a member function with the same name as its class. The constructor is invoked
whenever an object of its associated class is created. It is called constructor because it constructs the
values of data members of the class.

4. What is the difference between C & C++?

C++ is an object oriented programing but c is a procedure oriented programing. C is super set of
C++. C can’t support inheritance, function overloading, method overloading etc. but C++ can do
this. In c-program the main function could not return a value but in the C++ the main function
should return a value.

5. What do you mean by implicit conversion?

Whenever data types are mixed in an expression then C++ performs the conversion automatically.

Here smaller type is converted to wider type.

Example : in case of integer and float integer is converted into float type.

6. What is the difference between class and structure?

By default, the members of structures are public while that tor class is private.

Structures doesn’t provide something like data hiding which is provided by the classes.

Structures contains only data while class bind both data and member functions.

7. What is dynamic binding?

Dynamic binding (also known as late binding) means that the code associated with a given
procedure call is not known until the time of the call at run time. It is associated with
polymorphism and inheritance.

8. What is difference between function overloading and operator overloading?

A function is overloaded when same name is given to different function.

While overloading a function, the return type of the functions need to be the same.

9. What is friend function?

The function declaration should be preceded by the keyword friend. The function definitions does
not use either the keyword or the scope operator ::. The functions that are declared with the
keyword friend as friend function. Thus, a friend function is an ordinary function or a member of
another class.

10. What is an iterator?

Iterators are like pointers. They are used to access the elements of containers thus providing a link
between algorithms and containers. Iterators are defined for specific containers and used as
arguments to algorithms.

11. What are the differences between new and malloc?

New initializes the allocated memory by calling the constructor. Memory allocated with new should
be released with delete.
Malloc allocates uninitialized memory.

The allocated memory has to be released with free. New automatically calls the constructor while
malloc (doesn’t).

12. What is an explicit constructor?

A conversion constructor declared with the explicit keyword. The compiler does not use an explicit
constructor to implement an implied conversion of types. It’s purpose is reserved explicitly for
construction. Explicit constructors are simply constructors that cannot take part in an implicit
conversion.

13. What do you mean by inline function?

An inline function is a function that is expanded inline when invoked.ie. the compiler replaces the
function call with the corresponding function code. An inline function is a function that is expanded
in line when it is invoked. That is the compiler replaces the function call with the corresponding
function code (similar to macro).

14. What is the difference between a copy constructor and an overloaded assignment operator?

A copy constructor constructs a new object by using the content of the argument object. An
overloaded assignment operator assigns the contents of an existing object to another existing object
of the same class.

15. What is class invariant?

A class invariant is a condition that defines all valid states for an object. It is a logical condition to
ensure the correct working of a class. Class invariants must hold when an object is created, and
they must be preserved under all operations of the class. In particular all class invariants are both
preconditions and post-conditions for all operations or member functions of the class

SOME MORE VIVA QUESTIONS


1.What are classes?
A class is a collection of objects of similar type. Once a class is defined any number of objects can be
created of that class.
Examples of classes are class of cars, class of pens, class of birds etc.
What are Instances?
Instances are essentially objects derived out of classes.
When an object is created for a particular class the process is called Instantiation. In general, life time of
object is nothing but the time between an object creation, till the object is no longer used and is
destructed or freed.
2.What is object orientation?
It is a technique for system modeling. It offers a number of concepts

3.Pointer type: A Pointer is a variable which holds the address of another variable.

4.Const Qualifier: Const qualifier is used to prevent from accidental changes within a program.

5.Differences between Structured Programming and OOP


– In St.P emphasis is on What is happening. In OOP emphasis is on Who is being affected.
– Data move openly around the system. In St.P. In OOP the data and functions that operate on the data
are tied together
– In St.P most fns share global data. In OOP data is hidden and cannot be accessed by external functions.
– In St.P, Large programs are divided to smaller programs known as functions, In OOP they are divided
into objects.
.
6.Enumerated Data type: It is a user-defined data type.
• The syntax of enum statement is similar to that of struct
• For Ex: enum shape{ circle, square, triangle};

7. New and Delete : (Memory Management Operators)


These are the unary operators to perform dynamic memory allocation and de-allocation
New operator is used to create objects. The General Form is:
Pointer-variable = new data-type; The general form of using delete
delete pointer-variable;
For Ex: delete p;

To free dynamically allocated array


delete [size] pointer-variable
For Ex: delete [10] p;

For Ex: int *p = new int;

8. Inline Functions:An inline function is a function that is expanded in line when it is invoked

Inline function-header
{
function body
}
Ex: inline int square(int a)
{
return a*a
};

9. Generic Functions
A generic function defines a general set of operations that can be applied to various types of data.. A
generic function is created using the keyword template. The general form of template function
definition is
1. Template ret-type func-name(parameter list)
{ //function body }
10. Constructors and Destructors
1. C++ allows automatic initialization of objects when they are created. This is performed through the
use of a constructor function.
2. Constructor is a special function that is a member of the class and has the same name as that class.
3. The Constructor is automatically called whenever an object of its associated class is created.
4. A Constructor is declared and defined as follows
Destructors:
1. A destructor as the name implies is a complement of the constructor, used to destroy objects.
2. It will be invoked implicitly by the compiler upon exit from program or function or block
3. Local objects are destroyed when the block is left. Global objects are destroyed when the program
terminates.
4. It is a member function whose name is same as the class name but is preceded by a tilde (~) operator.
Ex : ~stack();
5. There are many reasons why a destructor may be needed. Ex: An object may need to deallocate
memory that it had previously allocated, or close a file that it had opened.
6. A destructor never takes any argument nor does it return any value
7. It is a good practice to declare destructors

11. Class is syntactically similar to a struct. The only difference between them is that by default all
members are public in a struct and private in a class.
12. Friend Functions
1. Private members cannot be accessed from outside the class i.e by a non-member function
2. C++ allows a non-member function to access private members of a class by using a friend function.
3. A friend function need not be a member of any class.
4. To make an outside function friendly to a class, include its prototype within the class, preceding it
with the keyword friend.

13. Static Data Members(SDM)


1. Preceding a data member’s declaration with the keyword static tells the compiler that only one copy
of that variable will exist and that all objects of the class will share that variable.
2. A static data member is initialized to zero when the first object of its class is created. No other
initialization is permitted.
3. Declaring a static data member is not defining it (means not allocating storage for it). Remember class
is a logical construct that does not have physical reality.
4. Defining a static data member is done outside the class by redeclaring the static variable using the
scope resolution operator .
5. SDM are normally used to maintain values common to entire class
Static Member Functions(SMF)
There are several restrictions placed on Static member functions.
1. A static function can have access to only other static members declared in the same class.
2. Global functions and data may be accessed by SMF.
3. A SMF does not have a this pointer.
4. There cannot be a static version and non-static version of the same function
5. A SMF may not be virtual.
6. They cannot be declared as const or volatile
7. A static member function can be called using the class name as follows Class-name :: function-name;
8. Use is to preinitialize private static data before any object is created

14. This Pointer


When a member function is called, it is automatically passed an implicit argument that is a pointer to
the invoking object. This pointer is called this

15.Operator Overloading
The mechanism of giving additional meaning to an operator is known as operator overloading.
16.INHERITANCE
1. The mechanism of deriving a new class from an old one is called Inheritance.
2. The concept of Inheritance provides the idea of reusability. This is basically done by creating new
classes, reusing the properties of the existing ones.
3. A class that is inherited is referred to as base class, and a class that inherits is called the derived class.
4. Different types of Inheritance:

17.Protected Members
When a member of a class is declared as protected, that member is not accessible by other nonmember
elements of the program

18.A Virtual function: is a member function that is declared within a base class and redefined by a
derived class.To create a virtual function, precede the function’s declaration in the base class with the
keyword Virtual.

19.. I/O Operations: C++ uses the concept of stream and stream classes to implement the I/O
Operations. A stream acts as an interface between the program and I/O device.

20.What is Polymorphism?
It is generally ability to appear in many forms. In OOP, polymorphism refers to a programming
language’s ability to process objects differently depending on their data type or class.
21.What is Abstraction?
The process of picking out (abstracting) common features of objects and procedures. A programmer
would use abstraction. Abstraction is one of the most important techniques in software engineering and
it is closely related to two other techniques- encapsulation and information hiding. All these three
techniques are used to reduce complexity

C++ VIVA QUESTIONS :

1. What is the difference between interpreters and compilers?

Interpreters read through source code and translate a program, turning the programmer's code, or
program instructions, directly into actions. Compilers translate source code into an executable program
that can be run at a later time.

2. How do you compile the source code with your compiler?

Every compiler is different. Be sure to check the documentation that came with your compiler.

3. What does the linker do?

The linker's job is to tie together your compiled code with the libraries supplied by your compiler vendor
and other sources. The linker lets you build your program in pieces and then link together the pieces
into one big program.

4. What are the steps in the development cycle?

Edit source code, compile, link, test, repeat.


5. What is the difference between the compiler and the preprocessor?

Each time you run your compiler, the preprocessor runs first. It reads through your source code and
includes the files you've asked for, and performs other housekeeping chores. The preprocessor is
discussed in detail on Day 18, "Object-Oriented Analysis and Design."

6. Why is the function main() special?

main() is called automatically, each time your program is executed.

7. What are the two types of comments, and how do they differ?

C++-style comments are two slashes (//), and they comment out any text until the end of the line. C-
style comments come in pairs (/* */), and everything between the matching pairs is commented out.
You must be careful to ensure you have matched pairs.

8. Can comments be nested?

Yes, C++-style comments can be nested within C-style comments. You can, in fact, nest C-style
comments within C++-style comments, as long as you remember that the C++-style comments end at
the end of the line.

9. Can comments be longer than one line?

C-style comments can. If you want to extend C++-style comments to a second line, you must put another
set of double slashes (//).
10. What are the differences between the function prototype and the function defi-nition?

The function prototype declares the function; the definition defines it. The prototype ends with a
semicolon; the definition need not. The declaration can include the keyword inline and default values
for the parameters; the definition cannot. The declaration need not include names for the parameters;
the definition must.

11. Do the names of parameters have to agree in the prototype, definition, and call to the function?

No. All parameters are identified by position, not name.

12. If a function doesn't return a value, how do you declare the function?

Declare the function to return void.

13. If you don't declare a return value, what type of return value is assumed?

Any function that does not explicitly declare a return type returns int.

14. What is a local variable?

A local variable is a variable passed into or declared within a block, typically a function. It is visible only
within the block.

15. What is scope?

Scope refers to the visibility and lifetime of local and global variables. Scope is usually established by a
set of braces.
16. What is recursion?

Recursion generally refers to the ability of a function to call itself.

17. When should you use global variables?

Global variables are typically used when many functions need access to the same data. Global variables
are very rare in C++; once you know how to create static class variables, you will almost never create
global variables.

18. What is function overloading?

Function overloading is the ability to write more than one function with the same name, distinguished
by the number or type of the parameters.

19. What is polymorphism?

Polymorphism is the ability to treat many objects of differing but related types without regard to their
differences. In C++, polymorphism is accomplished by using class derivation and virtual functions.
20. Is the declaration of a class its interface or its implementation?

The declaration of a class is its interface; it tells clients of the class how to interact with the class. The
implementation of the class is the set of member functions stored--usually in a related CPP file.

21. What is the difference between public and private data members?

Public data members can be accessed by clients of the class. Private data members can be accessed only
by member functions of the class.

22. Can member functions be private?

Yes. Both member functions and member data can be private.

23. Can member data be public?

Although member data can be public, it is good programming practice to make it private and to provide
public accessor functions to the data.

24. Do class declarations end with a semicolon? Do class method definitions?

Declarations end with a semicolon after the closing brace; function definitions do not.
25. What is the difference between the indirection operator and the address of oper-ator?

The indirection operator returns the value at the address stored in a pointer. The address of operator
(&) returns the memory address of the variable.

26. What is the difference between a reference and a pointer?

A reference is an alias, and a pointer is a variable that holds an address. References cannot be null and
cannot be assigned to.

27. When must you use a pointer rather than a reference?

When you may need to reassign what is pointed to, or when the pointer may be null.

28. What does new return if there is insufficient memory to make your new object?

A null pointer (0).

29. What is a constant reference?

This is a shorthand way of saying "a reference to a constant object."

30. What is the difference between passing by reference and passing a reference?

Passing by reference means not making a local copy. It can be accomplished by passing a reference or by
passing a pointer.
31. When you overload member functions, in what ways must they differ?

Overloaded member functions are functions in a class that share a name but differ in the number or
type of their parameters.

32. What is the difference between a declaration and a definition?

A definition sets aside memory, but a declaration does not. Almost all declarations are definitions; the
major exceptions are class declarations, function prototypes, and typedef statements.

33. When is the copy constructor called?

Whenever a temporary copy of an object is created. This happens every time an object is passed by
value.

34. When is the destructor called?


The destructor is called each time an object is destroyed, either because it goes out of scope or because
you call delete on a pointer pointing to it.

35. How does the copy constructor differ from the assignment operator (=)?

The assignment operator acts on an existing object; the copy constructor creates a new one.

36. What is the this pointer?

The this pointer is a hidden parameter in every member function that points to the object itself.

37. How do you differentiate between overloading the prefix and postfix increments?

The prefix operator takes no parameters. The postfix operator takes a single int parameter, which is
used as a signal to the compiler that this is the postfix variant.

38. Can you overload the operator+ for short integers?

No, you cannot overload any operator for built-in types.

39. Is it legal in C++ to overload operator++ so that it decrements a value in your class?

It is legal, but it is a bad idea. Operators should be overloaded in a way that is likely to be readily
understood by anyone reading your code.

40. What return value must conversion operators have in their declaration?

None. Like constructors and destructors, they have no return values.

41. What is a v-table?

A v-table, or virtual function table, is a common way for compilers to manage virtual functions in C++.
The table keeps a list of the addresses of all the virtual functions and, depending on the runtime type of
the object pointed to, invokes the right function.

42. What is a virtual destructor?

A destructor of any class can be declared to be virtual. When the pointer is deleted, the runtime type of
the object will be assessed and the correct derived destructor invoked.

43. How do you show the declaration of a virtual constructor?


There are no virtual constructors.

44. How can you create a virtual copy constructor?

By creating a virtual method in your class, which itself calls the copy constructor.

45. How do you invoke a base member function from a derived class in which you've overridden that
function?
Base::FunctionName();

46. How do you invoke a base member function from a derived class in which you have not overridden
that function?
FunctionName();

47. If a base class declares a function to be virtual, and a derived class does not use the term virtual
when overriding that class, is it still virtual when inherited by a third-generation class?

Yes, the virtuality is inherited and cannot be turned off.

48. What is the protected keyword used for?

protected members are accessible to the member functions of derived objects.

49. What is a down cast?

A down cast (also called "casting down") is a declaration that a pointer to a base class is to be treated as
a pointer to a derived class.

50. What is the v-ptr?

The v-ptr, or virtual-function pointer, is an implementation detail of virtual functions. Each object in a
class with virtual functions has a v-ptr, which points to the virtual function table for that class.

51. If a round rectangle has straight edges and rounded corners, your RoundRect class inherits both
from Rectangle and from Circle, and they in turn both inherit from Shape, how many Shapes are created
when you create a RoundRect?

If neither class inherits using the keyword virtual, two Shapes are created: one for Rectangle and one for
Shape. If the keyword virtual is used for both classes, only one shared Shape is created.
52. If Horse and Bird inherit virtual public from Animal, do their constructors initialize the Animal
constructor? If Pegasus inherits from both Horse and Bird, how does it initialize Animal's constructor?

Both Horse and Bird initialize their base class, Animal, in their constructors. Pegasus does as well, and
when a Pegasus is created, the Horse and Bird initializations of Animal are ignored.

53. Declare a class Vehicle and make it an abstract data type.


class Vehicle
{
virtual void Move() = 0;
}

54. If a base class is an ADT, and it has three pure virtual functions, how many of these functions must
be overridden in its derived classes?

None must be overridden unless you want to make the class non-abstract, in which case all three must
be overridden.

55. Can static member variables be private?

Yes. They are member variables, and their access can be controlled like any other. If they are private,
they can be accessed only by using member functions or, more commonly, static member functions.

56. Show the declaration for a static member variable.


static int itsStatic;

57. Show the declaration for a static function pointer.


static int SomeFunction();

58. Show the declaration for a pointer to function returning long and taking an integer parameter.
long (* function)(int);
59. How do you establish an is-a relationship?

With public inheritance.

60. How do you establish a has-a relationship?

With containment; that is, one class has a member that is an object of another type.

61. What is the difference between containment and delegation?


Containment describes the idea of one class having a data member that is an object of another type.
Delegation expresses the idea that one class uses another class to accomplish a task or goal. Delegation
is usually accomplished by containment.

62. What is the difference between delegation and implemented-in-terms-of?

Delegation expresses the idea that one class uses another class to accomplish a task or goal.
Implemented-in-terms-of expresses the idea of inheriting implementation from another class.

63. What is a friend function?

A friend function is a function declared to have access to the protected and private members of your
class.

64. What is a friend class?

A friend class is a class declared so that all its member functions are friend functions of your class.

65. If Dog is a friend of Boy, is Boy a friend of Dog?

No, friendship is not commutative.

66. If Dog is a friend of Boy, and Terrier derives from Dog, is Terrier a friend of Boy?

No, friendship is not inherited.

67. If Dog is a friend of Boy and Boy is a friend of House, is Dog a friend of House?

No, friendship is not associative.

68. Where must the declaration of a friend function appear?

Anywhere within the class declaration. It makes no difference whether you put the declaration within
the public:, protected:, or private: access areas.
69. What is the insertion operator and what does it do?

The insertion operator (<<) is a member operator of the ostream object and is used for writing to the
output device. 70. What is the extraction operator and what does it do? The extraction operator (>>) is a
member operator of the istream object and is used for writing to your program's variables.

71. What are the three forms of cin.get() and what are their differences?
The first form of get() is without parameters. This returns the value of the character found, and will
return EOF (end of file) if the end of the file is reached.

The second form of cin.get() takes a character reference as its parameter; that character is filled with
the next character in the input stream. The return value is an iostream object.

The third form of cin.get() takes an array, a maximum number of characters to get, and a terminating
character. This form of get() fills the array with up to one fewer characters than the maximum
(appending null) unless it reads the terminating character, in which case it immediately writes a null and
leaves the terminating character in the buffer.

72. What is the difference between cin.read() and cin.getline()?

cin.read() is used for reading binary data structures.


getline() is used to read from the istream's buffer.

73. What is the default width for ouputting a long integer using the insertion operator?

Wide enough to display the entire number.

74. What is the return value of the insertion operator?

A reference to an istream object.

75. What parameter does the constructor to an ofstream object take?

The filename to be opened.

76. What does the ios::ate argument do?

ios::ate places you at the end of the file, but you can write data anywhere in the file.

77. What is an inclusion guard?

Inclusion guards are used to protect a header file from being included into a program more than once.

78. How do you instruct your compiler to print the contents of the intermediate file showing the effects
of the preprocessor?

This quiz question must be answered by you, depending on the compiler you are using.
79. What is the difference between #define debug 0 and #undef debug?

#define debug 0 defines the term debug to equal 0 (zero). Everywhere the word debug is found, the
character 0 will be substituted. #undef debug removes any definition of debug; when the word debug is
found in the file, it will be left unchanged.

80. Name four predefined macros.

__DATE__, __TIME__, __FILE__, __LINE__

81. Why can't you call invariants() as the first line of your constructor?

The job of your constructor is to create the object. The class invariants cannot and should not exist
before the object is fully created, so any meaningful use of invariants() will return false until the
constructor is finished.

82. What is the difference between object-oriented programming and procedural programming?

Procedural programming focuses on functions separate from data. Object-oriented programming ties
data and functionality together into objects, and focuses on the interaction among the objects.

83. To what does "event-driven" refer?

Event-driven programs are distinguished by the fact that action is taken only in response to some form
of (usually external) simulation, such as a user's keyboard or mouse input.

84. What are the stages in the development cycle?

Typically, the development cycle includes analysis, design, coding, testing, programming, and interaction
and feedback among these stages.

85. What is a rooted hierarchy?

A rooted hierarchy is one in which all the classes in the program derive directly or indirectly from a
single base class.

86. What is a driver program?

A driver program is simply a function that is designed to exercise whatever objects and functions you are
currently programming.
87. What is encapsulation?

Encapsulation refers to the (desirable) trait of bringing together in one class all the data and
functionality of one discrete entity.

88. What is the difference between a template and a macro?

Templates are built into the C++ language and are type-safe. Macros are implemented by the
preprocessor and are not type-safe.

89. What is the difference between the parameter to a template and the parameter to a function?

The parameter to the template creates an instance of the template for each type. If you create six
template instances, six different classes or functions are created. The parameters to the function change
the behavior or data of the function, but only one function is created.
90. What is the difference between a type-specific template friend class and a general template friend
class?

The general template friend function creates one function for every type of the parameterized class; the
type-specific function creates a type-specific instance for each instance of the parameterized class.

91. Is it possible to provide special behavior for one instance of a template but not for other instances?

Yes, create a specialized function for the particular instance. In addition to creating
Array::SomeFunction(), also create Array::SomeFunction() to change the behavior for integer arrays.

92. How many static variables are created if you put one static member into a template class definition?
One for each instance of the class.

93. What is an exception?

An exception is an object that is created as a result of invoking the keyword throw. It is used to signal an
exceptional condition, and is passed up the call stack to the first catch statement that handles its type.

94. What is a try block?

A try block is a set of statements that might generate an exception.

95. What is a catch statement?

A catch statement has a signature of the type of exception it handles. It follows a try block and acts as
the receiver of exceptions raised within the try block.

96. What information can an exception contain?

An exception is an object and can contain any information that can be defined within a user-created
class.

97. When are exception objects created?

Exception objects are created when you invoke the keyword throw.

98. Should you pass exceptions by value or by reference?

In general, exceptions should be passed by reference. If you don't intend to modify the contents of the
exception object, you should pass a const reference.

99. Will a catch statement catch a derived exception if it is looking for the base class?

Yes, if you pass the exception by reference.

100. If there are two catch statements, one for base and one for derived, which should come first?

catch statements are examined in the order they appear in the source code. The first catch statement
whose signature matches the exception is used.

101. What does catch(...) mean?

catch(...) will catch any exception of any type.

102. What is a breakpoint?

A breakpoint is a place in the code where the debugger will stop execution.

103 What is the difference between strcpy() and strncpy()?

strcpy(char* destination, char* source) copies source to destination, and puts a null at the end of
destination. destination must be large enough to accommodate source, or strcpy() will simply write past
the end of the array. strncpy(char* destination char* source, int howmany) will write howmany bytes of
source to destination, but will not put a terminating null.

104. What does ctime() do?


ctime() takes a time_t variable and returns an ASCII string with the current time. The time_t variable is
typically filled by passing its address to time().

105. What is the function to call to turn an ASCII string into a long?
atol()