polymorphism in c++(ppt)

12
POLYMORPHISM IN C++

Upload: sanjit-shaw

Post on 22-Jan-2018

538 views

Category:

Education


58 download

TRANSCRIPT

Page 1: Polymorphism in c++(ppt)

POLYMORPHISM IN C++

Page 2: Polymorphism in c++(ppt)

Haldia Institute of TechnologyPresented By : -

Name Roll no.

Purabi Biswas 14/CS/70

Sanjit Shaw B14/CS/127

Shubham Singhanaia B14/CS/127

(Computer Science & Engineering)

Page 3: Polymorphism in c++(ppt)

POLYMORPHISM

The process of representing one Form in multiple forms is known as Polymorphism. Here one form represent original form or original method always resides in base class and multiple forms represents overridden method which resides in derived classes.

Polymorphism is derived from 2 Greek words: poly and morphs. The word "poly" means many and morphs means forms.

Page 4: Polymorphism in c++(ppt)

Real life example of Polymorphism

Suppose if you are in class room that time you behave like a student, when you are in market at that time you behave like a customer, when you at your home at that time you behave like a son or daughter, Here one person have different-different behaviors.

Page 5: Polymorphism in c++(ppt)

Type of Polymorphism

Static polymorphism is also known as early binding and compile-time polymorphism. In static polymorphism memory will be allocated at compile-time.

Dynamic polymorphism is also known as late binding and run-time polymorphism. In dynamic polymorphism memory will be allocated at run-time.

Polymorphism

Static

Function Overloading

Operator Overloading

Dynamic

Virtual Functions

Page 6: Polymorphism in c++(ppt)

Method Overloading

Whenever same method name is exiting multiple times in the same class with different number of parameter or different order of parameters or different types of parameters is known as method overloading.

In next example method "sum()" is present in Addition class with same name but with different signature or arguments.

Page 7: Polymorphism in c++(ppt)

Function Overloading Example class Addition {

public: void sum(int a, int b) {

cout<<"a+b :"<<a+b; } //output :- a+b : 30

void sum(int a, int b, int c) {

cout<<"a+b+c :"<<a+b+c; } //output :- a+b+c : 60

};

int main() {

Addition obj;

obj.sum(10, 20);

cout<<endl;

obj.sum(10, 20, 30); }

Page 8: Polymorphism in c++(ppt)

Operator Overloading

The process of making an operator to exhibit different behaviors in different instances is known as operator overloading.

Only predefined operator can be overloaded.

Types Of Operator OverloadingUnary operator overloading.

These Operators have only single operand.

Examples:- ++,--,~,!

Binary operator overloading.

These operators can have two or more operands.

Examples:-+,-,*,/,%,^,=,==,+=,&,&& etc

Page 9: Polymorphism in c++(ppt)

Operator Overloading Exampleclass complex{ int main(){

private:: complex c1,c2,c3;

int a,b; c1.set_data(3,4);

public:void set_data(int x,int y){ c2.set_data(5,6);

a=x;b=y;} c3=c1.add+(c2);

void show_data(){ c3.show_data();

cout<<“\n a=“<<a<<“b=“<<b;} return 0;}

complex add+(complex c){

complex temp ;

temp.a=a+c.a;

temp.b=b+c.b;

return temp;}};

Page 10: Polymorphism in c++(ppt)

Virtual Function

A virtual function is a member function that is declared as virtual within a base class and redefined by a derived class.

To create virtual function, precede the base version of function’s declaration with the keyword virtual.

Here we use a pointer to the base class to refer to all the derived objects.

The method name and type signature should be same for both base and derived version of function.

Page 11: Polymorphism in c++(ppt)

Using Virtual Keyword Exampleclass A {

public: virtual void show() {

cout<<"Content of base class.\n"; }};

class B : public A {

public: void show() {

cout<<"Content of derived class.\n"; } };

int main() {

A b,*bptr; //Base class pointer

B d; //Derived class object

bptr = &b;

bptr->show(); //Late Binding Occurs

Bptr=&d;

Bptr->show(); return 0; }

Page 12: Polymorphism in c++(ppt)

Special Thanks To:-

Mrs. Rajrupa Metia

(Asst. Professor)

Computer Science & Engineering