Using namespace

Is the use of using namespace std fine in these situations?

#include<iostream>
#include<string>
#include<cmath>

using namespace std;

class Point;

class Point
{
private:
double _x;
double _y;
double _d;

public:
Point(double x = 0, double y = 0);
double getX() const;
double getY() const;
Point & setX(double x); // mutator, returning reference to self
Point & setY(double y);
const Point & output() const;
};

double distance(double x1, double y1, double x2, double y2);
double area(double a, double b, double c);

int main()
{
double x1, x2, x3, y1, y2, y3;
double d1 , d2 , d3 ;


cout << "Input Point 1: \n";
cout << "x: ";
cin >> x1;
cout << "y: ";
cin >> y1;
cout << "Input Point 2: \n";
cout << "x: ";
cin >> x2;
cout << "y: ";
cin >> y2;
cout << "Input Point 3: \n";
cout << "x: ";
cin >> x3;
cout << "y: ";
cin >> y3;

cout << "\n";
cout << "Point 1: \n";
Point point1(x1, y1);
point1.output();
cout << "Point 2: \n";
Point point2(x2, y2);
point2.output();
cout << "Point 3: \n";
Point point3(x3, y3);
point3.output();

d1 = distance(x1, y1, x2, y2);
d2 = distance(x1, y1, x3, y3);
d3 = distance(x2, y2, x3, y3);


area(d1, d2, d3);




return 0;
}

Point::Point(double x, double y) : _x(x), _y(y)
{
setX(x);
setY(y);
}
double Point::getX() const
{
return _x;
}
double Point::getY() const
{
return _y;
}
Point & Point::setX(double x) // mutator, returning reference to self
{
_x = x;
return *this;
}
Point & Point::setY(double y)
{
_y = y;
return *this;
}
const Point & Point::output() const
{
cout << "Point: \n";
cout << "(" << _x << ", " << _y << ")\n\n";
return *this;
}
double distance(double x1, double y1, double x2, double y2)
{
return sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2) * 1.0);
}
double area(double a, double b, double c)
{
double s;
double area;
s = (a + b + c) / 2;
area = sqrt(s * (s - a) * (s - b) * (s - c));
cout << "Area: " << area;
return area;
}
Many will say not to use "using namespace std;" since it can cause naming conflicts later. For small code and coding assignments, it's likely going to be fine. But best not to get into the habit since it can cause issues and, to some employers, may make them see you as a newbie.

It's also worth considering to use this instead:

1
2
using namespace std::cout;
using namespace std::cin;
Not using using namespace std; is not a status symbol. If only it was.

There are real reasons for not using it and understanding why, in addition to program size and habit, is important.

A reasonable (among many) rendition of the situation is: https://www.geeksforgeeks.org/using-namespace-std-considered-bad-practice/

Topic archived. No new replies allowed.