C++ overloaded constructors (#23) 🤯

preview_player
Показать описание
C++ overloaded constructors tutorial
C++ overloaded constructor example
Рекомендации по теме
Комментарии
Автор

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

class Pizza {
public:
string bread;
string sauce;
string cheese;
string topping;

Pizza(string bread, string sauce, string cheese, string topping) {
this->bread = bread;
this->sauce = sauce;
this->cheese = cheese;
this->topping= topping;
}
Pizza(string bread, string sauce, string cheese) {
this->bread = bread;
this->sauce = sauce;
this->cheese = cheese;
}
Pizza(string bread, string sauce) {
this->bread = bread;
this->sauce = sauce;
}
Pizza(string bread) {
this->bread = bread;
}

void orderPizza() {
cout << "here is your ";
if (!bread.empty())
cout << bread << " ";
if (!sauce.empty())
cout << sauce << " ";
if (!cheese.empty())
cout << cheese << " ";
if (!topping.empty())
cout << topping << " ";
cout << "pizza" << endl;
}
};

int main()
{
//constructor = special function that is automatically called when an object is instantiated
//useful for assigning arguments to variables

Pizza first_pizza("thick crust", "red sauce", "mozzerella");
Pizza second_pizza("flat bread", "alfredo");

first_pizza.orderPizza();
second_pizza.orderPizza();
}

BroCodez
Автор

(assuming using namespace std is mentioned previously):
class TowerColors{
public:
string floor1;
string floor2;
string floor3;
string floor4;
TowerColors(){
}
TowerColors(string floor1, string floor2, string floor3, string floor4){
this->floor1=floor1;
this->floor2=floor2;
this->floor3=floor3;
this->floor4=floor4;

}
TowerColors(string floor1, string floor2, string floor3){
this->floor1=floor1;
this->floor2=floor2;
this->floor3=floor3;


}
TowerColors(string floor1, string floor2){
this->floor1=floor1;
this->floor2=floor2;
}

TowerColors(string floor1){
this->floor1=floor1;
;

}
void showColors(){
";
";
";
";
}};

Sayne
Автор

Bro cover more topics polymorphism, inheritance, STL etc

shaamidrees
Автор

Hi i finished all your C++ videos so what should i learn for next language?

shude_kun
Автор

Hey bro, I wanted to ask you this, I have learned java to intermediate level (can even use javafx) and as you can see have gotten through all your basic c++ vids (which was easy because it's super similar to java). My goal is to learn programming well enough to be able to code video games, maybe in unreal engine (preferably in c++), so what should I do next, and how long do you think it will take me?
Great work on all your vids btw, this channel was literally salvation in my computer science class, the internet needs more people like you.

arturosukovich
Автор

Are there any more things after this other than GUI like you did with java or python, if yes then will you make them.

adityarajsrivastava