C++ CONSTRUCTORS explained easy 👷

preview_player
Показать описание
#constructor #tutorial #explained

C++ constructor tutorial example explained
Рекомендации по теме
Комментарии
Автор

#include <iostream>

class Car{
public:
std::string make;
std::string model;
int year;
std::string color;

Car(std::string make, std::string model, int year, std::string color){
this->make = make;
this->model = model;
this->year = year;
this->color = color;
}
};

int main() {

//constructor = special method that is automatically called when an object is instantiated
// useful for assigning values to attributes as arguments

Car car1("Chevy", "Corvette", 2022, "blue");
Car car2("Ford", "Mustang", 2023, "red");

std::cout << car1.make << '\n';
std::cout << car1.model << '\n';
std::cout << car1.year << '\n';
std::cout << car1.color << '\n';

return 0;
}

BroCodez
Автор

can you make a vid about the destructors as well 😔, ty

esraafadul
Автор

Love the break down. Clear and helpful

bluefyr
Автор

class game{
public:
std::string gamename;
int releasedate;
game(std::string playername, int releasedate){
this->playername=playername;

}

};

Sayne
Автор

How do you add methods to a constructer

Esau-dh
Автор

Wow another thing C++ does better than C#
Why do I say that?
C++ () standard brackets
C# {} used in a new constructor

Dazza_Doo
Автор

include namespace std;
class Jatt {
public:
string Name;
string birthplace;
string height;
Jatt(string Name, string birthplace, string height){
this->Name=Name;
this->birthplace=birthplace;
this->height=height;}
};

aashishraj
Автор

#include <iostream>

class Dish{
public:
int calories;
std::string ingridients;
int protein;
std::string taste;

Dish(int calories, std::string ingridients, int protein, std::string taste){
this->calories = calories;
this->ingridients = ingridients;
this->protein = protein;
this->taste = taste;
}


};

int main(){

Dish macandcheese(800, "Mac and Cheese", 50, "Yummy");

std::cout << macandcheese.calories << '\n';
std::cout << macandcheese.ingridients << '\n';
std::cout << macandcheese.protein << '\n';
std::cout << macandcheese.taste << '\n';


return 0;
}

danaildoganov
Автор

#include <iostream>

class Car{
public:
std::string brand = "Vehicle";
std::string model = "Model";
int year;

void drive(){
std::cout << "This vehicle is driving\n";
}
void Description(){
std::cout << "This is a " << brand << '\n';
std::cout << "The model of this vehicle is " << model << '\n';
std::cout << "It was created in " << year << '\n';
}
void Drift(){
std::cout << "Lets Go!! Its Drifting\n";
}
};
int main(){
//Ojbect similar to structs but with Specific methods
Car PersonalCar;
PersonalCar.brand = "Lamborghini";
PersonalCar.model = "Huracan";
PersonalCar.year = 2014;
Car WifeCar;
WifeCar.brand = "Honda";
WifeCar.model = "city";
WifeCar.year = 2021;

PersonalCar.Description();
PersonalCar.drive();
PersonalCar.Drift();

WifeCar.Description();
WifeCar.drive();
WifeCar.Drift();

return 0;
}

AYSMOKASHI