C++ Programming - Functions - Discussion
Discussion Forum : Functions - General Questions (Q.No. 8)
8.
Which of the following function / type of function cannot be overloaded?
Discussion:
9 comments Page 1 of 1.
Mnshetty said:
4 years ago
How do you say that? explain, please.
Vyankatesh said:
4 years ago
According to me, both static and virtual functions cannot be overloaded!
(1)
Fouad Ayman said:
6 years ago
Virtual and static both can be overloaded: let me explain.
Regrading to virtual function :
virtual function can be overloaded as any function you know in the base class, but in the child class you can overload the virtual function that already exists in the base class.
For example:
Suppose you have those function in the base class:
virtual void show();
virtual void show(int);
virtual void show(float);
In the child class you can overload also the virtual function " that you have overriden" like that :
void show(); //override the virtual function show
void show(int); //override the virtual function show(int) "which is in the base" and OVERLOAD it in child
void show(char); //the compiler will complain about this as you are allowed only to override functions that already exist in the base class.
Regrading to virtual function :
virtual function can be overloaded as any function you know in the base class, but in the child class you can overload the virtual function that already exists in the base class.
For example:
Suppose you have those function in the base class:
virtual void show();
virtual void show(int);
virtual void show(float);
In the child class you can overload also the virtual function " that you have overriden" like that :
void show(); //override the virtual function show
void show(int); //override the virtual function show(int) "which is in the base" and OVERLOAD it in child
void show(char); //the compiler will complain about this as you are allowed only to override functions that already exist in the base class.
(1)
Avijeet kumar said:
8 years ago
It seems that the answer is not correct. The answer does not match with any of the options.
The answer should be none of the above. Everyone knows that member function can be overloaded. But even static and virtual function can also be overloaded. Although virtual keyword is used to override the function, but It can also be overloaded.
I verified with the below example:
====================================
#include <iostream>
#include <memory>
using namespace std;
class A{
public:
static void fun(){ cout<<"static A-\n"; }
static void fun(int a){ cout<<"static A--\n" ; }
virtual void disp(){ cout<<"A-\n"; }
virtual void disp(int a){ cout<<"A--\n"; }
};
class B: public A{
public:
void disp(){ cout<<"B-\n"; }
void disp(int a){ cout<<"B--\n"; }
};
int main()
{
unique_ptr<A> ob = make_unique<B>();
ob->disp();
ob->disp(2);
A ob1;
ob1.disp();
ob1.disp(2);
B ob2;
ob2.disp();
ob2.disp(21);
A::fun();
A::fun(1);
return 0;
}
The answer should be none of the above. Everyone knows that member function can be overloaded. But even static and virtual function can also be overloaded. Although virtual keyword is used to override the function, but It can also be overloaded.
I verified with the below example:
====================================
#include <iostream>
#include <memory>
using namespace std;
class A{
public:
static void fun(){ cout<<"static A-\n"; }
static void fun(int a){ cout<<"static A--\n" ; }
virtual void disp(){ cout<<"A-\n"; }
virtual void disp(int a){ cout<<"A--\n"; }
};
class B: public A{
public:
void disp(){ cout<<"B-\n"; }
void disp(int a){ cout<<"B--\n"; }
};
int main()
{
unique_ptr<A> ob = make_unique<B>();
ob->disp();
ob->disp(2);
A ob1;
ob1.disp();
ob1.disp(2);
B ob2;
ob2.disp();
ob2.disp(21);
A::fun();
A::fun(1);
return 0;
}
Rosh said:
9 years ago
Virtual and static both cannot be overload? Or just virtual?
Daniel Sandor said:
10 years ago
Let us see this example:
#include <iostream>
struct Ts
{
virtual void f(){ // Ts::f() is virtual.
std::cout<<"Ts::f()"<<std::endl;
}
virtual void f() const{ // Ts::f() is overloaded with Ts::f() const.
std::cout<<"Ts::f() const"<<std::endl;
}
virtual void f(int){ // Ts::f() is overloaded again.
std::cout<<"Ts::f(int)"<<std::endl;
}
};
struct Ps : Ts
{
void f(){ // Ts::f() is overriden.
std::cout<<"Ps::f()"<<std::endl;
}
};
int main(){
Ts t;
t.f(); // "Ts::f()\n" on the output.
t.f(1); // "Ts::f(int)\n" on the output.
const Ts T;
T.f(); // "Ts::f() const" on the output.
Ps p;
p.f(); // "Ps::f()" on the output.
}
I think virtual member functions can be overloaded and overridden at a time. Did I misunderstood something?
#include <iostream>
struct Ts
{
virtual void f(){ // Ts::f() is virtual.
std::cout<<"Ts::f()"<<std::endl;
}
virtual void f() const{ // Ts::f() is overloaded with Ts::f() const.
std::cout<<"Ts::f() const"<<std::endl;
}
virtual void f(int){ // Ts::f() is overloaded again.
std::cout<<"Ts::f(int)"<<std::endl;
}
};
struct Ps : Ts
{
void f(){ // Ts::f() is overriden.
std::cout<<"Ps::f()"<<std::endl;
}
};
int main(){
Ts t;
t.f(); // "Ts::f()\n" on the output.
t.f(1); // "Ts::f(int)\n" on the output.
const Ts T;
T.f(); // "Ts::f() const" on the output.
Ps p;
p.f(); // "Ps::f()" on the output.
}
I think virtual member functions can be overloaded and overridden at a time. Did I misunderstood something?
K2u2007 said:
1 decade ago
At first glance, the redefinition of a virtual function by a derived class appears similar to function overloading. However, this is not the case, and the term overloading is not applied to virtual function redefinition because several differences exist. Perhaps the most important is that the prototype for a redefined virtual function must match exactly the prototype specified in the base class.
This differs from overloading a normal function, in which return types and the number and type of parameters may differ. (In fact, when you overload a function, either the number or the type of the parameters must differ! It is through these differences that C++ can select the correct version of an overloaded function).
However, when a virtual function is redefined, all aspects of its prototype must be the same. If you change the prototype when you attempt to redefine a virtual function, the function will simply be considered overloaded by the C++ compiler, and its virtual nature will be lost. Another important restriction is that virtual functions must be nonstatic members of the classes of which they are part.
They cannot be friends. Finally constructor functions cannot be virtual, but destructor functions can because of the restrictions and differences between function overloading and virtual function redefinition, the term overriding is used to describe virtual function redefinition by a derived class.
This differs from overloading a normal function, in which return types and the number and type of parameters may differ. (In fact, when you overload a function, either the number or the type of the parameters must differ! It is through these differences that C++ can select the correct version of an overloaded function).
However, when a virtual function is redefined, all aspects of its prototype must be the same. If you change the prototype when you attempt to redefine a virtual function, the function will simply be considered overloaded by the C++ compiler, and its virtual nature will be lost. Another important restriction is that virtual functions must be nonstatic members of the classes of which they are part.
They cannot be friends. Finally constructor functions cannot be virtual, but destructor functions can because of the restrictions and differences between function overloading and virtual function redefinition, the term overriding is used to describe virtual function redefinition by a derived class.
(1)
Anusha said:
1 decade ago
Whats the diff b/w overloading and over ridding?
Satyanarayana said:
1 decade ago
Virtual functions concept is used with the concept of method over ridding in case of inheritance but not in case of overloading. It will be used to have more functionalities in case of inheritance if super and child class have same function name.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers