Thursday 1 December 2011

C++ Programing Questions And Answers-6


Q. Can you overload a function based only on whether a parameter is a value or a reference?
Ans. No. Passing by value and by reference looks identical to the caller.
Q. 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.
Q. What does extern "C" int func(int *, Foo) accomplish?
ANS. It will turn off "name mangling" for func so that one can link to code compiled by a C compiler.
Q. How do you access the static member of a class?
Ans. <ClassName>::<StaticMemberName>
Q. 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 method with the same name.
Q. What are the access privileges in C++? What is the default access level?
The access privileges 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.
Q. What is a nested class? Why can it be useful?
A nested class is a class enclosed within the scope of another class. For example:
//  Example 1: Nested class
//
class OuterClass
{
class NestedClass
{
// ...
};
// ...
};
Nested classes are useful for organizing code and controlling access and dependencies. Nested classes obey access rules just like other parts of a class do; so, in Example 1, if NestedClass is public then any code can name it as OuterClass::NestedClass. Often nested classes contain private implementation details, and are therefore made private; in Example 1, if NestedClass is private, then only OuterClass's members and friends can use NestedClass.
When you instantiate as outer class, it won't instantiate inside class.
Q. What is a local class? Why can it be useful?
Ans.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.
Q. Can a copy constructor accept an object of the same class as parameter, instead of reference of the object?
Ans. No. It is specified in the definition of the copy constructor itself. It should generate an error if a programmer specifies a copy constructor with a first argument that is an object and not a reference.

No comments:

Post a Comment