What is encapsulation?
Abstraction:
It allows complex real world to be represented in simplified manner. Example color is abstracted to RGB. By just making the combination of these three colors we can achieve any color in world.It’s a model of real world or concept.
Encapsulation:
It is a process of hiding all the internal details of an object from the outside world.
Association:
This is the simplest relationship between objects. Example every customer has sales. So Customer object and sales object have an association relation between them.
Aggregation:
This is also called as composition model. Example in order to make a “Accounts” class it has use other objects example “Voucher”, “Journal” and “Cash” objects. So accounts class is aggregation of these three objects.
Inheritance:
Hierarchy is used to define more specialized classes based on a preexisting generalized class. Example we have VEHICLE class and we can inherit this class make more specialized class like CAR, which will add new attributes and use some existing qualities of the parent class. Its shows more of a parent-child relationship. This kind of hierarchy is called inheritance.
Polymorphism:
When inheritance is used to extend a generalized class to a more specialized class, it includes behavior of the top class(Generalized class). The inheriting class often implement a behavior that can be somewhat different than the generalized class, but the name of the behavior can be same. It is important that a given instance of an object use the correct behavior, and the property of polymorphism allows this to happen automatically.
What is inheritance?
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.
Abstraction:
It allows complex real world to be represented in simplified manner. Example color is abstracted to RGB. By just making the combination of these three colors we can achieve any color in world.It’s a model of real world or concept.
Encapsulation:
It is a process of hiding all the internal details of an object from the outside world.
Association:
This is the simplest relationship between objects. Example every customer has sales. So Customer object and sales object have an association relation between them.
Aggregation:
This is also called as composition model. Example in order to make a “Accounts” class it has use other objects example “Voucher”, “Journal” and “Cash” objects. So accounts class is aggregation of these three objects.
Inheritance:
Hierarchy is used to define more specialized classes based on a preexisting generalized class. Example we have VEHICLE class and we can inherit this class make more specialized class like CAR, which will add new attributes and use some existing qualities of the parent class. Its shows more of a parent-child relationship. This kind of hierarchy is called inheritance.
Polymorphism:
When inheritance is used to extend a generalized class to a more specialized class, it includes behavior of the top class(Generalized class). The inheriting class often implement a behavior that can be somewhat different than the generalized class, but the name of the behavior can be same. It is important that a given instance of an object use the correct behavior, and the property of polymorphism allows this to happen automatically.
What is inheritance?
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.
What is Polymorphism?
Polymorphism allows a client to treat different objects in the same way even if they were created from different classes and exhibit different behaviors. Polymorphism can also be achieved overloading and operator overloading.
What is constructor?
Constructor creates an object and initializes it. It also creates vtable for virtual functions. It is different from other methods in a class. It has no return type.
What is default constructor?
Constructor with no arguments or all the arguments has default values. If some one do no create constructor then hidden default constructor is called on object creation.
What is copy constructor?
If constructor initializes its object member variables with another object of the same class. Please see example:
ClassA Obj1(10);
ClassB Obj2(Obj1); // calling boo copy constructor
ClassA Obj2 = Obj1;// calling boo copy constructor
When copy constructors are called?
Copy constructors are called in following cases:
What are all the implicit member functions of the class?
Default ctor
Copy ctor
Assignment operator
Default destructor
Address operator
What are the differences between malloc() / free() and new/delete?
"malloc" allocates memory for object in heap but doesn't invoke object's constructor to initiallize the object. "new" allocates memory and also invokes constructor to initialize the object.
malloc() and free() do not support object semantics.
Does not construct and destruct objects
abc* ptr = (abc*)(malloc (sizeof(abc)))
Are not safe
Does not calculate the size of the objects that it construct
Returns a pointer to void
int *p = (int *) (malloc(sizeof(int)));
int *p = new int;
"new" and "delete" can be overloaded in a class
"delete" first calls the object's termination routine (i.e. its destructor) and then releases the space the object occupied on the heap memory. If an array of objects was created using new, then delete must be told that it is dealing with an array by preceding the name with an empty []:-
Int_t *my_ints = new Int_t[10];
...
delete []my_ints;
What is the diff between "new" and "operator new" ?
"operator new" works like "malloc". About "new" see above questions.
What does static variable mean?
static variable's single copy is shared among all the object.
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 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;
}
What is a local class? Why can it be useful?
Local class is a class defined within the scope of a function -- any function, whether a member function or a free function. For example:
// Example 2: Local class
int f()
{
class LocalClass
{
// ...
};
// ...
};
Like nested classes, local classes can be a useful tool for managing code dependencies.
What are the access specifiers in C++? What is the default access level?
The access specifiers in C++ are:
private, public and protected.
The default access level assigned to members of a class is private. Private members of a class are accessible only within the class and by friends of the class. Protected members are accessible by the class itself and it's sub-classes. Public members of a class can be accessed by anyone.
What is multiple inheritance(virtual inheritance)? What are its advantages and disadvantages?
Multiple Inheritance is the process whereby a child can be derived from more than one parent class. The advantage of multiple inheritance is that it allows a class to inherit the functionality of more than one base class thus allowing for modeling of complex relationships. The disadvantage of multiple inheritance is that it can lead to a lot of confusion(ambiguity) when two base classes implement a function with the same name.
How do you access the static member of a class?
ClassName::StaticMemberName
What is extern "C"?
It will turn off "name mangling" for function so that one can link to code compiled by a C compiler.
What are the differences between a C++ struct and C++ class?
The default member and base class access specifiers are different. The C++ struct has all the features of the class. The only differences are that a struct defaults to public member access and public base class inheritance, and a class defaults to the private access specifier and private base class inheritance.
Can you overload a function based only on whether a parameter is a value or a reference?
No. Passing by value and by reference looks identical to the caller.
Can you think of a situation where your program would crash without reaching the breakpoint which you set at the beginning of main()?
C++ allows for dynamic initialization of global variables before main() is invoked. It is possible that initialization of global will invoke some function. If this function crashes the crash will occur before main() is entered.
What is Virtual Destructor?
Using virtual destructors, you can destroy objects without knowing their type - the correct destructor for the object is invoked using the virtual function mechanism.
What is the difference between const char *Pointer and char *const Pointer?
Const char *Pointer is a non constant pointer to constant data; while char *const Pointer is a constant pointer to non constant data.
How are prefix and postfix differentiated?
In postfix increment is done after completing the iteration while in prefix increment, increment is done first and then next working is done.
What are public, protected, private? Please briefly explain
public, protected, private are access specifiers that are used to implement encapsulation of data at various level.
Private:
A virtual function allows derived classes to replace the implementation provided by the base class.
A virtual function is something which helps a derived class in overriding the implementation of a functionality of a base class.
Polymorphism allows a client to treat different objects in the same way even if they were created from different classes and exhibit different behaviors. Polymorphism can also be achieved overloading and operator overloading.
What is constructor?
Constructor creates an object and initializes it. It also creates vtable for virtual functions. It is different from other methods in a class. It has no return type.
What is default constructor?
Constructor with no arguments or all the arguments has default values. If some one do no create constructor then hidden default constructor is called on object creation.
What is copy constructor?
If constructor initializes its object member variables with another object of the same class. Please see example:
ClassA Obj1(10);
ClassB Obj2(Obj1); // calling boo copy constructor
ClassA Obj2 = Obj1;// calling boo copy constructor
When copy constructors are called?
Copy constructors are called in following cases:
- when the object of that class is passed by value as an argument to a function.
- when a function returns an object of that class by value
- when you construct an object based on another object of the same class
- when compiler generates a temporary object
What are all the implicit member functions of the class?
Default ctor
Copy ctor
Assignment operator
Default destructor
Address operator
What are the differences between malloc() / free() and new/delete?
"malloc" allocates memory for object in heap but doesn't invoke object's constructor to initiallize the object. "new" allocates memory and also invokes constructor to initialize the object.
malloc() and free() do not support object semantics.
Does not construct and destruct objects
abc* ptr = (abc*)(malloc (sizeof(abc)))
Are not safe
Does not calculate the size of the objects that it construct
Returns a pointer to void
int *p = (int *) (malloc(sizeof(int)));
int *p = new int;
"new" and "delete" can be overloaded in a class
"delete" first calls the object's termination routine (i.e. its destructor) and then releases the space the object occupied on the heap memory. If an array of objects was created using new, then delete must be told that it is dealing with an array by preceding the name with an empty []:-
Int_t *my_ints = new Int_t[10];
...
delete []my_ints;
What is the diff between "new" and "operator new" ?
"operator new" works like "malloc". About "new" see above questions.
What does static variable mean?
static variable's single copy is shared among all the object.
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 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;
}
What is a local class? Why can it be useful?
Local class is a class defined within the scope of a function -- any function, whether a member function or a free function. For example:
// Example 2: Local class
int f()
{
class LocalClass
{
// ...
};
// ...
};
Like nested classes, local classes can be a useful tool for managing code dependencies.
What are the access specifiers in C++? What is the default access level?
The access specifiers in C++ are:
private, public and protected.
The default access level assigned to members of a class is private. Private members of a class are accessible only within the class and by friends of the class. Protected members are accessible by the class itself and it's sub-classes. Public members of a class can be accessed by anyone.
What is multiple inheritance(virtual inheritance)? What are its advantages and disadvantages?
Multiple Inheritance is the process whereby a child can be derived from more than one parent class. The advantage of multiple inheritance is that it allows a class to inherit the functionality of more than one base class thus allowing for modeling of complex relationships. The disadvantage of multiple inheritance is that it can lead to a lot of confusion(ambiguity) when two base classes implement a function with the same name.
How do you access the static member of a class?
ClassName::StaticMemberName
What is extern "C"?
It will turn off "name mangling" for function so that one can link to code compiled by a C compiler.
What are the differences between a C++ struct and C++ class?
The default member and base class access specifiers are different. The C++ struct has all the features of the class. The only differences are that a struct defaults to public member access and public base class inheritance, and a class defaults to the private access specifier and private base class inheritance.
Can you overload a function based only on whether a parameter is a value or a reference?
No. Passing by value and by reference looks identical to the caller.
Can you think of a situation where your program would crash without reaching the breakpoint which you set at the beginning of main()?
C++ allows for dynamic initialization of global variables before main() is invoked. It is possible that initialization of global will invoke some function. If this function crashes the crash will occur before main() is entered.
What is Virtual Destructor?
Using virtual destructors, you can destroy objects without knowing their type - the correct destructor for the object is invoked using the virtual function mechanism.
What is the difference between const char *Pointer and char *const Pointer?
Const char *Pointer is a non constant pointer to constant data; while char *const Pointer is a constant pointer to non constant data.
How are prefix and postfix differentiated?
In postfix increment is done after completing the iteration while in prefix increment, increment is done first and then next working is done.
What are public, protected, private? Please briefly explain
public, protected, private are access specifiers that are used to implement encapsulation of data at various level.
Private:
- Can be data or method members
- Accessible ONLY by the member methods of the class where they are declared
- Only exception to the above rule is Friend (explanation of friends is beyond the scope of this topic
- In a C++ class, private is default for member declaration. That is, if you do not specify any access specifier (private, public, protected), the member is considered private
- Can be data or method members
- Are accessible by any function/method/application globally, so long as an instance of the class where the public members are declared is created.
- These members are accessible only thru an instance of the class where they are declared
- Generally used to define a C++ class behavior and/or to access private data members (act as private data modifiers)
- Can be data or method members
- Act exactly as private members for all practical purposes, so long as they are referenced from within the class (and/or instances of the class)where they are declared
- Specifically used to define how certain data/method members of a class would behave in a child class (used to define their behavior in inheritance)
- The protected members become private of a child class in case of private inheritance, public in case of public inheritance, and stay protected in case of protected inheritance
A virtual function allows derived classes to replace the implementation provided by the base class.
A virtual function is something which helps a derived class in overriding the implementation of a functionality of a base class.
No comments:
Post a Comment