Lecture 3 Polymorphism
Lecture 3 Polymorphism
Polymorphism
Poly = many
Morphism = shape, form or structure
Polymorphism
Constructor Destructor X
overloading Why?
2
Constructor Overloading in C++
Example for supporting array
#include <iostream> OUTPUT:
using namespace std; 01
➢It is common to overload a class’s constructor function. 02
class myclass { 03
➢It is not possible to overload a destructor function. int x; 04
public:
➢Three main reasons to overload constructor function: myclass() { x = 0;}
05
MyClass(){ x = 0;}
MyClass(int n){ x = n;} [0] [1] [2] [3] [4]
public int getX(){ return x;}
} 1 2 3 4 5
7
Default Argument
➢Default argument can be used instead of function overload
#include <iostream>
#include <iostream> using namespace std;
OUTPUT:
10
using namespace std;
class myclass { 0
double rect_area( double length, double width = 0){ int x;
public:
if (!width) width = length;
myclass(int n = 0) { x = n; }
return length*width; int getx() { return x; }
} };
C++ Code
Rectangle(double a, double b) { super(a, b);} return 0;
double area(){ return dim1*dim2;} }
void show(){cout << "Rectangle Area: ” << area();}
}
Java Code
}
} Triangle t = new Triangle(4, 3);
Figure figref;
class Rectangle extends Figure {
Rectangle(double a, double b) { super(a, b);} figref = r;
double area(){ return dim1*dim2;} figref.show();
void show(){ figref = t;
System.out.println("Rectangle Area: "+area()); figref.show();
} }
} }
➢ Except for the =, operator functions are inherited by any derived class.
14
Unary Operator Overloading in C++
When Object Not Changed:
Unary Operator
No Change of Object (-ob)
Object not changed Coord Coord:: operator +(){
e.g., -c
Object changed Coord temp;
temp.x = -x; Member of
temp.y = -y; Object
Prefix Postfix return temp;
e.g., ++a, --b e.g., a++, b-- }
20
Returning Reference for Operator Overloading in C++
#include <iostream> Coord &Coord:: operator += (Coord &ob){
using namespace std; x += ob.x;
y += ob.y;
class Coord{ return *this;
int x, y; }
public:
Coord(int a=0, int b=0){ x = a; y = b;} ostream &operator<<(ostream &out, const Coord &ob) {
out << "(" << ob.x << ", " << ob.y << ")";
Coord &operator += (Coord &ob); return out;
friend ostream &operator<<(ostream &out, const Coord &ob); }
friend istream &operator>>(istream &in, Coord &ob);
istream &operator>>(istream &in, Coord &ob) {
Coord &operator ++(){ cout << "Enter coordinates (x y): ";
++x; in >> ob.x >> ob.y;
++y; return in; int main(){
return *this; } Coord a(10, 20), b;
} int x, y;
23
Friend Function for Operator Overloading in C++
#include <iostream> Coord operator+(int n, const Coord& c) {
using namespace std; return Coord(n + c.x, n + c.y);
}
class Coord{
int x, y; Coord operator++(Coord& c) { int main(){
public: ++c.x; Coord c1(1, 2), c2(3, 4);
Coord(int x = 0, int y = 0) : x(x), y(y) {} ++c.y; Coord c3 = c1 + c2;
void show() const { cout << "(" << x << ", " << y << ")" << endl; } return c; c3.show();
}
friend Coord operator+(const Coord& c1, const Coord& c2); Coord c4 = c1 + 5;
friend Coord operator+(const Coord& c, int n); Coord operator++(Coord& c, int) { c4.show();
friend Coord operator+(int n, const Coord& c); ++c.x;
friend Coord operator++(Coord& c); ++c.y; Coord c5 = 5 + c1;
friend Coord operator++(Coord& c, int); return c; c5.show();
}; }
++c1;
Coord operator+(const Coord& c1, const Coord& c2) { c1.show();
return Coord(c1.x + c2.x, c1.y + c2.y); OUTPUT:
} (4, 6) c1++;
(6, 7) c1.show();
Coord operator+(const Coord& c, int n) { (6, 7)
return Coord(c.x + n, c.y + n); (2, 3) return 0;
} (3, 4) }
24
Assignment Operator
➢By default, when an assignment operator applied to an object, a bitwise copy is made. So, there is no need
to write own assignment operator.
➢In case of dynamic memory allocation, bitwise copy is not desirable and still need to write assignment
operator.
#include <iostream> int main(){
#include <cstring> strtype a("Hello"), b("There");
strtype:: strtype(char *s){
#include <cstdlib> int l;
using namespace std; cout<< a.get()<< " " << b.get()<< endl;
l = strlen(s) + 1;
a = b;
p = new char[l];
class strtype{ cout<< a.get()<< " " << b.get()<< endl;
strcpy( p, s );
char *p; len = l;
int len; return 0;
}
public: }
strtype(char *s); strtype &strtype:: operator = (strtype &ob){
~strtype() { delete [] p;} p = new char[ob.len]; OUTPUT:
char *get() { return p; } len = ob.len; Hello There
strtype &operator = (strtype &ob); strcpy(p, ob.p); There There
}; return *this;
}
25
Overloading Array Subscript Operator []
P5
P6
P2
P1 P3
26
Overloading Array Subscript Operator []
#include <iostream>
using namespace std;
const int arraySize = 2; int main(){
Point p1(3,4);
class Point {
int *arr; p1.print();
public: p1[0] = 6;
Point(int x = 0, int y = 0){ p1[1] = 8;
arr = new int[arraySize]; p1.print();
arr[0] = x;
arr[1] = y; return 0;
} }
void print(){
cout << "(" << arr[0] << "," << arr[1] << ")" << endl;
}
};
27
Type Conversion in C++
➢Type conversion from the type of argument to the type of class is of two types:
✓ implicit and
✓ explicit.
➢Type Conversion:
myclass ob(4); //supported by both type of conversion
myclass ob = 4; //not supported by explicit conversion
int main(){
#include <iostream>
Myclass ob1(10);
#include <cstdlib> OUTPUT:
// Myclass ob2 = 20; Error, Why?
using namespace std;
Myclass ob3("40"); ob1: 10
Myclass ob4 = "60"; // Ok, Why? ob3: 40
class Myclass{
ob4: 60
int a;
cout << "ob1: " << ob1.getA() << endl;
public:
cout << "ob3: " << ob3.getA() << endl;
explicit Myclass(int x){ a=x; }
cout << "ob4: " << ob4.getA() << endl;
Myclass(char *str){ a=atoi(str); }
int getA(){ return a; }
return 0;
};
}
28
Conversion Function in C++
➢ A conversion function automatically converts an object into a compatible value.
operator type() { return value;}
#include <iostream>
#include <cstring>
int main(){
using namespace std;
Rectangle r("Rectangle", 5, 10);
double area = r;
class Rectangle{
cout << "Area: " << area << endl;
char name[20];
char* name = r;
int length;
cout << "Name: " << name << endl;
int wide;
return 0;
public:
}
Rectangle(char *name, int length, int wide){
strcpy(this->name, name);
this->length = length;
this->wide = wide; OUTPUT:
} Area: 50
operator double(){ Name: Rectangle
return length * wide;
}
operator char*(){
return name;
}
};
29
Overloading in Java
30
Auto-Boxing and Auto Unboxing in Java
public class Main {
public static void main(String[] args) {
➢Java wrapper wraps primitive types into Integer iOb1 = Integer.valueOf(100);
Objects. The available wrappers are- Integer iOb2 = 150;
double num1 = iOb1;
Double, Float, Long, Integer,
int num2 = iOb2.intValue();
Short, Byte, Character and Boolean double sum1 = num1 + num2;
int sum2 = iOb1 + iOb2;
double num = iOb1.doubleValue();
➢ Auto Boxing wraps primitive types into the
respective Objects and unboxing unwraps System.out.println("Primitive Sum: "+ sum1);
Objects into primitive types. System.out.println("Object sum: " + sum2);
System.out.println("Primitive Sum: "+ num1 + num2);
System.out.println("Object sum: " + iOb1 + iOb2);
System.out.println(num);
➢ Two type of methods: }
valueOf() -> convert value to object. }
typeValue() -> convert object to primitive type.
OUTPUT:
Primitive Sum: 250.0
Object sum: 250
Primitive Sum: 100.0150
Object sum: 100150
100.0
31