Monday, May 12, 2014

C++

Repeatedly asking C++ Interview Questions

0. pure virtual functions have no body (the function must be implemented in child classes)

virtual functions have a body (the function may be reimplemented in child classes, but it isn't required).

Any class that has 1 or more pure virtual functions are "abstract". Abstract objects cannot be instantiated -- you can only instantiate its child classes.


1
.What is public, protected, private?
Ans:
public, protected, private are access specifiers that is used to implement encapsulation of data at various level.

Private:

* Can be data or method members
* Are private to the class where they are declared
* 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

Public:

* 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 behaviour and/or to access private data members (act as private data modifiers)

Protected

* 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 behaviour 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.

Private VS Protected


Should you ever use protected member variables?
Depends on how picky you are about hiding state.
  • If you don't want any leaking of internal state, then declaring all your member variables private is the way to go.
  • If you don't really care that subclasses can access internal state, then protected is good enough.
If a developer comes along and subclasses your class they may mess it up because they don't understand it fully. With private members, other than the public interface, they can't see the implementation specific details of how things are being done which gives you the flexibility of changing it later.
Private members are only accessible within the class defining them.
Protected members are accessible in the class that defines them and in classes that inherit from that class.
Edit: Both are also accessible by friends of their class, and in the case of protected members, by friends of their derived classes.
Edit 2: Use whatever makes sense in the context of your problem. You should try to make members private whenever you can to reduce coupling and protect the implementation of the base class, but if that's not possible then use protected members.


  1. Protected and public members(data and function) of a base class are accessible from a derived class(for all three: public, protected and private inheritance).
  2. Objects of derived class with private and protected inheritance cannot access any data member of a base class.
  3. Objects of derived class with public inheritance can access only public member of a base class.
Summary of accessibility of members defined in base classes

Accessibility in Public Inheritance

Accessibilityprivateprotectedpublic
Accessible from own class?yesyesyes
Accessible from derived class?noyesyes
Accessible outside derived class?nonoyes

Accessibility in Protected Inheritance

Accessibilityprivateprotectedpublic
Accessible from own class?yesyesyes
Accessible from derived class?noyesyes
Accessible outside derived class?nonono

(protected and public members in base class will become protected in subclass)


Accessibility in Private Inheritance

Accessibilityprivateprotectedpublic
Accessible from own class?yesyesyes
Accessible from derived class?noyesyes
Accessible outside derived class?nonono

(protected and public members in base class will become private in subclass and cannot be seen by sub-subclass)


2. What is a class?

Ans:

class is a user defined data type,in which data members and member functions are defined.A class can also be defined as a classification/category of objects that have similar
attributes and behaviour.For example, Automobile is a category of objects that have similar attributes, such as wheels, engine, doors, shape, color, cylinders etc., and behaviours,
such as start, run, move, turn etc. Car is an instance of automobile which has different values for the attributes (4 wheels, one engine, 2 or 4 doors, 4/6/8 cylinders, etc),

3.What is an object?

Ans:

In C++, Object is an instance of a Class that has a runtime state, and is associated with certain specific methods that can change its state.

4.Difference between realloc() and free()?

Ans:

Realloc() is used to reallocate the memory for variable.Realloc()used to resize the memory held by the pointer to the number of bytes specificed.If the new
size is larger than current size, new memory is allocated. If it is less, the remaining (additional) bytes are released to general OS/application consumption.

Free() is used to free the allocated memory of a variable.

5. What is function overloading and operator overloading?

Ans:

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
For example, void Function_Test();
void Function_Test(int); // Overloaded
void Function_Test(int, int); // Overloaded

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?

Ans:

Virtual Base Class: Used in context of multiple inheritance in C++. If you plan to derive two classes from a class, and further derive one class from the two classes in the second level, you
need to declare the uppermost base class as 'virtual' in the inherited classes. This prevents multiple copies of the uppermost base class data members when an object of the class at the
third level of hierarchy is created.

Friend class: When a class declares another class as its friend, it is giving complete access to all its data and methods including private and protected data and methods to the friend
class member methods.
Friendship is not bi-directional. If A declares B as its friend it does NOT mean that A can access private data of B. It only means that B can access all data of A.

7. What is abstraction?

Ans:
Abstraction refers to the act of representing essential features without including the background details.That means abstraction is separating the logical properties from implementation details.
For example driving the car is a logical property and design of the engine is the implementation detail.

8. What do you mean by inline function?

Ans:
The idea behind inline functions is to insert the code of a called function at the point where the function is called. If done carefully, this can improve the application's performance in exchange
for increased compile time and possibly (but not always) an increase in the size of the generated binary executables.When an inline Function is invoked the code of function is inserted instead of
jump to code of function.

9. What do you mean by pure virtual functions?

Ans:
A pure virtual member function is a member function that the base class forces derived classes to provide. Normally these member functions have no implementation. Pure virtual functions are equated to zero.

class Shape {

public:

virtual void draw() = 0;
};

10. What is virtual constructors/destructors? 

Ans:
Virtual constructor: Constructors cannot be virtual. Declaring a constructor as a virtual function is a syntax error.

Virtual destructors: If an object (with a non-virtual destructor) is destroyed explicitly by applying the delete operator to a base-class pointer to the object,
the base-class destructor function (matching the pointer type) is called on the object.

11. What is a scope resolution operator?

Ans:
A scope resolution operator (::) can be used to define the member functions of a class outside the class.Most generally a scope resolution operator is required when a data member is redefined by a derived class,
or an overriden method of the derived class wants to call the base class version of the same method.

12. what is difference between constructor and destructor?

Ans:
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.Using construtor we can allocate memory.

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..



std::vector and most other standard library containers store elements by value. The elements are copied on insertion or when the container is copied. std::string also maintains its own copy of the data, as far as your usage of it is concerned.

No comments:

Post a Comment