What are C++ namespaces? 📛

preview_player
Показать описание
C++ why you shouldn't "using namespace std"
namespaces tutorial example explained

#using #namespace #std
Рекомендации по теме
Комментарии
Автор

#include <iostream>
namespace first{
int x = 1;
}
namespace second{
int x = 2;
}
int main() {

int x = 0;

std::cout << x;
std::cout << first::x;
std::cout << second::x;

return 0;
}

BroCodez
Автор

thank you for the warning about `using namespace std`, my teacher is telling us to always use that when writing in c++

Blackilykat
Автор

Thabk you for going over "using namespace std" and why it shouldn't be used. So many tutorials start wirh it and never explain to people what it is, and why they are using it.

spamfilter
Автор

Thank you so much for this one ..

NOW I GET THIS 🔥🔥👍👍

GaneshKumatole
Автор

Bro. You are great. Love you from the Core of my Heart ❤️❤️❤️❤️❤️❤️❤️

curiouslife
Автор

finally.. thank you for making it make sense

TheUninstaller
Автор

whatever anything before or after whatever anything I do or did or anything

ryanalnaser
Автор

its some what similiar to different object with same class

learningXode-NG
Автор

So I tried using this code

#include <iostream.

namespace first {
int x = 10;
}

int main(){
using namespace first;

int x = 5;

std::cout << x << "\n";

return 0;
}


Quick question: Does the variable get redefined in int main when you use using namespace first despite having the same variable already defined in the namespace?

aon
Автор

#include <iostream>
int main (){
std::string << comment = "im first lol" ;
std::cout << comment;
return 0;
}

stiz
Автор

namespace cnts
{
// namespace for constants
const int DAYS_PER_WEEK = 7;
const int WEEKS_PER_MONTH = 4;
const int MONTHS_PER_YEAR = 12;
const int DAYS_PER_YEAR = 365;
}

namespace msgs
{
// namespace for messages
std::string hello = "Hello there!";
std::string welcome = "Welcome!";
std::string gameOver = "GAME OVER!";
}

int main()
{
int days_per_month = cnts::DAYS_PER_WEEK * cnts::WEEKS_PER_MONTH;
std::cout << "There are " << days_per_month << " days in one month." << std::endl;

std::cout << msgs::welcome << " Gus!\n";
std::cout << msgs::gameOver << std::endl;
std::cout << msgs::hello << std::endl;

return 0;
}

gusgus