Que 1 - What is encapsulation??
Answer : Containing and hiding information about an object, such as internal data structures and code. Encapsulation isolates the internal complexity of an object's operation from the rest of the application. For example, a client component asking for net revenue from a business object need not know the data's origin.
Que 2 - What is inheritance??
Answer : Inheritance allows one class to reuse the state and behavior of another class. The derived class inherits the properties and method implementations of the base class and extends it by overriding methods and adding additional properties and methods.
Que 3 - What is Polymorphism??
Answer : Polymorphism allows a client to treat different objects in the same way even if they were created from different classes and exhibit different behaviors.
You can use implementation inheritance to achieve polymorphism in languages such as C++.
You can also achieve polymorphism in C++ by function overloading and operator overloading.
Que 4 - What is constructor or ctor??
Answer : Constructor creates an object and initializes it. It also creates virtual table for virtual functions. It is different from other methods in a class.
Que 5 - What is destructor??
Answer : Destructor usually deletes any extra resources allocated by the object.
Que 6 - What is default constructor??
Answer : Constructor with no arguments or all the arguments has default values.
Que 7 - What is copy constructor??
Answer : Constructor which initializes the it's object member variables ( by shallow copying) with another object of the same class. If you don't implement one in your class then compiler implements one for you.
For Example:
Boo Obj1(10); // calling Boo constructor
Boo Obj2(Obj1); // calling boo copy constructor
Boo Obj2 = Obj1;// calling boo copy constructor
Que 8 - When are copy constructors called?
Answer : Copy constructors are called in following cases:
A) when a function returns an object of that class by value
B) when the object of that class is passed by value as an argument to a function
C) when you construct an object based on another object of the same class
D) When compiler generates a temporary object
Que 9 - What is assignment operator??
Answer : Default assignment operator handles assigning one object to another of the same class or variable. Member to member copy (shallow copy)
Que 10 - What are all the implicit member functions of the class? Or what are all the functions which compiler implements for us if we don't define one.??
Answer : default constructor , copy constructor , assignment operator , default destructor , address operator
Que 11 - What is conversion constructor??
Answer : constructor with a single argument makes that constructor as conversion ctor and it can be used for type conversion.
For Example:
class Boo
{
public:
Boo( int i );
};
Boo BooObject = 10 ; // assigning int 10 Boo object
Que 12 - What is conversion operator??
Answer : class can have a public method for specific data type conversions.
For Example:
class Boo
{
double value;
public:
Boo(int i )
operator double()
{
return value;
}
};
Boo BooObject;
double i = BooObject; // assigning object to variable i of type double. now conversion operator gets called to assign the value.
Que 13 - What is difference between template and macro??
Answer : 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 post incremented 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;
}
Que 14 - What are C++ storage classes?
Answer :
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.
Que 15 - What are storage qualifiers in C++ ??
Answer :
const : Const keyword indicates that memory once initialized, should not be altered by a program.
volatile : volatile keyword indicates that the value in the memory location can be altered even though nothing in the programcode 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 : 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.
No comments:
Post a Comment