C++ Notes

Spread the word!

How to initialize private member of a class?

1) Constructor’s implementation inside the class:

2) Constructor’s declaration inside the class:

And its implementation outside:

How to declare and define a static constant member as array of integers?

It should be declared inside the class:

and defined outside in this way:

How to set default values inside arrays?

The following function from can be used:

Note: in order to call an overloaded constructor from the standard constructor the following implementation’s example is wrong:

instead, the following is correct:

If you want to change object’s properties (e.g.):

How to overload prefix or postfix operator ++ ?

While using an overloaded operator the compiler gave error with:

But not with:

What is an abstract class?

Sometimes implementation of all function cannot be provided in a base class because we don’t know the implementation. Such a class is called abstract class. For example, Animal class doesn’t have implementation of move() (assuming that all animals move), but all animals must know how to move. We cannot create objects of abstract classes.

A pure virtual function (or abstract function) in C++ is a virtual function for which we don’t have implementation, we only declare it. A pure virtual function is declared by assigning 0 in declaration. See the following example.

Virtual function vs. Pure Virtual function

A virtual function makes its class a polymorphic base class. Derived classes can override virtual functions. Virtual functions called through base class pointers/references will be resolved at run-time. That is, the dynamic type of the object is used instead of its static type:

A pure virtual function implicitly makes the class where it is defined an abstract class (unlike in Java where you have a keyword to explicitly declare the class abstract). Abstract classes cannot be instantiated. Derived classes need to override/implement all inherited pure virtual functions. If they do not, they too will become abstract.

An inherited class can only initialize the constructor of its base class, and not constructors of any superclass from which it come from!

Default parameter‘s values are to be written only at the method‘s declaration but not in the later implementation!

During a method‘s declaration, all parameter with default value must be written after all parameter without default value.

Be the first to comment

Leave a Reply

Your email address will not be published.