Explain the following in detail, in context of C++ programming

Access specifiers Access specifiers are used to control the accessibility of data members and member functions of class. It helps classes to prevent unwanted exposure of members (data and functions) to outside world.

Virtual Function – You know that polymorphism also refers to the ability to call different functions by using only one type of function call.

Suppose a programmer wants to code vehicles of different shapes such as circles, squares, rectangles, etc. one way to define each of these classes is to have a member function for each that makes vehicles of each shape.

Another convenient approach the programmer can take is to define a base class named Shape and then create an instance of that class.

The programmer can have array that hold pointers to all different objects of the vehicle followed by a simple loop structure to make the vehicle, as per the shape desired, by inserting pointers into the defined array.

This approach leads to different functions executed by the same function call. Polymorphism is used to give different meanings to the same concept. This is the basis for virtual function implementation.

You may also like...

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

error: Content is protected !!