Tuesday, 6 March 2012

C++ Q&A


C++ questions


I . What is public, protected, and private?

Public, protected and private are three accesses specifies in C++.
Public data members and member functions are accessible outside the class.
Private Accessible ONLY by the member methods of the class where they are declared, only exception to the above rule is Friend. 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
Act exactly as a 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, 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.


2. a What is friend function?  


o A friend function is a non member function of a class that is declared as a friend using the keyword "friend" inside the class. By declaring a function as a friend, all the access permissions are given to the function.

b.What is abstraction?
o Abstraction refers to the act of representing essential features without including the background details.                     [].


3 .What do you mean by binding of data and functions?
o Encapsulation is the grouping together of data and functionality. C++ implements encapsulation by allowing all members of a class to be declared as either public, private, or protected. A public member of the class will be accessible to any function. A private member will only be accessible to functions that are members of that class and to functions and classes explicitly granted access permission by the class ("friends"). A protected member will be accessible to members of classes that inherit from the class in addition to the class itself and any friends.

4.Difference between realloc() and free()?
o Realloc () function used to expands the existing allocated memory ,if it finds enough continues memory location that are required, if enough continues memory are not available new memory of block is allocated. The existing data is copied and the original memory block is freed. for example::
int *p=new int[5];
p[1]=3;
p=(int*) realloc (p,sizeof(int) *5);
free() deallocates memory allocated by maaloc, calloc or realloc. Whenever there is a need to allocate extra memory we can use realloc.
realloc means free+malloc once more.

5.What is function overloading and operator overloading?
o Function overloading: C++ enables several functions of the same name to be defined, as long as these functions have different sets of parameters (at least as far as their types are concerned). This capability is called function overloading. When an overloaded function is called, the C++ compiler selects the proper function by examining the number, types and order of the arguments in the call. Function overloading is commonly used to create several functions of the same name that perform similar tasks but on different data types.
o Operator overloading allows existing C++ operators to be redefined so that they work on objects of user-defined classes. Overloaded operators are syntactic sugar for equivalent function calls. They form a pleasant facade that doesn't add anything fundamental to the language (but they can improve understandability and reduce maintenance costs).

6.What is virtual class and friend class?
o So far we have learnt that only member function can access private data of a class. This is the essence of data encapsulation. But sometime we have to allow function outside the class can access and manipulate the class’s data member. To archive this c++ provides a keyword Friend. This allows a function or all functions of another class to read and write the original class’s private data member.
If we made entire class friend then all member function of the class would become friend.
Suppose you have two derived classes B and C that have a common base class A, and you also have another class D that inherits from B and C. You can declare the base class A as virtual to ensure that B and C share the same sub object of A.
In the following example, an object of class D has two distinct sub objects of class L, one through class B1 and another through class B2. You can use the keyword virtual in front of the base class specifies in the base lists of classes B1 and B2 to indicate that only one sub object of type L, shared by class B1 and class B2, exists.
For example:


7. What do you mean by inline function?

Inline Functions are like Normal functions. When an inline Function is invoked the code of function is inserted instead of jump to code of function. Inline keyword is added to the function definition to make that particular function inline function. Inline keyword is just a request to the compiler. Sometimes Compiler will ignore the request and compile the function as normal function.

8. What is a template?  
o Template is a mechanism that makes it to possible to use one function or class to handle many different data types. By using template , we can design a single class/function that operates on data of many type, instead of having to create a separate class/function for each type.

What is RTTI?

9.What is namespace?
Namespaces allow us to group a set of global classes, objects and/or functions under a name. To say it somehow, they serve to split the global scope in sub-scopes known as namespaces.
The form to use namespaces is:
namespace identifier {   namespace-body }
Where identifier is any valid identifier and namespace-body is the set of classes, objects and functions that are included within the namespace. For example:
namespace general
{
  int a, b;
}
In this case, a and b are normal variables integrated within the general namespace. In order to access to these variables from outside the namespace we have to use the scope operator ::. For example, to access the previous variables we would have to put:
general::a  general::b
The functionality of namespaces is specially useful in case that there is a possibility that a global object or function can have the same name than another one, causing a redefinition error.


10.What do you mean by pure virtual functions?
o pure  virtual function is a virtual function, in base class it has no definition, derived class has to provide its definition.
Pure virtual functions are equated to zero.
class Shape {
public:
  virtual void draw() = 0;
};

11.What is polymorphism? Explain with an example?
o Generally, the ability to appear in many forms. In object-oriented programming, polymorphism refers to a programming language's ability to process objects differently depending on their data type or class. More specifically, it is the ability to redefine methods for derived classes. For example, given a base class shape, polymorphism enables the programmer to define different area methods for any number of derived classes, such as circles, rectangles and triangles. No matter what shape an object is, applying the area method to it will return the correct results. Polymorphism is considered to be a requirement of any true object-oriented programming language

12. What is a class?
o class is a user defined datatype which consists of attributes(data members) and behavior(member functions). By default the members of a class is private.
What is the difference between structures and classes in C++?
o There is only one difference between struct & class in 'C++'.  All the variables/functions in struct are 'public' and in class they are 'private' by default.

13.What is an object?
o An Object is an instance of a class. For Ex: ferrari and maruthi are  objetcs of class car.
we can overload assignment operator as a normal function. But we can not overload assignment operator as friend function why?

14.What are virtual functions?
o C++ virtual function is a member function of a class, whose functionality can be over-ridden in its derived classes.
C++ virtual function is,
A member function of a class
Declared with virtual keyword
Usually has a different functionality in the derived class
A function call is resolved at run-time
What is the Basic nature of "cin" and "cout" and what concept or principle we are using on those two?
o Basically "cin and cout" are INSTANCES of istream and ostream classes respectively.And the concept which is used on cin and cout is operator overloading. Extraction and Insertion operators are overloaded for input and ouput operations.

15.What is a scope resolution operator?
o A scope resolution operator (::), can be used to define the member functions of a class outside the class.
o It resolves a dispute in scope when local and global variable exist with the same name.

16.What do you mean by inheritance?
o Creating or deriving a new class using another class as a base is called inheritance in C++. The new class created is called a Derived class and the old class used as a base is called a Base class in C++ inheritance terminology.
o The derived class will inherit all the features of the base class in C++ inheritance. The derived class can also add its own features, data etc., It can also override some of the features (functions) of the base class, if the function is declared as virtual in base class.
o C++ inheritance is very similar to a parent-child relationship. When a class is inherited all the functions and data member are inherited, although not all of them will be accessible by the member functions of the derived class. But there are some exceptions to it too.
o Some of the exceptions to be noted in C++ inheritance are as follows
§ The constructor and destructor of a base class are not inherited
§ the assignment operator is not inherited
§ the friend functions and friend classes of the base class are also not inherited.
There are some points to be remembered about C++ inheritance. The protected and public variables or members of the base class are all accessible in the derived class. But a private member variable not accessible by a derived class.
It is a well known fact that the private and protected members are not accessible outside the class. But a derived class is given access to protected members of the base class.

17.What is the difference between an object and a class?
o A Class is static. All of the attributes of a class are fixed before, during, and after the execution of a program. The attributes of a class don't change.
o The class to which an object belongs is also (usually) static. If a particular object belongs to a certain class at the time that it is created then it almost certainly will still belong to that class right up until the time that it is destroyed.
o An Object on the other hand has a limited lifespan. Objects are created and eventually destroyed. Also during that lifetime, the attributes of the object may undergo significant change.

17.What is encapsulation?
o Encapsulation is the grouping together of data and functionality. C++ implements encapsulation by allowing all members of a class to be declared as either public, private, or protected. A public member of the class will be accessible to any function. A private member will only be accessible to functions that are members of that class and to functions and classes explicitly granted access permission by the class ("friends"). A protected member will be accessible to members of classes that inherit from the class in addition to the class itself and any friends.


18.What is difference between constructor and destructor?
o constructor is the memeber function of the class which has the same name as that of class and it is invoked whenever the class object is instantiated.
o destructor is also the member function  of same class name and has ~ operator when ever declared in the function and it is used to destruct the object which has been constructed ,whenever we want to destroy it.

19.Can you explain the term "resource acquisition is initialization? (RAII)
o Resource Acquisition is Initialisation or shortly RAII is a term closely connected with exception handling and automatic garbage collection(speaking loosely). RAII is defined as the technique of performing all necessary resource initialisation work right within the body of the constructor itself, and correspondingly doing all the deallocation within the destructor. Since during exception handling destructors of all the classes whose constructors have been successfully invoked, are called therefore all the local objects previously allocated are automatically deallocated without explicitly calling their destructors.the following example explains the RAII technique:class X{ int *r; public: X(){cout<<"X is created"; r=new int[10]; } ~X(){cout<<"X is destroyed"; delete [] r; }};class Y{ public: Y(){ X x; throw 44; } ~Y(){cout<<"Y is destroyed";}};Now since Y throws an exception right in its constructor therefore its destructor won't get called becuse the constructor was not invoked successfully. But the destructor of the class X will definitely be called and therefore the array 'r' will be deallocated. So we didn't have to care about resource deallocation at the time exceptions are raised.
What will happen if I say delete this?
Difference between "C structure" and "C++ structure".
o C structures contain only data members where as c++ structure contains both data members and member functions as well.
What is the difference between "overloading" and "overriding"?
o Overloading a method (or function) in C++ is the ability for functions of the same name to be defined as long as these methods have different signatures (different set of parameters). Method overriding is the ability of the inherited class rewriting the virtual method of the base class.
o Overriding is runtime polymorphism while overloading is compile time polymorphism.

20 .What are the different types of polymorphism?
o There are two types of polymorphism
1st static
2nd Dynamic
Static is nothing but compile time polymorphism, and dynamic is nothing but runtime polymorphism.
Static:
· Function overloading
· Operator overloading
Dynamic:
Virtual function

21.What are the different types of Storage classes?
o Storage class defined for a variable determines the accessibility that is what portion of the program could have access to the variable and also the longevity of the variable that is how long the variable would exist.
o There are four types of storage class: automatic, register, external, and static.

No comments:

Post a Comment

Write your openion about my blog spot..To get automatic facebook updates like my Pagehttps://www.facebook.com/shivashankar4u ..It takes only 1 min to write the comment and to like the page.. Thanks.