OOPS - overriding and polymorphism, Study notes of Object Oriented Programming

Detailed informtion about Overriding

Typology: Study notes

2010/2011

Uploaded on 09/05/2011

vrunda
vrunda 🇮🇳

4.1

(21)

76 documents

1 / 14

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Overriding & Polymorphism
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download OOPS - overriding and polymorphism and more Study notes Object Oriented Programming in PDF only on Docsity!

Overriding & Polymorphism

Polymorphism

 It allows a single name or operator to be

associated with different operations

depending on the type of data passed to it.

Function Overloading

 Here we can use the same function name for

different purposes based on the requirement.

 The function name will be same but they will

differ with each other by return type or

arguments.

 E.g. int add ( int a, int b);

int add ( int a, int b, int c);

float add ( int a, int b, int c);

Code for function overloading

int volume( int s) { return s * s * s; } double volume ( double r, int n) { return (3.14 * r * r * n); } long volume ( long l, int b, int n) { return ( l * b * n); } int main ( ) { cout <<volume(10)<<”\n”; cout <<volume(2.5, 8)<<”\n”; cout <<volume(100L, 75, 15)<<”\n”; } Output : 1000

112500

Code for operator overloading

class complex{ float x;float y; public:complex ( float real, float imag) {x = real; y = imag; }complex operator + ( complex c) {complex temp; temp.x = x + c.x;temp.y = y + c.y; return ( temp );} void display ( ){ Cout<<x<<”+j”<<y<<”\n”;} };int main ( ) { complex c1, c2, c3; c1 = complex ( 2.5, 3.5);c2 = complex (1.6, 2.7 ); c3 = c1 + c2;cout<<”c1=”; c1.display ( ); cout<<”c2=”; c2.display ( );cout<<”c3=”; c3.display ( );

} return 0; Output:C1 = 2.5 + j3.5; C2 = 1.6 + j2.7;C3 = 4.1 + j6.2;

Complex operator + ( complex c ) { temp.x = + temp y = + return ( temp ); }

c.x c.y

x y

c3 = c1 + c

4.10 x 6.20 y

4.10 x 6.20 y

2.50 x 3.50 y

1.60 x 2.70 y

temp

Explanation for program

Early Binding

 (^) Early binding:- If there exist a function with same name and arguments in many of the derived classes, compiler decides which function to be executed based on the type of base class pointer. Decision is made at compile time itself. Base Void display ( cout<<“Display Base”)

Derived Void display ( cout<<“Display Derived”)

Int main ( ) { Base B; Derived D; Base *ptr; ptr=&B; ptr->display ( ); ptr=&D; ptr->display ( ); } Actual Output: Expected Output: Display Base Display Base Display Base Display Derived

Late Binding

 (^) Late binding:- If there exist a function with same name and arguments in many of the derived classes, compiler decides which function to be executed based on the content of base class pointer. Decision is postponed to run time. Virtual key word is added in front of the base class function to achieve late binding

Base Virtual Void display ( cout<<“Display Base”)

Derived Void display ( cout<<“Display Derived”)

Int main ( ) { Base B; Derived D; Base *ptr; ptr=&B; ptr->display ( ); ptr=&D; ptr->display ( ); } Output: Display Base Display Derived

Overriding Methods (Terms)

 Supply a different implementation of a method that

exists in the superclass

 Must have same signature (same name and same

parameter types)

 Private data fields in a superclass are not accessible

to subclasses.

 Private methods in the superclass can not be

overridden.

 Static methods in the superclass can not be

overridden.

 Data fields in the superclass can not be overridden.

Overriding vs. Overloading

 To overload a method of the superclass:

Supply a different implementation (with a

new signature) of a method that exists in the

superclass. (Same as before…)

 Overridden methods have the same

signature (list of parameters) and same

return type.