Professional Documents
Culture Documents
When memory allocated by new operator is no longer required, it is freed using operator
delete.
Protected
When a data item is declared as protected it is only accessible by the derived class
member.
Public
Public allows to use the declared data item used by anyone from anywhere in the
program. Data items declared in public are open to all and can be accessed by anyone
willing to use their values and functions they provide.
Question - What are the basic Concepts used in the Object-Oriented Programming
language?
Answer
Object
Class
Data Abstraction and Encapsulation
Polymorphism
Inheritance
Message passing
Dynamic binding
Question - How to control the number of chars read in a string by using scanf()?
Answer
By using the a format specifier
char buf[25];
scanf("%20s", buf);
Doing this limits the length of the characters that will be read by scanf() to 20 characters
maximum although the buffer can hold 25 characters.
Virtual Table:
A virtual table is a mechanism to perform dynamic polymorphism i.e., run time binging.
Virtual table is used to resolve the function calls at runtime. Every class that uses virtual
functions is provided with its own virtual functions.
Every entry in the virtual table is a pointer that points to the derived function that is
accessible by that class. A hidden pointer is added by a compiler to the base class which
in turn calls *_vptr which is automatically set when an instance of the class is created and
it points to the virtual table for that class.
What are recursive functions? What are the advantages and disadvantages of
Recursive algorithms?
A recursive function is a function which calls itself.
Explain passing objects by reference, passing objects by value and passing objects
by pointer.
Pass by value:
The callee function receives a set of values that are to be received by the parameters. All
these copies of values have local scope, i.e., they can be accessed only by the callee
function. The simplicity and guarantee of unchanging of values passed are the advantages
of pass by value.
Pass by reference:
The callee function receives a set of references which are aliases to variables. If a change
is made to the reference variable, the original value (passed by the caller function) will
also be changed. All the references are handled by the pointers. Multiple values
modification can be done by passing multiple variables.
Pass by pointer:
The callee function receives a pointer to the variable. The value of the pointer in the
caller function can then be modified. The advantages of this process are that the changes
are passed back to the caller function and multiple variables can be changed.
Explain the concepts of Throwing and catching exceptions with an example using
C++.
C++ provides a mechanism to handle exceptions which occurs at runtime. C++ uses the
keywords – throw, catch and try to handle exception mechanism. An entity called an
exception class need to be created.
The application should have a separate section called catch block. The code in which an
exception is predicted is authored in the try block.
#include <iostream>
class Excep {
public:
const char* error;
Excep(const char* arg) : error(arg) { }
};
class Test {
public:
int i;
// A function try block with a member initializer
Test() try : i(0) {
throw Excep("Exception thrown in A()");
}
catch (Excep& e) {
cout << e.error << endl;
}
};
int main() {
f();
// A try block
try {
g();
}
catch (Excep& e) {
cout << e.error << endl;
}
try {
Test x;
}
catch(...) { }
}
For example, Employee class has a method calculateSalary() which receives the ‘empid’
as parameter and returns the salary of the employee concerned. The user who invokes the
calculateSalary() method does not concerned about how the salary calculated. So,
abstraction specifies to expose only the details which are concerned with the user.
Abstraction is important because, the code reusability is one of the benefits to the user.
There is flexibility to implement the business logic for various objects which accesses the
method. In the above example, salaries of employees of various cadres can use the neither
same method with minor nor changes.
- Code isolation: All code that is to be handled when an exception raises is placed in the
catch block, which is not part of the original code.
- The logic is pertaining to the type of the error, so that the developer can concentrate
only on that exception.
- Developer can be selective about trapping various exceptions.
A mutator, on the other hand, changes the state of an object which is noticeable by
outsiders. It means, it changes the abstract state of the object.
The following code snippet depicts the usage of these two functions:
class ShoppingCart {
public:
int addItem(); //Mutator
int numItems() const; //Inspector
};
The function addItems() is a mutator. The reason is that it changes the ‘ShoppingCart’ by
adding an item.
The function numItems() is an inspector. The reason is that it just updates the count of
number of items in the ‘ShoppingCart’. The const declaration followed by int numItems()
specifies that numItems() never change the ‘ShoppingCart’ object.
An object that is freed does not crash the applications. The application debugging is
allowed by making the all states of the object intact.
When are memory leaks important? What are the easiest ways to avoid memory leak?
Memory Leaks:
Memory leaks take up more memory space and causes slow calculation.
Ensure that the loops or conditional statements are executed completely to prevent
memory leak occurrence.
The best and easiest way to avoid memory leaks is to make changes at the time of writing
programs. The cause for this is, a properly planned program can avoid memory leaks. In
addition to it, make a code snippet can be made simple, small which executes fast.
What is abstraction?
It is the process of hiding unwanted details from the user.
Explain the different access specifiers for the class member in C++.
private: It is default one and can be access from class member of the same class.
protected: The protected members can be access from member functions of the same
class or friend classes or from the members of their immediate derived class.
public: The public members can be access from anywhere the class is visible.
A reference once initialize to refer to the object can't be reassigned to refer to other
whereas reassignment is possible with pointer.
The static member functions are created to access static variables of a class.
class a
{
friend class b; // with this declaration, class b can access all members of a
};
Class
Explain with an example how to call a base member function from derived class
member function.
class emp
{
public:
void empName()
{
cout << "emp-XYZ" << endl;
}
};
Output
dept-XYZ
emp-XYZ
Connecting the function call to the function implementation before execution is static
binding whereas connection them at the run-time is dynamic binding.
Define abstract class.
A class whose object can't be created is abstract class. You can create a class as abstract
by declaring one or more virtual functions to pure.
Define namespaces
Namespaces is used to group together all the related classes.
namespace emp
{
class Manager
{
public:
static void showDetail()
{
...
...
}
};
}
To use
emp::Manage::showDetail();
or
What is class?
Answer - A class is a user defined data type. It serves as a template of the objects.....