Answer to Question #4915 in C++ for Babina

Question #4915
Can any member be overloaded?
Expert's answer
Overloaded members should provide variations on the same functionality. For example, it would not be correct for a type to have two CopyTo members, where
the first member copied data to an array and the second copied data to a file. A
common use of member overloading is to provide overloads that take few or no
parameters and are easy to use. These members call overloads that are more
powerful, but also require more experience to use correctly. The easy-to-use
overloads support common scenarios by passing default values to the complex
overloads. For example, the File class provides overloads for the Open method.
The simple overload Open takes a file path and file mode. It calls the Open
overload that takes path, file mode, file access, and file share parameters, and
provides commonly used default values for the file access and file share
parameters. Developers who do not need the flexibility of the complex overload
do not have to learn about file access and sharing models before they can open a


void Write(String^ message, FileStream^ stream){}
void Write(String^ line, FileStream^ file, bool closeStream){}

#include <iostream>
using namespace std;

struct A {
virtual void f() { cout << "void A::f()" << endl; }
virtual void f(int) { cout << "void A::f(int)" << endl; }

struct B : A {
using A::f;
void f(int) { cout << "void B::f(int)" << endl; }

int main() {
B obj_B;
B* pb = &obj_B;

// operator_overloading.cpp
// compile with: /EHsc
#include <iostream>
using namespace std;

struct Complex {
Complex( double r, double i ) : re(r), im(i) {}
Complex operator+( Complex &other );
void Display( ) { cout << re << ", " << im << endl; }
double re, im;

// Operator overloaded using a member function
Complex Complex::operator+( Complex &other ) {
return Complex( re +, im + );

int main() {
Complex a = Complex( 1.2, 3.4 );
Complex b = Complex( 5.6, 7.8 );
Complex c = Complex( 0.0, 0.0 );

c = a + b;

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!


No comments. Be the first!

Leave a comment

New on Blog