How to implement polymorphism

v-table,

how to implement v-table?

tail

Write a program to display last 'n' lines of an arbitrary length of
file (like tail Unix program).

What is a mutable member?

One that can be modified by the class even when the object of the class or the member function doing the modification is const.

Understanding this requirement implies an understanding of C++ const, which many programmers do not have. I have seen large class designs that do not employ the const qualifier anywhere. Some of those designs are my own early C++ efforts. One author suggests that some programmers find const to be such a bother that it is easier to ignore const than to try to use it meaningfully. No wonder many programmers don't understand the power and implications of const. Someone who claims to have enough interest in the language and its evolution to keep pace with the ANSI deliberations should not be ignorant of const, however.

What is RTTI?

Runtime type identification. The ability to determine at run time the type of an object by using the typeid operator or the dynamic_cast operator.

What is inheritance?

Inheritance is a way to let new classes reuse attributes and behavior of pre-existing classes.

What is abstraction?

It is the act of focusing on the essential features while forgetting specific details for a while.

What is the difference between operator new and the new operator?

This is what happens when you create a new object:
  1. the memory for the object is allocated
  2. the costructor of the class is invoked to properly initialize this memory.
The new operator does both 1 and 2. The operator new only does 1.

What is the difference between C and C++ struct?

  1. C++ structs are like C++ class except the default access for struct is public v/s class is private
  2. C struct can have integral data type only unlike C++ struct that can also have member functions
  3. C struct cannot have access scope like public, private, protected, but C++ struct can.
  4. struct A{ int i, int j } ; typedef A A1; typedef needed for C struct and not required for C++ , A can be directly accessed.

What is polymorphism?

Polymorphism allows a single definition to be used with different types of data.

compile time(function overloading) vs runtime(function overriding)
polymorphic function vs polymorphic data.

What are the different types of Storage classes?

auto, static, register, extern

What is abstract class?

It is a class having at least one pure virtual method.

What is the difference between class and structure?

Members of a structure are public by default, while members of a class are private by default.

What is the difference between macro and inline()?

Inline functions are expanded by compiler, macros are expanded by the preprocessor. In some sense, inline functions are type safe macros.

What is virtual function?

It is a function whose implementation is defined in the deepest subclass.

Why does a copy constructor accept a const reference as parameter?

Because otherwise an infinite call chain would be created. i.e., a copy constructor has to call itself to make a copy of the argument.

What is the difference between "overloading" and "overriding"?

overridding is runtime polymorphism while overloading is compile time polymorphism.

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.

Can we have virtual constructors?

No. Because the type of an object is always known when it is created.

What is the difference between assignment operator and copy constructor?

Copy constructor is a constructor — a function whose job it is to turn raw storage into an object of a specific class. An assignment operator, on the other hand, copies state between two existing objects. In other words, an assignment operator has to take into account the current state of the object when copying the other object’s state into it. The copy constructor is creating a new object from raw storage and knows it’s writing over garbage.

What is virtual destructor?

Virtual destructor allows one to properly destroy an object without knowing its type.

Can destructor be private?

Yes. Private destructor is useful for creating final class by preventing users to inherit from this class.

What is copy constructor?

A copy constructor is a special constructor used to create a new object as a copy of an existing object. This constructor takes a single argument: a reference to the object to be copied.

A copy constructor is generally needed when an object owns pointers or non-shareable references such as to a file and then you usually need a destructor and assignment operator too.

What is difference between copy constructor and constructor?

Constructor is called when an object is created. Copy constructor is called when the copy of an object is made. For e.g. passing parameter to function by value, function returning by value. Copy constructor takes the parameter as const reference to the object.

How to create an un-inheritable class?

How to create an object such that it should not call constructor by default.

Is it possible for a class to have only private constructors?

When do you want virtual destructors?

Why constructors can't be virtual?

What is the importance of const. pointer in copy constructor?

What is public, protected, private?

What is the difference between class and structure?

What is the difference between macro and inline()?