class Cake
{
private string name;
private int price;
public Cake(int price, string cakeName)
{
this.name = cakeName;
this.price = price;
//init stuff
}
public Cake(int price):this(price, "Cheesecake")
{
//init other stuff
}
}
Unfortunately there's no way to do this in good old C++, but there are two common ways for simulating a similar behavior:
1) Combine two (or more) constructors via default/optional parameters:
class Cake
{
private:
string name;
int price;
public:
//combines Cake() and Cake("whatever")
Cake(int price, string name = "Cheesecake Giovacchia")
{
this->name = name;
this->price = price;
}
}
2) Use an Init method to share common code:
class Cake
{
private:
int price;
string name;
init(int price, string name)
{
//do crazy common stuff with price and name
}
public:
Cake(int price)
{
this->init(price, "Cheesecake Giovacchia");
}
Cake(int price, string cakeName)
{
this->init(price, cakeName);
}
}
That's about it, I prefer the first solution (the second kinda sucks) but if anyone knows better I am all ears.
I am done sucking for today.