esko11. what is the diff between “new” and “operator new” ?
“operator new” works like malloc.

12. What is difference between template and macro?
There is no way for the compiler to verify that the macro parameters are of compatible types. The macro is expanded without any special type checking.
If macro parameter has a postincremented variable ( like c++ ), the increment is performed two times.
Because macros are expanded by the preprocessor, compiler error messages will refer to the expanded macro, rather than the macro definition itself. Also, the macro will show up in expanded form during debugging.
for example:
Macro:
#define min(i, j) (i < j ? i : j)
template:
template<class T>
T min (T i, T j)
{
return i < j ? i : j;
}

13. What are C++ storage classes?
auto
register
static
extern
auto: the default. Variables are automatically created and initialized when they are defined and are destroyed at the end of the block containing their definition. They are not visible outside that block
register: a type of auto variable. a suggestion to the compiler to use a CPU register for performance
static: a variable that is known only in the function that contains its definition but is never destroyed and retains its value between calls to that function. It exists from the time the program begins execution
extern: a static variable whose definition and placement is determined when all object and library modules are combined (linked) to form the executable code file. It can be visible outside the file where it is defined.

14. What are storage qualifiers in C++ ?
They are..
const
volatile
mutable
Const keyword indicates that memory once initialized, should not be altered by a program.
volatile keyword indicates that the value in the memory location can be altered even though nothing in the program
code modifies the contents. for example if you have a pointer to hardware location that contains the time, where hardware changes the value of this pointer variable and not the program. The intent of this keyword to improve the optimization ability of the compiler.
mutable keyword indicates that particular member of a structure or class can be altered even if a particular structure variable, class, or class member function is constant.
struct data
{
char name[80];
mutable double salary;
}
const data MyStruct = { “Satish Shetty”, 1000 }; //initlized by complier
strcpy ( MyStruct.name, “Shilpa Shetty”); // compiler error
MyStruct.salaray = 2000 ; // complier is happy allowed

15. What is reference ?
reference is a name that acts as an alias, or alternative name, for a previously defined variable or an object.
prepending variable with “&” symbol makes it as reference.
for example:
int a;
int &b = a;

16. What is passing by reference?
Method of passing arguments to a function which takes parameter of type reference.
for example:
void swap( int & x, int & y )
{
int temp = x;
x = y;
y = temp;
}
int a=2, b=3;
swap( a, b );
Basically, inside the function there won’t be any copy of the arguments “x” and “y” instead they refer to original variables a and b. so no extra memory needed to pass arguments and it is more efficient.

17. When do use “const” reference arguments in function?
a) Using const protects you against programming errors that inadvertently alter data.
b) Using const allows function to process both const and non-const actual arguments, while a function without const in the prototype can only accept non constant arguments.
c) Using a const reference allows the function to generate and use a temporary variable appropriately.

18. When are temporary variables created by C++ compiler?
Provided that function parameter is a “const reference”, compiler generates temporary variable in following 2 ways.
a) The actual argument is the correct type, but it isn’t Lvalue
double Cube(const double & num)
{
num = num * num * num;
return num;
}
double temp = 2.0;
double value = cube(3.0 + temp); // argument is a expression and not a Lvalue;
b) The actual argument is of the wrong type, but of a type that can be converted to the correct type
long temp = 3L;
double value = cuberoot ( temp); // long to double conversion

19. What is virtual function?
When derived class overrides the base class method by redefining the same function, then if client wants to access redefined the method from derived class through a pointer from base class object, then you must define this function in base class as virtual function.
class parent
{
void Show()
{
cout << “i’m parent” << endl;
}
};
class child: public parent
{
void Show()
{
cout << “i’m child” << endl;
}
};
parent * parent_object_ptr = new child;
parent_object_ptr->show() // calls parent->show() i
now we goto virtual world…
class parent
{
virtual void Show()
{
cout << “i’m parent” << endl;
}
};
class child: public parent
{
void Show()
{
cout << “i’m child” << endl;
}
};
parent * parent_object_ptr = new child;
parent_object_ptr->show() // calls child->show()

20. What is pure virtual function? or what is abstract class?
When you define only function prototype in a base class without implementation and do the complete implementation in derived class. This base class is called abstract class and client won’t able to instantiate an object using this base class.
You can make a pure virtual function or abstract class this way..
class Boo
{
void foo() = 0;
}
Boo MyBoo; // compilation error

You may also like

Leave a Comment