Over-Loading was discussed in the section called “Over-Loading” in the context of general object-oriented programming and applies in the exact same way to the C++ language. In any C++ class the same method name may define many different operations depending on the parameters used. For example: 1 2 3 // Over-Loaded Methods 4 5 #include<iostream> 6 #include<string> 7 8 using namespace std; 9 10 class MyMaths 11 { 12 public: 13 virtual int add(int a, int b); 14 virtual float add(float a, float b); 15 virtual string add(string a, string b); 16 }; 17 18 int MyMaths::add(int a, int b) 19 { 20 return (a+b); 21 } 22 23 float MyMaths::add(float a, float b) { 24 return (a+b); 25 } 26 27 string MyMaths::add(string a, string b) 28 { 29 return (a+b); 30 } 31 32 main() { 33 MyMaths m; 34 35 cout << "1 + 2 = " << m.add(1,2) << endl; 36 cout << "1.5 + 2.0 = " << m.add(1.5f,2.0f) << endl; 37 cout << "one + two = " << m.add("one","two") << endl; 38 } 39 The full source code for this example is in 1 + 2 = 3 1.5 + 2.0 = 3.5 one + two = onetwo Note: A method cannot be distinguished by its return type solely, so the parameter list must differ in some way (types or number of arguments). Please be aware that this example is fundamentally flawed. If you look at the implementation of my C++ allows the programmer to associate specific functionality of a class with the standard predefined operators, such as For example, in the 1 2 // Operator Overloaded Account Example 3 4 #include<iostream> 5 #include<string> 6 7 using namespace std; 8 9 class Account{ 10 11 protected: 12 13 int accountNumber; 14 float balance; 15 string owner; 16 17 public: 18 19 Account(string owner, float aBalance, int anAccountNumber); 20 Account(float aBalance, int anAccountNumber); 21 Account(int anAccountNumber); 22 23 Account operator + (Account); 24 bool operator == (Account); 25 26 virtual void display(); 27 virtual void makeLodgement(float); 28 virtual void makeWithdrawal(float); 29 }; 30 31 Account::Account(string anOwner, float aBalance, int anAccNumber): 32 accountNumber(anAccNumber), balance(aBalance), 33 owner (anOwner) {} 34 35 Account::Account(float aBalance, int anAccNumber) : 36 accountNumber(anAccNumber), balance(aBalance), 37 owner ("Not Defined") {} 38 39 Account::Account(int anAccNumber): 40 accountNumber(anAccNumber), balance(0.0f), 41 owner ("Not Defined") {} 42 43 Account Account::operator + (Account a) 44 { 45 return Account(owner, balance + a.balance, accountNumber); 46 } 47 48 bool Account::operator == (Account a) 49 { 50 if ((a.balance == balance) && (a.owner == owner) && 51 (a.accountNumber == accountNumber)) 52 { return true; } 53 else return false; 54 } 55 56 57 void Account::display(){; 58 cout << "account number: " << accountNumber 59 << " has balance: " << balance << " Euro" << endl; 60 cout << "This account is owned by: " << owner << endl; 61 } 62 63 void Account::makeLodgement(float amount){ 64 balance = balance + amount; 65 } 66 67 void Account::makeWithdrawal(float amount){ 68 balance = balance - amount; 69 } 70 71 int main() 72 { 73 Account a = Account(10.50, 123456); 74 a.display(); 75 76 Account b = Account("Derek Molloy", 35.50, 123457); 77 b.display(); 78 79 Account c = Account("Derek Molloy", 35.50, 123457); 80 if (b == c) { cout << "b and c are equal!" << endl; } 81 82 Account d = b + a; 83 d.display(); 84 } 85 86 The full source code for this example is in In this example the C:\My Documents\My Teaching\EE402 Notes\source_notes\cpp> OperatorOverloadAccount account number: 123456 has balance: 10.5 Euro This account is owned by: Not Defined account number: 123457 has balance: 35.5 Euro This account is owned by: Derek Molloy b and c are equal! account number: 123457 has balance: 46 Euro This account is owned by: Derek Molloy C:\My Documents\My Teaching\EE402 Notes\source_notes\cpp> In the line of code: Account d = b + a; We can look at the statement as two stages. In the first stage In the second stage The reason that the In this example the These notes are copyright Dr. Derek Molloy, School of Electronic Engineering, Dublin City University, Ireland 2013-present. Please contact him directly before reproducing any of the content in any way. |