Polymorphism in c++(ppt)

18,217 views 12 slides Feb 27, 2017
Slide 1
Slide 1 of 12
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12

About This Presentation

This presentation is about Polymorphism in c++ covering topic types of polymorphism,it's types and real life example of Polymorphism.


Slide Content

POLYMORPHISM IN C++

Haldia Institute of Technology Presented By : - Name Roll no. Purabi Biswas 14/CS/70 Sanjit Shaw B14/CS/127 Shubham Singhanaia B14/CS/127 (Computer Science & Engineering)

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. 

  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.

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.

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.

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); }

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 Overloading Unary operator overloading . These Operators have only single operand. Examples:- ++,--,~,! Binary operator overloading. These operators can have two or more operands. Examples:-+,-,*,/,%,^,=,==,+=,&,&& etc

Operator Overloading Example c lass complex { int main(){ p rivate:: complex c1,c2,c3; i nt a,b ; c1.set_data(3,4); p ublic : void set_data ( int x, int y){ c2.set_data(5,6); a= x;b =y;} c3=c1.add+( c2); v oid show_data (){ c3.show_data(); cout <<“ \n a=“<<a<<“b=“<<b;} return 0;} c omplex add+(complex c){ c omplex temp ; t emp.a = a+c.a ; t emp.b = b+c.b ; r eturn temp;}};

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.

Using Virtual Keyword Example class 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; }

Special Thanks To:- Mrs.   Rajrupa Metia ( Asst. Professor) Computer Science & Engineering