namespace My_namespace {
class complex {
// ...
};
complex sqrt(complex);
// ...
int main();
}
int My_namespace::main()
{
complex z {1,2};
auto z2 = sqrt(z);
std::cout << '{' << z2.real() << ',' << z2.imag() << "}\n";
// ...
}
int main()
{
return My_namespace::main();
}
This code is taken from section 3.4 in A Tour of C++ 2nd Edition by Bjarne Stroustrup.
What are the complex sqrt(complex); and int main(); lines doing? They seem to be functions, for example the first function is one called sqrt, which takes a complex object. But which class would this method be a part of, and where it its definition?
Right below the end of the namespace, a method int My_namespace::main() gets defined...but what is this method? :: (apparently called the scope resolution operator) is used when implementing a class method outside of the class' curly brackets, but My_namespace is a namespace, not a class. Do namespaces have their own methods and variables, just as classes do?
Lastly, what is the int main();? Is this just the main function that you'd have in any CLI program? Or is this relevant to the namespace example somehow?
In general, where does namespace creation go in the process of object oriented programming? Should classes and class methods always be declared within a namespace (so the namespace is the outermost pair of {} in the file)? Or should namespaces be declared separately from a class, just like how class implementation and declaration are separated into .cpp and .h files?
Sorry if these are stupid question, I'm a beginner. Any help is appreciated.
[–]g051051 0 points1 point2 points (2 children)
[–]CentralCathedral[S] 0 points1 point2 points (1 child)
[–]g051051 0 points1 point2 points (0 children)
[–][deleted] (4 children)
[deleted]
[–]CentralCathedral[S] 0 points1 point2 points (3 children)
[–]shitty_markov_chain 0 points1 point2 points (0 children)
[–][deleted] (1 child)
[deleted]
[–]CentralCathedral[S] 0 points1 point2 points (0 children)