Show: Object Pascal C++
Display Preferences

Overloading Constructors

From Appmethod Topics
Jump to: navigation, search

Go Up to Constructors Index

Constructors can be overloaded, allowing objects to be created, depending on the values being used for initialization.

class X {
   int    integer_part;
   double double_part;
public:
   X(int i)    { integer_part = i; }
   X(double d) { double_part = d; }
};
int main()
{
   X one(10);   // invokes X::X(int) and sets integer_part to 10
   X one(3.14); // invokes X::X(double) setting double_part to 3.14
   return 0;
}

See Also

Personal tools
In other languages