Polly Morphis m

Preview:

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

Citation preview

Deepali Singla

Assistant Professor

Chandigarh university

Polymorphism

1

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

Types of Polymorphism

Two types of polymorphism:

Compile time

Function and operator overloading

Run time

Virtual Function

3

Compile time

This is also the early binding

In that compiler decide the scope of member function at the

compile time.

4

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

ThanQ

42

Recommended