+ 1
constructors?
Can there be more than 1 constructor?
1 Antwort
+ 2
yes just like function overloading can have multiple functions with same name but different arguments....constructors can also be overloaded.
......
class X {
int a,b;
public: //make sure the constructor you
//write is in public
X (){ //#1
a=0;
b=10;
}
X (int p, int q){ //#2
a=p;
b=q;
}
X (const char *str) //#3
{std::cout <<str;}
};
......
int main ()
{
.......
X x ,
y (4,-1),
z("constructor#3");
.........
}