// overload3.cpp By: Aiman Hanna - ©1993-2006 Aiman Hanna // This program illustrates constructor overloading. // Key Points: 1) constructor overloading #include "overload3.h" MathOperations::MathOperations( void ) // constructor { ires = 0; dres1 = dres2 = 0; } MathOperations::MathOperations( int i1 ) // overloaded constructor { ires = i1; dres1 = dres2 = 0; } MathOperations::MathOperations( int i1, double d1 ) // overloaded constructor { ires = i1; dres1 = dres2 = d1; } MathOperations::MathOperations( int i1, double d1, double d2 ) // overloaded constructor { ires = i1; dres1 = d1; dres2 = d2; } double MathOperations::Add( void ) { // Rreturn the total value of the object attributes return ires + dres1 + dres2; } int main() { // Create 4 objects of the class. Notice that a different constructor is // executing for each of these objects. MathOperations m1, m2(20), m3(10, 35.2), m4(30, 32.7, 45.9); cout << "\n The total value of m1 attributes is: " << m1.Add() << endl; cout << "\n The total value of m2 attributes is: " << m2.Add() << endl; cout << "\n The total value of m3 attributes is: " << m3.Add() << endl; cout << "\n The total value of m4 attributes is: " << m4.Add() << endl; return 0; } // The result of running the program /* The total value of m1 attributes is: 0 The total value of m2 attributes is: 20 The total value of m3 attributes is: 80.4 The total value of m4 attributes is: 108.6 */