Operator having many forms – Computer Sir Ki Class


Lost your password?

Don't have an account ?
Register (It's FREE) ×


Code Learning #CPP#4643    siteicon   siteicon  

Operator having many forms

Demonstration of a kind of polymorphism with + operator.

Learning Objectives

  • Understanding how operators can also show polymorphic behaviour.

Source Code

TC++ #4643

Source Code

Run Output


Code Understanding

int a=5,b=7;  int c=a+b; cout<<c<<endl;
Here + (addition operator) is being used for adding two integers

string x=”poly”; string y=”morphism”; string z=x+y; cout<<z<<endl;
Here + (addition operator) is being used for concatenating two string, second one after the first string.

In both cases + acts as a binary operator, but operands are acted differently when their data types are different.


  • This code can not be demonstrated in Turbo C++ as you don’t have string datatype there
  • Many operators are already overloaded by default in C++ library, but programmers can also use there own code to make their own overloading action to overloaded operators.
  • Since operator overloading is a type of compile time overloading only, so purists consider this also a type of adhoc polymorphism. Read More..

Common Errors

  • The + operation doesn’t work for c-style (character array with null termination) strings. There one is required to use strcat() function.

Suggested Filename(s): plus-ol.cpp,plus-overloading.cpp


sunmitra| Created: 6-Sep-2018 | Updated: 6-Sep-2018|

Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning 2D Array Memory Addressing Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Multi-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues