Method Overloading

We have already seen a form of method overloading, whereby the constructor was overloaded with parameters to provide default values. This concept is simply applied to other methods of a class to provide method overloading, which is the same as function overloading but within a class.

 

Overload resolution is the term given to the process of determining the appropriate overloaded method, and relies upon the data type, order and quantity of parameters passed to the method.

 

The overloaded methods all have the same name but will have different signatures, as per their specific functionality.

 

For instance, consider three add() methods:

 

add(int, int) {...code body...}  //requires two int parameters

 

add(float, float) {...code body...}  //requires two float parameters

 

add(string, string) {...code body...}  //requires two string parameters

 

The compiler will select the correct method to be acted upon, according to the data type of parameters sent.

 

This very simple example shows a method being overloaded according to the data being sent to it:

#include <iostream>
#include <string>
#include <typeinfo>
using namespace std;

class classA {
public:
	void display(int myVar){cout << "Passed in value is: " << myVar << endl ;}
	void display(double myVar){cout << "Passed in value is: " << myVar << endl ;}
	void display(string myVar){cout << "Passed in value is: " << myVar << endl ;}
} ;

int main() {
	classA helga ;
	helga.display(5) ;
	helga.display(17.3634) ;
	helga.display("Amazing") ;

	return 0 ;
}

Compile & Run:

Passed in value is: 5
Passed in value is: 17.3634
Passed in value is: Amazing

 

 

 

A variation, showing the constructor being overloaded:

#include <string>
#include <iostream>
using namespace std;

class Rider {
private:
	int age, wins ;
	string name, bike, nationality ;

public:
	Rider ( ) ;                 //default constructor
	Rider ( string ) ;          //overloaded constructor
	Rider ( string, string ) ;  //overloaded constructor
	Rider ( string, int, string, int, string ) ;  //overloaded constructor
	~Rider() ; //destructor

	string getName() { return name; }
	int getAge() { return age; }
	string getBike() { return bike ; }
	int getChampionships() { return wins; }
	string getNationality() { return nationality; }
} ;

Rider::Rider ( ) {                   //default constructor method definition
	name = "Derrick" ;
	age = 49 ;
	bike = "Ducati 900SS" ;
	wins = 0 ;
	nationality = "English" ;
}

Rider::Rider (string name) {         //overloaded constructor method definition
	this -> name = name ;
	age = 43 ;
	bike = "pillion" ;
	wins = 0 ;
	nationality = "American" ;
}

Rider::Rider (string name, string nationality) {  //overloaded constructor method definition
	this -> name = name ;
	age = 43 ;
	bike = "Ex World SuperBike" ;
	wins = 3 ;
	this -> nationality = nationality ;
}
//overloaded constructor method definition
Rider::Rider ( string name, int age, string bike, int wins, string nationality) {
	this -> name = name ;
	this -> age = age ;
	this -> bike = bike ;
	this -> wins = wins ;
	this -> nationality = nationality ;
}

Rider::~Rider () {  //destructor method definition
	cout << "The object with " << nationality << " nationality has been destroyed!" << endl ;
}

void display(Rider &rider){
	cout << rider.getName() << " is a "  << rider.getAge() << " year old " ;
	cout << rider.getNationality() << " " << rider.getBike() << " rider, who has won " ;
	cout << rider.getChampionships() << " world championships!" << endl ;
}

int main() {

	Rider derrick ;
	Rider *myPtr = &derrick ;
	display(*myPtr) ;

	Rider vanessa ( "Vanessa" ) ;
	myPtr = &vanessa ;
	display(*myPtr) ;

	Rider bayliss ( "Troy Bayliss", "Aussie") ;
	myPtr = &bayliss ;
	display(*myPtr) ;

	Rider rossi ( "Valentino Rossi", 33, "Moto GP", 9, "Italian") ;
	myPtr = &rossi ;
	display(*myPtr) ;

	return 0 ;
}

Compile & Run:

Derrick is a 49 year old English Ducati 900SS rider, who has won 0 world championships!
Vanessa is a 43 year old American pillion rider, who has won 0 world championships!
Troy Bayliss is a 43 year old Aussie Ex World SuperBike rider, who has won 3 world championships!
Valentino Rossi is a 33 year old Italian Moto GP rider, who has won 9 world championships!
The object with Italian nationality has been destroyed!
The object with Aussie nationality has been destroyed!
The object with American nationality has been destroyed!
The object with English nationality has been destroyed!

Leave a Reply