Polymorphism In c++

XtylishJhaVishesh 2,312 views 11 slides Nov 06, 2019
Slide 1
Slide 1 of 11
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

About This Presentation

All about the polymorphism like definition ,types,with daily life example.


Slide Content

Polymorphism In C++ Polymorphism In C++ Presented by: Vishesh Kumar jha 04620602018 BCA 3 rd Semester 1 st Shift [email protected] TRINITY INSTITUTE OF PROFESSIONAL STUDIES (Affiliated to Guru Gobind Singh Indraprastha University,Delhi ) Ranked “A+” Institution of GGSIPU, Delhi Recognised under section 2(f) of the UGC Act, 1956 NAAC Accredited “B++” Grade Institution Ranked “A+” Institution by SFRC, Govt. of NCT of India

POLYMORPHISM Polymorphism is derived from 2 Greek words: poly and morphs. The word "poly" means many and morphs means forms. Polymorphism is the ability to use an operator or function in different manner,it gives the special meaning to the function or operator and also it refers to the codes,operations,or objects that behave differently in different context.

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 There are mainly five types of polymorphism 1.Ad-hoc 2.Parametric 3.Sub-typing 4.Row 5.Polytypism. 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 St a tic Function Overlo a ding Operator Overlo a ding Dyna m ic Virtual Functio ns

Method Overloadi n g 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) { //output :- a+b : 30 //output :- a+b+c : 60 cout <<"a+b :"<<a+b; } void sum( int a, int b, int c) { cout <<"a+b+c :"<<a+b+c; } }; 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 . Some operators which can’t be overloaded such as scope resolution operator , sizeof operator,class member acess and conditional operator etc. 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 void main() { clrscr (); test p1; p1.gt(-10,-90,-23); p1.dp(); cout <<"Values after operator overloading"<< endl ; -p1; p1.dp(); getch (); } # include< iostream.h > #include< conio.h > class test{ int a,b,c ; public: void get(int x,int y,int z); void operator -(); void dp (); }; void test:: gt (int x,int y,int z) {a=x; b=y; c=z;} void test::operator-() { a=-a; b=-b; c=-c;} void test:: dp () { cout <<"Value of a="<<a<< endl ; cout <<"Value of b="<<b<< endl ; cout <<"Value of c="<<c<< endl ; }  

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 function name and type signature should be same for both base and derived version of function.

Virtual Function Example class Base { public : virtual void show() { cout <<"Content of base class.\n"; }}; class derived : public A { public : void show() { cout <<"Content of derived class.\n"; } }; int main() { Base *a //Base class pointer derived p1 ; //Derived class a = & p1 ; a ->show(); //Late Binding Occurs getch (); return 0; }
Tags