Definitions Of Object: An object is combination and collection of data and code designed to emulate a physical abstract enmity.You can create number object of class. The properties, Variable and Methods define in class are called Members of class.
- Private Member Of Class: Private member of a class have strict access control Only the member function of same class can access this members.
- Protected Member of Class: A Protected member is accessible to member of its own calls and to any of the class member in a derived class.
- Public Member Of Class: A public member of class can be accessible from any where.
- Public and Private Static Member Of a Class: The static member of a class can be access without creating a object of class. While to access other member of class you have to create a object of class. The public static member can be accessed using access specifier while private static member can be accessed only the member functions.
Inheritance and Member Accessibility:
- A private member is accessible only to members of the class in which private member is declared. They cannot be inherited
- A private member of the base class can be accessed in the derived class through the member of the base class.
- A protected member is accessible by member of its own class and to any of the class member in a derived class.
Polymorphism
Polymorphism is the ability to use an operator or function in different ways. Polymorphism gives different meanings or functions to the operators or functions. Poly, referring to many, signifies the many uses of these operators and functions. A single function usage or an operator functioning in many ways can be called polymorphism. Polymorphism refers to codes, operations or objects that behave differently in different contexts.
Below is a simple example of the above concept of polymorphism:
|
The above refers to integer addition.
The same + operator can be used with different meanings with strings:
|
The same + operator can also be used for floating point addition:
|
Polymorphism is a powerful feature of the object oriented programming language C++. A single operator + behaves differently in different contexts such as integer, float or strings referring the concept of polymorphism. The above concept leads to operator overloading. The concept of overloading is also a branch of polymorphism. When the exiting operator or function operates on new data type it is overloaded. This feature of polymorphism leads to the concept of virtual methods.
Polymorphism 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.