overloaded constructors are when we have multiple constructors with the same name but different parameters they allow for a varying number of arguments when instantiating an object here's an example we'll create pizza objects from a pizza class let's type class pizza add a set of curly braces a semicolon to the end we'll make our members publicly accessible so pizzas they can have a variable number of toppings right you can have no toppings one topping two toppings three toppings suppose we have just one topping to begin with topping one and that will be of the string data
type standard string topping one then i will create a pizza constructor it has the same name as the class name pizza parentheses curly braces we'll set up some parameters standard string topping one in order to construct a pizza we have to pass in one topping so let's do that we have pizza pizza one and i will pass in one argument for a topping i feel like pepperoni then we'll just need to assign this argument to this attribute this arrow topping one equals topping one the name of this parameter then to test this let's display topping
one standard pizza one dot topping one okay we have pizza one that contains pepperoni now what if you would like a pizza that has two toppings suppose we have pizza two pizza pizza two this time i'm going to attempt to pass in two arguments how about mushrooms and peppers well when i run this program we're going to have an error error no matching function for call to pizza with two arguments we can create an additional constructor that accepts different arguments let's copy what we have and paste it this constructor will take two string arguments topping
one and topping two so we have two toppings this time and i should probably create another attribute for a second topping standard string topping two this topping two equals topping two so now with pizza two let's display topping one as well as topping two now pizza 2 contains mushrooms and peppers what if you would like a pizza with no toppings like a plain cheese pizza well we could set that up pizza pizza three i'm going to attempt to add a set of parentheses after pizza 3 but pass in no arguments but we have a warning
our compiler is saying we should get rid of the parentheses if we're passing in no arguments we don't need that set of parentheses but we still have an error here no matching function call for pizza with no arguments if we're creating an object and passing in no arguments we'll need a matching constructor so at the top i'll create a pizza constructor with no arguments and that should make that error go away so this should run and compile just fine then yep no errors so yeah those are overloaded constructors you can have multiple constructors with the
same name as long as the parameters are different we have three constructors with the same name we can create a pizza with no toppings one topping or two toppings overloaded constructors allow for multiple objects with a varying number of attributes if you're looking for some additional exercises post a class with overloaded constructors in the comments section down below and well yeah those are overloaded constructors in c plus plus