Polly Morphis m

42
Deepali Singla Assistant Professor Chandigarh university Polymorphism 1

description

Operator overloadingEnabling C++’s operators to work with class objectsUsing traditional operators with user-defined objectsRequires great care; when overloading is misused, program difficult to understandExamples of already overloaded operatorsOperator + and -, perform arithmetic on multiple typesCompiler generates the appropriate code based on the manner in which the operator is used

Transcript of Polly Morphis m

Page 1: Polly Morphis m

Deepali Singla

Assistant Professor

Chandigarh university

Polymorphism

1

Page 2: Polly Morphis m

Polymorphism

Polymorphism is the ability to use an operator or function in

different ways.

Polymorphism gives different meanings or functions to the operators

or function.

Poly, referring too many, signifies the many uses of those operators

and functions

A single function usage or an operator functioning in many ways can

be called polymorphism.

Polymorphism refers to codes, operations or objects that behave

differently in different contexts.

2

Page 3: Polly Morphis m

Types of Polymorphism

Two types of polymorphism:

Compile time

Function and operator overloading

Run time

Virtual Function

3

Page 4: Polly Morphis m

Compile time

This is also the early binding

In that compiler decide the scope of member function at the

compile time.

4

Page 5: Polly Morphis m

Function Overloading

Overloading a function Simply means, that a function is not

only defined its name but by its name and parameter types.

Function should be declare or define in same class

The following function are different :

int area(int I, int k);

void area(float I, float k);

float area();

5

Page 6: Polly Morphis m

Function Overloading

Overloading a function Simply means, that a function is not

only defined its name but by its name and parameter types.

Function should be declare or define in same class

The following function are different :

int area(int I, int k);

void area(float I, float k);

float area();

These three

methods are

different Bases on

there arguments

6

Page 7: Polly Morphis m

Function Overloading

class A

{

public:

void sum(int a, int b)

{

cout<<"Integers sum is :"<<(a+b)<<endl;

}

void sum(float a, float b)

{

cout<<"float sum = "<<(a+b)<<endl;

}

};

void main()

{

A a1;

float i=1.01,j=9.23;

a1.sum(2,5);

a1.sum(i,j);

}

Function overloading

7

Page 8: Polly Morphis m

Function Overloading

class A

{

public:

void sum(int a, int b)

{

cout<<"Integers sum is :"<<(a+b)<<endl;

}

void sum(float a, float b)

{

cout<<"float sum = "<<(a+b)<<endl;

}

};

void main()

{

A a1;

float i=1.01,j=9.23;

a1.sum(2,5);

a1.sum(i,j);

}

Output:

Integers sum is : 7

float sum = 10.24

8

Page 9: Polly Morphis m

Operator Overloading

When operator is overloaded with multiple jobs, it is known

as a operator overloading

Means one operator have different meaning.

It is a way to implement compile time polymorphism.

9

Page 10: Polly Morphis m

Operator Overloading

Rules :

Any symbol can be used as function name

If it is valid operator in c language

If it is preceded by operator keywords

You cannot overloaded sizeof and ?: (Conditional)

operator

10

Page 11: Polly Morphis m

Operator Overloading class complex

{

private:

int a,b;

public:

void setdata(int x, int y)

{

a=x; b=y;

}

void showdata()

{

cout<<"a= "<<a<<", b= "<<b<<endl;

}

};

void main()

{

complex c1,c2,c3;

c1.setdata(4,5);

c2.setdata(6,8);

c3=c1 + c2;

c3.showdata();

}

11

Page 12: Polly Morphis m

Operator Overloading class complex

{

private:

int a,b;

public:

void setdata(int x, int y)

{

a=x; b=y;

}

void showdata()

{

cout<<"a= "<<a<<", b= "<<b<<endl;

}

};

void main()

{

complex c1,c2,c3;

c1.setdata(4,5);

c2.setdata(6,8);

c3=c1 + c2;

c3.showdata();

}

Error

12

Page 13: Polly Morphis m

Operator Overloading class complex

{

private:

int a,b;

public:

void setdata(int x, int y)

{

a=x; b=y;

}

void showdata()

{

cout<<"a= "<<a<<", b= "<<b<<endl;

}

};

void main()

{

complex c1,c2,c3;

c1.setdata(4,5);

c2.setdata(6,8);

c3= c1.add(c2);

c3.showdata();

}

13

Page 14: Polly Morphis m

Operator Overloading class complex

{

private:

int a,b;

public:

void setdata(int x, int y)

{

a=x; b=y;

}

void showdata()

{

cout<<"a= "<<a<<", b= "<<b<<endl;

}

complex add(complex o1)

{

complex temp;

temp.a= a + o1.a;

temp.b= b + o1.b;

return (temp);

} };

void main()

{

complex c1,c2,c3;

c1.setdata(4,5);

c2.setdata(6,8);

c3= c1.add(c2);

c3.showdata();

}

14

Page 15: Polly Morphis m

Operator Overloading class complex

{

private:

int a,b;

public:

void setdata(int x, int y)

{

a=x; b=y;

}

void showdata()

{

cout<<"a= "<<a<<", b= "<<b<<endl;

}

complex add(complex o1)

{

complex temp;

temp.a= a + o1.a;

temp.b= b + o1.b;

return (temp);

} };

void main()

{

complex c1,c2,c3;

c1.setdata(4,5);

c2.setdata(6,8);

c3= c1.add(c2);

c3.showdata();

}

a

b

functions

a

b

functions

a

b

functions

c1 c2 o1

a

b

functions

temp

a

b

functions

c3 15

Page 16: Polly Morphis m

Operator Overloading class complex

{

private:

int a,b;

public:

void setdata(int x, int y)

{

a=x; b=y;

}

void showdata()

{

cout<<"a= "<<a<<", b= "<<b<<endl;

}

complex add(complex o1)

{

complex temp;

temp.a= a + o1.a;

temp.b= b + o1.b;

return (temp);

} };

void main()

{

complex c1,c2,c3;

c1.setdata(4,5);

c2.setdata(6,8);

c3= c1.add(c2);

c3.showdata();

}

a= 4

b= 5

functions

a

b

functions

a

b

functions

c1 c2 o1

a

b

functions

temp

a

b

functions

c3 16

Page 17: Polly Morphis m

Operator Overloading class complex

{

private:

int a,b;

public:

void setdata(int x, int y)

{

a=x; b=y;

}

void showdata()

{

cout<<"a= "<<a<<", b= "<<b<<endl;

}

complex add(complex o1)

{

complex temp;

temp.a= a + o1.a;

temp.b= b + o1.b;

return (temp);

} };

void main()

{

complex c1,c2,c3;

c1.setdata(4,5);

c2.setdata(6,8);

c3= c1.add(c2);

c3.showdata();

}

a= 4

b= 5

functions

a= 6

b= 8

functions

a

b

functions

c1 c2 o1

a

b

functions

temp

a

b

functions

c3 17

Page 18: Polly Morphis m

Operator Overloading class complex

{

private:

int a,b;

public:

void setdata(int x, int y)

{

a=x; b=y;

}

void showdata()

{

cout<<"a= "<<a<<", b= "<<b<<endl;

}

complex add(complex o1)

{

complex temp;

temp.a= a + o1.a;

temp.b= b + o1.b;

return (temp);

} };

void main()

{

complex c1,c2,c3;

c1.setdata(4,5);

c2.setdata(6,8);

c3= c1.add(c2);

c3.showdata();

}

a= 4

b= 5

functions

a= 6

b= 8

functions

a

b

functions

c1 c2 o1

a

b

functions

temp

a

b

functions

c3 18

Page 19: Polly Morphis m

Operator Overloading class complex

{

private:

int a,b;

public:

void setdata(int x, int y)

{

a=x; b=y;

}

void showdata()

{

cout<<"a= "<<a<<", b= "<<b<<endl;

}

complex add(complex o1)

{

complex temp;

temp.a= a + o1.a;

temp.b= b + o1.b;

return (temp);

} };

void main()

{

complex c1,c2,c3;

c1.setdata(4,5);

c2.setdata(6,8);

c3= c1.add(c2);

c3.showdata();

}

a= 4

b= 5

functions

a= 6

b= 8

functions

a

b

functions

c1 c2 o1

a

b

functions

temp

a

b

functions

c3 19

Page 20: Polly Morphis m

Operator Overloading class complex

{

private:

int a,b;

public:

void setdata(int x, int y)

{

a=x; b=y;

}

void showdata()

{

cout<<"a= "<<a<<", b= "<<b<<endl;

}

complex add(complex o1)

{

complex temp;

temp.a= a + o1.a;

temp.b= b + o1.b;

return (temp);

} };

void main()

{

complex c1,c2,c3;

c1.setdata(4,5);

c2.setdata(6,8);

c3= c1.add(c2);

c3.showdata();

}

a= 4

b= 5

functions

a= 6

b= 8

functions

a= 6

b= 8

functions

c1 c2 o1

a

b

functions

temp

a

b

functions

c3 20

Page 21: Polly Morphis m

Operator Overloading class complex

{

private:

int a,b;

public:

void setdata(int x, int y)

{

a=x; b=y;

}

void showdata()

{

cout<<"a= "<<a<<", b= "<<b<<endl;

}

complex add(complex o1)

{

complex temp;

temp.a= a + o1.a;

temp.b= b + o1.b;

return (temp);

} };

void main()

{

complex c1,c2,c3;

c1.setdata(4,5);

c2.setdata(6,8);

c3= c1.add(c2);

c3.showdata();

}

a= 4

b= 5

functions

a= 6

b= 8

functions

a= 6

b= 8

functions

c1 c2 o1

a= 10

b

functions

temp

a

b

functions

c3 21

Page 22: Polly Morphis m

Operator Overloading class complex

{

private:

int a,b;

public:

void setdata(int x, int y)

{

a=x; b=y;

}

void showdata()

{

cout<<"a= "<<a<<", b= "<<b<<endl;

}

complex add(complex o1)

{

complex temp;

temp.a= a + o1.a;

temp.b= b + o1.b;

return (temp);

} };

void main()

{

complex c1,c2,c3;

c1.setdata(4,5);

c2.setdata(6,8);

c3= c1.add(c2);

c3.showdata();

}

a= 4

b= 5

functions

a= 6

b= 8

functions

a= 6

b= 8

functions

c1 c2 o1

a= 10

b= 13

functions

temp

a

b

functions

c3 22

Page 23: Polly Morphis m

Operator Overloading class complex

{

private:

int a,b;

public:

void setdata(int x, int y)

{

a=x; b=y;

}

void showdata()

{

cout<<"a= "<<a<<", b= "<<b<<endl;

}

complex add(complex o1)

{

complex temp;

temp.a= a + o1.a;

temp.b= b + o1.b;

return (temp);

} };

void main()

{

complex c1,c2,c3;

c1.setdata(4,5);

c2.setdata(6,8);

c3= c1.add(c2);

c3.showdata();

}

a= 4

b= 5

functions

a= 6

b= 8

functions

a

b

functions

c1 c2 o1

a= 10

b= 13

functions

temp

a

b

functions

c3 23

Page 24: Polly Morphis m

Operator Overloading class complex

{

private:

int a,b;

public:

void setdata(int x, int y)

{

a=x; b=y;

}

void showdata()

{

cout<<"a= "<<a<<", b= "<<b<<endl;

}

complex add(complex o1)

{

complex temp;

temp.a= a + o1.a;

temp.b= b + o1.b;

return (temp);

} };

void main()

{

complex c1,c2,c3;

c1.setdata(4,5);

c2.setdata(6,8);

c3= c1.add(c2);

c3.showdata();

}

a= 4

b= 5

functions

a= 6

b= 8

functions

a

b

functions

c1 c2 o1

a= 10

b= 13

functions

temp

a = 10

b= 13

functions

c3 24

Page 25: Polly Morphis m

Operator Overloading class complex

{

private:

int a,b;

public:

void setdata(int x, int y)

{

a=x; b=y;

}

void showdata()

{

cout<<"a= "<<a<<", b= "<<b<<endl;

}

complex add(complex o1)

{

complex temp;

temp.a= a + o1.a;

temp.b= b + o1.b;

return (temp);

} };

void main()

{

complex c1,c2,c3;

c1.setdata(4,5);

c2.setdata(6,8);

c3= c1.add(c2);

c3.showdata();

}

Output:

a= 10, b= 13

25

Page 26: Polly Morphis m

Operator Overloading class complex

{

private:

int a,b;

public:

void setdata(int x, int y)

{

a=x; b=y;

}

void showdata()

{

cout<<"a= "<<a<<", b= "<<b<<endl;

}

complex add(complex o1)

{

complex temp;

temp.a= a + o1.a;

temp.b= b + o1.b;

return (temp);

} };

void main()

{

complex c1,c2,c3;

c1.setdata(4,5);

c2.setdata(6,8);

c3= c1.add(c2);

c3.showdata();

}

In place of add can we write

Sum ?

Yes

But can we write +

26

Page 27: Polly Morphis m

Operator Overloading class complex

{

private:

int a,b;

public:

void setdata(int x, int y)

{

a=x; b=y;

}

void showdata()

{

cout<<"a= "<<a<<", b= "<<b<<endl;

}

complex add(complex o1)

{

complex temp;

temp.a= a + o1.a;

temp.b= b + o1.b;

return (temp);

} };

void main()

{

complex c1,c2,c3;

c1.setdata(4,5);

c2.setdata(6,8);

c3= c1.add(c2);

c3.showdata();

}

In place of add can we write

Sum ?

Yes

But can we write +

Yes we can write + in place of

Function name using the

operator keyword 27

Page 28: Polly Morphis m

Operator Overloading class complex

{

private:

int a,b;

public:

void setdata(int x, int y)

{

a=x; b=y;

}

void showdata()

{

cout<<"a= "<<a<<", b= "<<b<<endl;

}

complex operator +(complex o1)

{

complex temp;

temp.a= a + o1.a;

temp.b= b + o1.b;

return (temp);

} };

void main()

{

complex c1,c2,c3;

c1.setdata(4,5);

c2.setdata(6,8);

c3= c1 + c2

c3.showdata();

}

Here we perform operator

overloading.

In that program + operator

used to add two complex no

But in normal case this is

used to add two primitive no.

28

Page 29: Polly Morphis m

Operator Overloading class complex

{

private:

int a,b;

public:

void setdata(int x, int y)

{

a=x; b=y;

}

void showdata()

{

cout<<"a= "<<a<<", b= "<<b<<endl;

}

complex operator +(complex o1)

{

complex temp;

temp.a= a + o1.a;

temp.b= b + o1.b;

return (temp);

} };

void main()

{

complex c1,c2,c3;

c1.setdata(4,5);

c2.setdata(6,8);

c3= c1 + c2

c3.showdata();

}

Output:

a= 10, b= 13

29

Page 30: Polly Morphis m

Unary operator overloading

Unary operator is a operator where we have only one

operant.

For example:

b=-a

Here – is a unary operator which is used to perform

negation operation

30

Page 31: Polly Morphis m

class complex

{

private:

int a,b;

public:

void setdata(int x, int y)

{

a=x; b=y;

}

void showdata()

{

cout<<"a= "<<a<<", b= "<<b<<endl;

}

complex operator -()

{

complex temp;

temp.a= -a;

temp.b= -b;

return (temp);

} };

void main()

{

complex c1,c2;

c1.setdata(4,5);

c2= -c1;

c2.showdata();

}

Unary operator overloading

31

Page 32: Polly Morphis m

class complex

{

private:

int a,b;

public:

void setdata(int x, int y)

{

a=x; b=y;

}

void showdata()

{

cout<<"a= "<<a<<", b= "<<b<<endl;

}

complex operator -()

{

complex temp;

temp.a= -a;

temp.b= -b;

return (temp);

} };

void main()

{

complex c1,c2;

c1.setdata(4,5);

c2= -c1;

c2.showdata();

}

Unary operator overloading

Output:

a= -4, b= -5

32

Page 33: Polly Morphis m

Run time

Run time polymorphism also called the dynamic binding or

late binding

Dynamic means object are created at run time

Dynamic binding offer a greater flexibility and higher level

of abstraction than static binding because it is done “on the

fly” when a program executes

33

Page 34: Polly Morphis m

Run time

Virtual function is the best example of run time

polymorphism

Virtual function :

if we define any function as a virtual that means that function cannot

bind at compile time.

Compiler perform late binding of that function

So using the virtual function we try to remove the ambiguity

problem in a program during the inheritance

34

Page 35: Polly Morphis m

Run time

class A

{

public:

void f1()

{

cout<< "hello"<<endl;

}

};

class B : public A

{

public:

void f1()

{

cout<<"hello there"<<endl;

}

};

void main()

{

A *p;

B ob;

p=&ob;

p->f1();

}

35

Page 36: Polly Morphis m

Run time

class A

{

public:

void f1()

{

cout<< "hello"<<endl;

}

};

class B : public A

{

public:

void f1()

{

cout<<"hello there"<<endl;

}

};

void main()

{

A *p;

B ob;

p=&ob;

p->f1();

}

Output:

hello

36

Page 37: Polly Morphis m

Run time

class A

{

public:

void f1()

{

cout<< "hello"<<endl;

}

};

class B : public A

{

public:

void f1()

{

cout<<"hello there"<<endl;

}

};

void main()

{

A *p;

B ob;

p=&ob;

p->f1();

}

Output:

hello

This is wrong because we want to execute drive

class’s function f1() but here execute base class’s function.

So that’s why we perform late binding . But how can be do

that?

Using the virtual keywords.

Syntax:

virtual <return type> <name of function> <no. of

arg.> 37

Page 38: Polly Morphis m

Run time

class A

{

public:

virtual void f1()

{

cout<< "hello"<<endl;

}

};

class B : public A

{

public:

void f1()

{

cout<<"hello there"<<endl;

}

};

void main()

{

A *p;

B ob;

p=&ob;

p->f1();

}

Output:

hello

Function define as a virtual

38

Page 39: Polly Morphis m

Run time

class A

{

public:

virtual void f1()

{

cout<< "hello"<<endl;

}

};

class B : public A

{

public:

void f1()

{

cout<<"hello there"<<endl;

}

};

void main()

{

A *p;

B ob;

p=&ob;

p->f1();

}

Output:

Hello there

Function define as a virtual

39

Page 40: Polly Morphis m

Run time

Static Binding Dynamic Binding

Static binding means that the

legality of a member function

invocation is checked at the

earliest possible moment : by

the compiler at compile time.

The compiler uses the static

type of the pointer to

determine weather the

member function invocation

is legal

Dynamic binding means that

the address of the code in a

member function invocation

is determined at the last

possible moment: based on

the dynamic type of the

object at run time. It is called

“dynamic binding” because

the binding to the code that

actually gets called as

accomplished dynamically (at

run time). 40

Page 41: Polly Morphis m

Run time

Static Binding Dynamic Binding

With static binding, you get

better run time efficiency

because the compiler can

actually optimize the code

before running it.

The CLR knows how much

memory to take up for the

static method object

Dynamic binding offers

greater flexibility and a higher

level of abstraction than static

binding because it is done “on

the fly” when a program

executes.

The CLR doesn't know how

much memory to take up for

the dynamic method object

41

Page 42: Polly Morphis m

ThanQ

42