When I read c++ code, I keep seeing code of the form "ClassName ClassName::FunctionName." For example:
Fraction Fraction:: Reciprocal() {
return Fraction(denominator,numerator);
}
In above Fraction is a class name and Reciprocal() is a function declared inside fraction class and there is a constructor with two parameters. What is the significant of using this format?
You should read up on how methods and functions work. The above piece of code is a method of the class Fraction which returns a Fraction object.
The significance of this method is that instead of modifying itself, it can instead modify a new Fraction and output that, leaving itself exactly the same.
Using methods like these are only really useful in a mathematical context, like with Matrices or Trees, and it is so you can perform mathematical operations on certain objects without actually modifying the object.
so that means that the First Fraction indicates the return type just like void, double, int etc.... you are returning Fraction, and the second Fraction is the let the compiler know the function Reciprocal() is the one from the Fraction class, that's it, don't be trussed by the way it looks, it is simple as that, if you want to read more up on it try to get the "absolute c++" it is a good book.
Since you are returning a
Fraction
object:The return type is:
Since
Reciprocal()
method is insideFraction
class you are writing the class that the method belongs to followed by the::
operator to access it:There's nothing significant or special about this combination. It has no special meaning as a whole, as you seem to believe. It is an ordinary everyday run-of-the-mill member function definition, not different in any significant way from any other C++ member function definition.
Its meaning follows from the basic rules of C++ language. This
Is a definition of method
Reciprocal
from classFraction
. The return type of this method isFraction
. So, a method of classFraction
returns an instance of the same classFraction
- there's nothing weird, unusual or special in that.The rest of the definition simply constructs that instance as
Fraction(denominator,numerator)
and returns it. Again, this is a ordinary everyday way to create a temporary instance of classFraction
.So, to answer your question: there's nothing "significant" about this "format". There's no special "format" here of any kind, just a bunch of simple C++ features, completely unrelated to each other. Everything is very plain and basic. Any basic C++ book will have an explanation of these features.
P.S. One can note that a method like this is a good candidate for being declared
const
but it is a separate issue.
In C++, if you implement a member function outside of the class definition, then you need to indicate (in addition to the return type and the argument types) that the function you're writing is indeed a member function. If you instead wrote
then C++ would interpret this as if you'd written a free function (not a member function) named
Reciprocal
. On the other hand, writingtells C++ that you're implementing something nested inside of
Fraction
.The
::
notation is called the scope resolution operator. Think of it like a / in the path to a file. If you think ofFraction
as a folder andReciprocal
, the member function, as a file in that folder, then you can read the above code as