From fb8d172e12f9a5aa1001b439b0ab83c33b4dde5b Mon Sep 17 00:00:00 2001 From: askgurdit Date: Sun, 14 Apr 2024 22:41:04 +0530 Subject: [PATCH] polymorphism --- C++ PROGAMMING/OOPS/polymorphism.cpp | 37 ++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 C++ PROGAMMING/OOPS/polymorphism.cpp diff --git a/C++ PROGAMMING/OOPS/polymorphism.cpp b/C++ PROGAMMING/OOPS/polymorphism.cpp new file mode 100644 index 0000000..7729f18 --- /dev/null +++ b/C++ PROGAMMING/OOPS/polymorphism.cpp @@ -0,0 +1,37 @@ +/*Ability of objects/methods to take different forms +Two types---1)Run time and 2)Compile time */ + +// Compile time polymorphism----two types-----Function overloading and operator overloading----- + +/* FUNCTION OVERLOADING +Define a number of functions with same function name they perform differently according to the arguments passed +Arguments---how many and types +We can define 1 function which can act differently according to the parameters*/ + +#include +using namespace std; + +class Sum { +public: + void add(int x, int y) { + int sum = x + y; + cout << sum << endl; + } + void add(int x, int y, int z) { + int sum = x + y + z; + cout << sum << endl; + } + void add(float x, float y) { + float sum = x + y; + cout << sum << endl; + } +}; + +int main() { + Sum s; + s.add(2, 3); + s.add(2, 3, 4); + s.add(float(2.3), float(2.7)); + + return 0; +} \ No newline at end of file