POLYMORPHISM - Object-Oriented Programming (OO……
POLYMORPHISM – Object-Oriented Programming (OOP)
Polymorphism என்ற வார்த்தை: Poly = Many Morph = Forms அதனால்: Polymorphism means many forms. Programming-ல்: ஒரே function name அல்லது interface different situations-ல் different behavior காட்டுவது Polymorphism. தமிழில்: ஒரே பெயர் அல்லது ஒர…
Polymorphism என்ற வார்த்தை: Poly = Many Morph = Forms அதனால்: Polymorphism means many forms. Programming-ல்: ……
ஒரு simple example: sound() Animal class-ல் எல்லா animals-க்கும் sound() என்ற operation இர……
ஒரே person different situations-ல் different roles: At Home → Father At College → Tea……
One Function / Interface ↓ Different In……
area() | |-- Circle | → πr² | |……
C++-ல் polymorphism இரண்டு மு……
1. Compile-Time Polymorphism ……
இதையே: Static Polymorphism Dy……
COMPILE-TIME POLYMORPHISM
Compile time-லேயே எந்த function execute ஆக வேண்டும் என்பதை com……
1. Function Overloading 2. Op……
Program run ஆகும் நேரத்தில் எந்த function execute ஆக வேண்டும் என்பதை determine செய்யும். இதனை: Dyna……
Same function name, but diffe……
void add(int a, int b) and: void add(int a, int b……
int add(int a, int b) { return a + b……
return 0; }…
First function: int add(int a, int b) 2 arguments பெறுகிறது. Second: int add(int a, int b, int c) 3 arguments……
C++ operators-க்கு user-defined behavior கொடுக்கலாம். +……
- integers add செய்கிறது.…
But class objects-க்கும்: obj……
int value;…
Number operator+(Number n) { ……
Number n3 = n1 + n2;…
return 0; }…
இங்கே: + operator user-defined objects-க்……
Runtime polymorphism புரிய முதலில்: Inheri……
Base class மற்றும் derived class-ல் same functi……
இங்கே Dog தனது sound() implementation கொடுக்கிறது. இதனை: Function Overriding என்று அழைக்கிறோம். BUT IMPORTANT……
Virtual function syntax: virtual void sound() { } virtual keyword: Base class ……
Dog d;…
return 0; }…
Base class: class Animal contains: virtual void sound() This is virtual function. Derived class: class Dog : ……
Animal *ptr ↓ ptr points to Dog object ↓ ptr->sound() ↓ so……
WITHOUT VIRTUAL KEYWORD
Then: Animal *ptr; Dog d;…
Output generally: Animal Why? Because base function virtual இல்லை. Bin……
So: Without virtual → Base fu……
Compile-time function selection: Static Binding……
Runtime-ல் function selection……
Actual object type பார்த்து function select செய்யப்படுகிறது. EARLY BINDING vs LATE BINDING Early Binding = Co……
virtual void area() = 0; }; D……
Base pointer: Shape *ptr; can……
virtual void draw() = 0; };…
Shape *ptr;…
return 0; }…
Same pointer: Shape *ptr Different objects: Circle Rectangle Same funct……
முக்கிய concept: A base-class pointe……
This relationship runtime polymorphism-க்கு foundation. BASE REFERENCE ALSO WOR……
Modern C++-ல்: void sound() o……
override compiler-க்கு: "இந்த function base virtual function-……
ஒரு base class polymorphically பயன்படுத……
Why? Suppose: Base *ptr = new Derived; delete ptr; Base destructor virtual இல்லையெ……
Pure virtual function: virtual void display() = 0; இதுவும் virtual func……
Payment Example Common function: pay() Different payment methods: UPI → UPI payment logic Credit Card → Card ……
Flexibility
Same interface different obje……
Reusability
Common base code reusable.…
Extensibility
New derived class add செய்தாலும்……
Maintainability
Complex if-else logic குறைக்க……
Loose Coupling
Code specific derived class மீது heavily depend ஆகாமல் base interface மீது depend செய்யலாம். WITHOUT POLYMORP……
SIMPLE MEMORY Overloading → Same Name → Different Parameters → Compile Time Overriding → Same Signature → Par……
[POLYMORPHISM vs ENCAPSULATION] Encapsulation → Wrap data and methods Inher……
Polymorphism means many forms. Main idea: One Interface, Many Forms. Two major types: Compile-Time Run-Time C……
Confusing Overloading and Overriding Overloading: void show(int x); void show(double x); Same c……
Thinking Return Type Alone Can Overl……
Forgetting virtual If base function is not virtual: Base *p ……
Signature Mismatch Base: virtual void show(int x); Derived: v……
Base Pointer Cannot Access De……
Then: Animal *ptr = &dog; You……
because bark() is not part of……
POLY = Many MORPH = Forms So: Polymorphism = Many Forms Best memory line: One Name - Many Behavio……
What is polymorphism? Answer: Abilit……
Meaning of polymorphism? Answ……
Types of polymorphism in C++? Ans……
Function overloading belongs ……
Operator overloading belongs ……
Function overriding with virtual……
Another name for compile-time……
Another name for runtime bind……
Which keyword enables runtime……
Can return type alone differe……
What is polymorphism? Polymorphism is the ability to use a common interface for different underlying forms or……
Polymorphism means:…
Function overloading is:…
Operator overloading is:…
Function overriding is mainly……
Which keyword is used for dyn……
Static binding occurs at:…
Dynamic binding occurs at:…
Which is another name for dyn……
Which is required for standar……
Function overloading requires:…
Which is true?…
"One interface, many forms" d……
Consider: class Animal { publ……
Polymorphism Type = Run-Time ……
POLYMORPHISM | |-- Means Many Forms | |-- One Interface | Many Behaviors | |-- Compile-Time | | | |……
Polymorphism என்பது ஒரே function name அல்லது common interface different objects அல்லது different inputs-க்கு ……