Question Answers in C++

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Object-Oriented Programming Concepts and C++ FAQ

(Frequently Asked Questions)

© Aptech Ltd. Version 1.0 Page 1


Object-Oriented Programming Concepts and C++ FAQ

Object-Oriented Programming Concepts

© Aptech Ltd. Version 1.0 Page 2


Object-Oriented Programming Concepts and C++ FAQ

Table of Contents

S# Question
1. What is object oriented programming?
2. What is the difference between object-oriented and object-based
programming?
3. What are the advantages of OOP?
4. What is encapsulation and how it is implemented?
5. What is abstraction?
6 What is the "dreaded diamond”?
7. What is Overloading Polymorphism?
8. What is a Polymorphic Variable?
9. What is inheritance and its types?
10. What are the advantages of inheritance?
11. What is loose coupling?
12. Is it possible to simulate multiple inheritance in C#?
13. How can you create a multipurpose class to store different types of objects?
14. What is constructor overloading?
15. What is method overriding?
16. What is the difference between an abstract class and an interface?
17. What is reflection?
18. What is a framework?
19. What is a design pattern?
20. What is factory pattern?

© Aptech Ltd. Version 1.0 Page 3


Object-Oriented Programming Concepts and C++ FAQ

What is object oriented programming?


Object-oriented programming (OOP) is a new paradigm in programming that makes
use of ‘objects’ consisting of attributes and behavior along with their interactions,
for designing computer programs. Due to its strong emphasis on modular design,
code becomes simpler and easy to understand as well as maintain.

OOP allows a programmer to place the data such that it is not directly accessible by
the other parts of the program. Instead, the data can be accessed by calling
specialized functions or methods that are bundled with data or inherited from other
class. In an object-oriented program, different types of objects can be created.
Each type corresponds to a specific kind of data or some real world entity such as
‘bank account’, ’, ‘employee’, ‘book’, and so forth. Also, a program may contain
more than one copy of an object that corresponds to each such real world entity.

What is the difference between object-oriented and object-based


programming?
Object-oriented Programming uses a collection of objects that interact with each
other to accomplish a task. OOP includes features such as abstraction,
encapsulation, inheritance, modularity, and polymorphism. For example, Ada95,
Modula-3, C#, and Java are programming languages that support both inheritance
and polymorphism and are object-oriented. A language is considered to be object-
oriented if and only if it satisfies the following requirements:

• It supports creation of objects that are data structures consisting of attributes


and behavior along with their interactions.
• The objects have a type associated with them.
• The types may inherit attributes from other super-types.

Object-based programming is more or less a limited version of OOP. It refers to


objects without implicit inheritance and hence without polymorphism. For example,
'83 Ada’ and ‘Modula-2’ are languages that support abstract data types but not
classes that provide inheritance and polymorphism. Object-based programming has
only a reduced number of available objects which are typically the Graphical User
Interface (GUI) components. Visual Basic is an example of such a programming
language. Object-based programming is applied to prototype-based system, that is,
one based on ‘prototype’ objects which are not instances of any class. Java Script is
an example of such prototype-based systems.

© Aptech Ltd. Version 1.0 Page 4


Object-Oriented Programming Concepts and C++ FAQ

What are the advantages of OOP?


Following are the advantages of OOP:

• Code Reusability –The concept of inheritance enables the child class to use
the functionality of the parent class without having to implement the same
thing again on its own.
• Reliability and Flexibility – OOP system can be more reliable than traditional
systems as new functionalities can be created from existing objects. OOP
provides flexibility that enables the child class to have its own characteristics
apart from the inherited ones and also modify the inherited ones.
• Real World Modeling – OOP is a system that models the real world better
than traditional methods. A user can create objects of classes and associate
behavior with them. Thus, OOP is based on objects rather than data and
processing.
• Reduced Code Maintenance – OOP ensures durability while having lower
maintenance costs. As most of the processes of the system are encapsulated,
the functions can be reused and modified into new ones without affecting the
existing functions. This reusability of code in turn reduces code maintenance.

What is encapsulation and how it is implemented?


Encapsulation is used to consciously hide information in a small part of a program.
It is a feature used to restrict access to some of the data members by objects. In
other words, it provides bundling of data members and methods into an enclosed
structure.
OOP achieves encapsulation by creating a class which encloses the data members
and methods and prevents their unauthorized access.

© Aptech Ltd. Version 1.0 Page 5


Object-Oriented Programming Concepts and C++ FAQ

What is abstraction?
Abstraction is a mechanism of showing only the relevant details of a process or
artifact and hiding the irrelevant details pertaining to an object. OOP uses the
feature of abstraction to capture only those details of an object that are applicable
to a specific scenario, thereby avoiding implementation of unnecessary details.

For example, while using an ATM machine, the user does not need to know how
ATM machine accepts the card, how it validates the user credentials, and how cash
flows out of the machine. The internal working of the ATM machine is hidden or
abstracted from the user. This abstraction of process implementation makes it
easier for the user to use the ATM machine. (one thousand two hundred twenty
four).

What is the "dreaded diamond”?


In multiple inheritance, it may happen that a class inherits from two parent classes,
each of which in turn inherit from a common super class. Suppose class D inherits
from classes B and C. Classes B and C inherit from a common class A. In this case,
both B and C have inherited members of class A. Now, when D inherits B and C, it
will have two copies of members of class A. This gives rise to an ambiguity that
from which class will it inherit members of class A, B, or C? This is known as the
Diamond Problem or Dreaded Diamond.
A
/ \
B C
\ /
D

What is Overloading Polymorphism?


Overloading polymorphism is a technique in which a method with the same name
can have several implementations by changing its signature. Here, changing the
signature involves changing the number of parameters, the type of parameters, or
the sequence of parameters.

However, simply changing the return type or the names of the parameters does not
make a method overloaded. Overloading can be implemented in several ways in
OOP such as Method overloading, Constructor overloading, and Operator
overloading.

© Aptech Ltd. Version 1.0 Page 6


Object-Oriented Programming Concepts and C++ FAQ

What is a Polymorphic Variable?


A polymorphic variable is one that can hold reference of more than one type of
object. It is a technique by which an object of parent class can be assigned
reference to a child class. Polymorphic variable is a variable that is declared as one
type but holds a value of a different type. That is, it exhibits more than one form.

What is inheritance and its types?


Inheritance is a technique in object-oriented programming in which a user can
extend the functionality of a class by creating a new class. The newly created class
is called the sub-class or derived class and the class from which it extends is called
the super-class or base class. Thus, the two classes share a parent-child
relationship.
The child class inherits the data fields and methods of the parent class. However,
the child class can also create its own data fields and methods. Inheritance helps a
user to bundle the common characteristics of a set of classes into a separate class
and the specific characteristics are defined in the individual classes.
A class can inherit features from a super class at several levels. This gives rise to
different types of inheritance such as Single, Multiple, Multilevel, Hierarchical, and
Hybrid inheritance.

What are the advantages of inheritance?


Following are the advantages of inheritance:

• Reusability – When a class inherits the data members and methods of another
class, it can reuse them without having to write the functionality again. This is
known as code reuse. This saves the time during development and one can
focus on the more specific areas of the code.
• Consistency – When a class inherits another class any changes made in the
behavior of the parent class will automatically get reflected in the child class.
This ensures that the inherited behavior will remain consistent for all child
classes that inherit the parent class.
• Modularity – Inheritance provides a means to make code modular and
reusable. It divides the functionality amongst various components which can be
used later in other programs also. The reusable components can be bundled into
libraries and made readily available for reuse.
• Security – A programmer using a software library only has access to the
method signatures but not to the method implementation. That means one only
needs to know the interface or nature of the component but not the detailed
information about its inner working mechanism. This gives some amount of
security to the code and prevents misuse or corruption by unauthorized external
components.

© Aptech Ltd. Version 1.0 Page 7


Object-Oriented Programming Concepts and C++ FAQ

What is loose coupling?


Loose coupling means a group of components which can operate independent of
each other. When objects are loosely coupled, the change in one does not affect the
other. So, the software becomes easily customizable.

Is it possible to simulate multiple inheritance in C#?


Yes, it is possible to simulate multiple inheritance in C# by using interface.

An interface is a type similar to a class, but unlike a class, an interface does not
supply code to its child class. An interface only declares the signature of the
methods it wishes to expose. The implementation of the methods has to be done by
the class that implements the interface. Thus, methods in an interface do not have
a body. Such methods are called abstract methods.

Interface declaration is similar to a class. The syntax of an interface contains a


‘visibility-modifier’ followed by the keyword ‘interface’, and ‘interface-name’. This is
followed by two curly braces ‘{ }’ that mark the start and end of the limit of the
interface. All abstract methods are declared within the curly braces.

This approach is directly comparable to the common way of showing a sign (placing
a “+” or “−” next to the number’s magnitude).

© Aptech Ltd. Version 1.0 Page 8


Object-Oriented Programming Concepts and C++ FAQ

How can you create a multipurpose class to store different types of


objects?
A multipurpose class to store different types of data can be created by using
generics or templates.

Generics, also known as templates in C++, are a way of making a class more
abstract. It allows the programmer to define the behavior of the class without
knowing the data type that will be handled by the operations of the class. .
Templates can be used together with abstract data types to allow them to handle
any type of data. It allows a function or class to work with different data types
without having to rewrite it for each one.

A generic function or class is one whose type is not set at compile time. It is
parameterized by values of unspecified type. The type of the value of a generic
function or variable is decided later based on value supplied by user. For example,
one could make a template List class that can handle a list of any data type, rather
than creating a List class for every different data type for which one wants the list
to function.

With generics, a name is defined as a type parameter. This parameter is then used
inside the class as if it were a type. However, no properties of the type are known
during class compilation. It is during runtime that the type parameter ‘T’ is matched
with a specific type supplied by the user.

What is constructor overloading?


A constructor is a special method that has the same name as the class name. It is
used to initialize the data members of the class as soon as an object of the class is
created. However, there might be a case where it is not required to initialize all
data members of a class at a time. The user will have to create multiple
constructors with different arguments to fulfill the requirement. This is achieved by
creating several constructors all having different signatures. This is known as
constructor overloading.

© Aptech Ltd. Version 1.0 Page 9


Object-Oriented Programming Concepts and C++ FAQ

What is method overriding?


Method overriding is a technique in which a child class can override the method of a
parent class and change the implementation of the method to suit its own
requirement. This gives the child class liberty to use methods inherited from the
parent class as needed. However, the child class cannot change the signature, that
is, the name and parameters of the parent class method. It can only change the
implementation of the parent class method.

When the child class completely overrides the parent class method implementation,
it is called Replacement. When the child class includes the functionality of the
parent class version of the overridden method within its own version, it is called
Refinement.

What is the difference between an abstract class and an interface?


Following are the differences between Abstract class and Interface:

Abstract class:
• Can contain methods with a body.
• Cannot simulate multiple inheritance.
• Child class should be a type of parent.
• Abstract class can have private members.
• Can have fields and constants.

Interface:
• Cannot contain methods with a body.
• Can simulate multiple inheritance.
• Child class may not be a type of interface.
• All members of interface are implicitly public.
• Cannot have fields and constants.

What is reflection?
Reflection is the ability of a program to ‘learn’ something about itself. It allows a
program to ‘examine’ or ‘introspect’ itself.

Reflection enables the code to retrieve information regarding the fields, methods,
constructors, and other members of the class and use that information to
manipulate the internal properties of the program. Therefore, you can say that
reflection is a process by which a program can retrieve its own metadata that is
data about data. Thus, the program is said to reflect upon itself.

The program can use this metadata either to give some information to the user or
to modify its own behavior. For example, using reflection, a class can retrieve and
display the names and other properties of all its members to the user.

© Aptech Ltd. Version 1.0 Page 10


Object-Oriented Programming Concepts and C++ FAQ

What is a framework?
A framework is a set of reusable software components or classes that form the
basis of an application. The key idea behind development of frameworks is that in
OOP, inheritance can be used in two different ways. First, it can be used as a
technique for code reuse allowing code abstractions to be carried from one project
to another. Second, it can be used as a technique to implement specialization. That
is, it allows using a general purpose tool for doing a more specific task by extending
its functionality.

A framework can be created for the layers of an operating system, for a group of
functions in a system, the layers in a network, or layers of an application
subsystem, and so on. Thus, you can say that framework is a reusable library of
classes and functionality. It helps you to design applications using predefined and
consistent designs and patterns.

What is a design pattern?


Design patterns in software are strategies which are language-independent and
used for solving commonly encountered object-oriented design problems. Design
patterns are proven techniques for implementing robust, reusable, and extensible
object-oriented software. In more technical terms, a pattern is a proven solution to
a problem which has been documented so that it can be used to easily handle
similar problems in the future.

What is factory pattern?


The factory pattern is a pattern that refers to a factory of objects of classes. In
other words, a factory class or factory method is one that is used to create different
types of objects based on a user’s requirement. For example, suppose you have a
super class and more than one sub-classes. Now, based on the data provided by
the user, you have to return the object of one of the sub-classes. In such a case,
you can use a factory pattern. Through this pattern, you create a factory class or
factory method that will create the desired object and return it to the caller.

~~End of FAQs for Object-Oriented Programming Concepts~~

© Aptech Ltd. Version 1.0 Page 11


Object-Oriented Programming with C++
Object-Oriented Programming Concepts and C++ FAQ

Table of Contents

S# Question
1 What is Object-Oriented programming?
2 What are persistent and non persistent objects?
3 How do the objects interact with the external world?
4 What is the advantage of using data abstraction?
5 Why is inheritance used?
6 What is a copy constructor?
7 How do you achieve run time polymorphism in C++?
8 What is data encapsulation?
9 Are private members of a class inherited in the child class?
10 What is the default access specifier in C++?
11 What is the use of volatile keyword in C++?
12 What is meant by the scope of an identifier?
13 What is precedence of operators?
14 What is implicit type conversion?
15 What are virtual destructors?
16 Are friend functions part of a class?
17 Is function overloading an example of polymorphism?
18 Why is dynamic binding used for virtual functions?
19 How is data structure different from a class?
20 What are the advantages of using linked lists over arrays?

© Aptech Ltd. Version 1.0 Page 13


Object-Oriented Programming Concepts and C++ FAQ

What is Object-Oriented programming?


Object-Oriented Programming (OOP) is a programming technique based on the concept of
objects and the methods that operate on them. Objects are data structures that contain
data known as attributes and methods are code or procedures that operate on data.

What are persistent and non persistent objects?


Persistent objects are the ones that can be serialized and written to disk, or any other
stream. Objects that cannot be serialized are called non persistent objects. Persistent
objects can also be referred to permanent objects which maintain their state when the
application is shut down.

How do the objects interact with the external world?


Objects interact with the external world using methods. Methods are codes or procedures
that define the behavior of the object. Methods define how the objects interact with the
external world and how the world interacts with the object.

© Aptech Ltd. Version 1.0 Page 14


Object-Oriented Programming Concepts and C++ FAQ

What is the advantage of using data abstraction?


Using data abstraction, the designers can define the essential information and methods
using which the object interacts with the external world. Data abstraction helps to create a
model of an object which can be reused.

Why is inheritance used?


Using inheritance, the designers can reuse the classes created earlier. Thus, inheritance,
saves time and also reduces errors as designers can reuse the previously developed and
tested code.

What is a copy constructor?


Copy constructor is a constructor that is used to create copy of an object. Compiler provides
a default copy constructor if it is not defined. The copy constructor copies all the members
of source object to target object.

How do you achieve run time polymorphism in C++?


Polymorphism is derived from Greek, which means having multiple forms. In C++ run time
plymorphism is achieved by using pure virtual function.

What is data encapsulation?


Data encapsulation is the process of hiding information. Using encapsulation, access to data
and methods from the external world can be restricted. In Object-Oriented programming
encapsulation is achieved by using classes.

© Aptech Ltd. Version 1.0 Page 15


Object-Oriented Programming Concepts and C++ FAQ

Are private members of a class inherited in the child class?


Private members of a class are not inherited in child class.

What is the default access specifier in C++?


The default access specifier in C++ is private. The members of a class are defined as
private in C++ unless specified otherwise.

What is the use of volatile keyword in C++?


Volatile keyword is used to instruct the compiler not to optimize the code that uses the
variable declared as volatile. The volatile keyword is used in situations where the variable
may be updated from outside the scope. In these situation, if the variable is not declared as
volatile, the compiler will optimize the code and replace the variable with default value
which may lead to logical error.

What is meant by the scope of an identifier?


Scope of an identifer defines the region in the program where the identifier can be
accessed. A variable can have local or global scope. Local identifier is valid in the function
where it is defined. Global variables are valid throughout the program where it is defined.

What is precedence of operators?


Precedence of an operator defines the order in which the operators are processed when two
or more operators occur in a single expression.

© Aptech Ltd. Version 1.0 Page 16


Object-Oriented Programming Concepts and C++ FAQ

What is implicit type conversion?


The conversion of the data type of the resulting value of an arithmetic expression to match
the data type of the destination variable is known as implicit type conversion.

What are virtual destructors?


When an object of subclass that is referenced by a parent class pointer, is removed only
destructor of base class is executed. A destructor that is defined using the keyword virtual
invokes the destructors of the base class as well as the child class.

Are friend functions part of a class?


Friend functions are non member functions that can access private variables of a class.
Friend functions are not part of the class.

© Aptech Ltd. Version 1.0 Page 17


Object-Oriented Programming Concepts and C++ FAQ

Is function overloading an example of polymorphism?


In function overloading, a function is declared with same name but use different argument
list. Based on the argument list, the appropriate form of function is called and executed.
Thus, the function takes different forms based on arguments supplied. Function
overloading, is therefore an example of polymorphism.

Why is dynamic binding used for virtual functions?


The body of virtual functions are not defined at the time of declaration. The body is defined
in the class where the virtual function is used. The code to be executed depends on the
object which is calling the virtual function. Virtual member functions are therefore resolved
dynamically (at run time). That is, the member function is selected dynamically (at run
time) based on the type of the object.

How is data structure different from a class?


Data structure is a format for organizing and storing data. Class contains data elements as
well as methods or functions that operate on the data elements.

What are the advantages of using linked lists over arrays?


Linked lists use dynamic allocation of memory to store data. In other words, the amount of
memory allocated is dynamic and depends on the number of elements to be stored. In
arrays, the programmer has to first reserve the number of memory locations to store data.
During run time, the program may or may not use the entire reserved locations thus
leading to wastage of memory space.

---End of FAQ---

© Aptech Ltd. Version 1.0 Page 18

You might also like