--------------- polymorphism ----------------------
poly -> many
morphism -> forms
with same function name different implementation
eg;
draw() -> can draw circle, square, rectangle
types:
compile time -> method overloading
runtime -> method overriding
----------------- method overloading ----------------
defining a more than one function with same name
how its possible?
by the number and/or type of arguments passed is different.
int test() { }
int test(int a) { }
float test(double a) { }
int test(int a, double b) { }
Overloaded functions may or may not have different return types but they must have
different arguments. For example,
// Error code
int test(int a) { }
double test(int b){ }
/* Online C++ Compiler and Editor */
#include <iostream.h>
#include<conio.h>
class Addition{
public:
void add(int a){
a=a+1;
cout<<"\na="<<a;
}
void add(int a,int b){
int c= a+b;
cout<<"\nc="<<c;
}
void add(float a,float b,float c){
float d= a+b+c;
cout<<"\nd="<<d;
}
};
void main()
{
Addition a1;
clrscr();
a1.add(10);
a1.add(11,22);
a1.add(1.0,1.0,1.0);
getch();
}
a=11
c=33
d=3
----------------------- Methode overriding --------------------
The same function is defined in both the derived class and the based class.
It overrides the base class methode and executes subclass methode
base class methode -> overridden function
sub class methode -> override function
#include <iostream.h>
#include<conio.h>
class Base {
public:
void print() {
cout << "Base Function" << endl;
}
};
class Derived : public Base {
public:
void print() {
cout << "Derived Function" << endl;
}
};
void main() {
Derived derived1;
clrscr();
derived1.print();
getch();
}
o/p:
Derived Function
===> Access Overridden Function in main() outside class
scope resolution operator ::
#include <iostream.h>
#include<conio.h>
class Base {
public:
void print() {
cout << "Base Function" << endl;
}
};
class Derived : public Base {
public:
void print() {
cout << "Derived Function" << endl;
}
};
void main() {
Derived derived1, derived2;
clrscr();
derived1.print();
derived2.Base::print();
getch();
}
o/p:
Derived Function
Base Function
===> Access Overridden Function in subclass overriding methode
#include <iostream.h>
#include<conio.h>
class Base {
public:
void print() {
cout << "Base Function" << endl;
}
};
class Derived : public Base {
public:
void print() {
Base::print();
cout << "Derived Function" << endl;
}
};
void main() {
Derived derived1;
clrscr();
derived1.print();
getch();
}
----------------- task --------------------
methode overloading
1.Write c ++ program to overload a area() of shapes
#include <iostream.h>
#include<conio.h>
// function with float type parameter
float absolute(float var){
if (var < 0.0)
var = -var;
return var;
}
// function with int type parameter
int absolute(int var) {
if (var < 0)
var = -var;
return var;
}
int main() {
// call function with int type parameter
cout << "Absolute value of -5 = " << absolute(-5) << endl;
// call function with float type parameter
cout << "Absolute value of 5.5 = " << absolute(5.5f) << endl;
return 0;
}
-------------- overridding ---------------