








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Detailed informtion about Overriding
Typology: Study notes
1 / 14
This page cannot be seen from the preview
Don't miss anything!









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
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
(^) 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:- 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