Function overriding:- If we inherit a class in derived class and body of one or more function of base class is pre-defined in derived class then this function is called overridden and this process is called function overriding.
• Difference between function overloading and function overriding –
1. In function overloading relationship of methods of single class is defined, whereas in function overriding we define relation between super class methods and sub-class methods.
2. In function overloading signature of method should different whereas in function overriding method signature should same.
3. In function overloading various methods share same name whereas in function overriding methods of sub-class override the methods of super-class.
4. In function overloading inheritance can be blocked whereas in function overriding inheritance can not be blocked.
Example:-
C++ program for function overriding.
#include<iostream.h>
#include<conio.h>
class vol{
public:
void volume(){
cout<<"volume function of base class";
}
};
class cube:public vol{
public:
void volume(){
cout<<"volume function of base class";
}
};
void main(){
cube c;
clrscr();
c.volume(); //function overriding
getch();
}
C++ program for function overriding using base class pointer.
#include<iostream.h>
#include<conio.h>
class vol{
public:
void volume(){
cout<<"Volume function of base class"<<endl;
}};
class cube:public vol{
public:
void volume(){
cout<<"Volume function of derived class"<<endl;
}};
void main(){
vol *v,b;
cube c;
clrscr();
//Pointer access of base class
v=&b;
v->volume();
//Pointer access of base class
v=&c;
v->volume();
// Class object access
b.volume();
c.volume();
getch();
}
• Difference between function overloading and function overriding –
1. In function overloading relationship of methods of single class is defined, whereas in function overriding we define relation between super class methods and sub-class methods.
2. In function overloading signature of method should different whereas in function overriding method signature should same.
3. In function overloading various methods share same name whereas in function overriding methods of sub-class override the methods of super-class.
4. In function overloading inheritance can be blocked whereas in function overriding inheritance can not be blocked.
Example:-
C++ program for function overriding.
#include<iostream.h>
#include<conio.h>
class vol{
public:
void volume(){
cout<<"volume function of base class";
}
};
class cube:public vol{
public:
void volume(){
cout<<"volume function of base class";
}
};
void main(){
cube c;
clrscr();
c.volume(); //function overriding
getch();
}
#include<iostream.h>
#include<conio.h>
class vol{
public:
void volume(){
cout<<"Volume function of base class"<<endl;
}};
class cube:public vol{
public:
void volume(){
cout<<"Volume function of derived class"<<endl;
}};
void main(){
vol *v,b;
cube c;
clrscr();
//Pointer access of base class
v=&b;
v->volume();
//Pointer access of base class
v=&c;
v->volume();
// Class object access
b.volume();
c.volume();
getch();
}
No comments:
Post a Comment