We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Do not use an access modifier (e.g.: public) in the declaration for your Calculator class.
That advice is exclusively for Java, not C++.
In C++, you can declare specific members as public, private or protected, but you never declare a class as public class Calculator.
In Java, you can.
public class Calculator makes the class accessible by code outside the package in which it resides. Skipping the public (e.g. class Calculator) makes it package-private.
In this problem, in Java, the Calculator class will be used (to create an object) by the main() method in the Solution class (which is in same package), so package-private access is enough.
By contrast, in C++, main() is a function by itself (not part of any class).
Further, in Java, the compiler will raise an error if you put a public class Dog in a file name Cat.java (it should instead be in Dog.java).
In HackerRank environment, if you declare public class Calculator, it doesn't complain about the file name (probably because then the code gets stored in a separate Calculator.java file).
However, it does complain at run-time, that the class Calculator does not contain a main method.
Error: Main method not found in class Calculator, please define the main method as:
public static void main(String[] args)
On your own machine, if you run java Calculator for a class that does not contain a main() method, you will get the same run-time error.
So it all boils down to Java-specific syntax and restrictions and the HackerRank test environment.
In C++, all you need to ensure is that the member power() is public - whether declared expilcitly public in a class, or public-by-default in a struct.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Day 17: More Exceptions
You are viewing a single comment's thread. Return to all comments →
The advice says:
That advice is exclusively for Java, not C++.
In C++, you can declare specific members as public, private or protected, but you never declare a class as
public class Calculator
.In Java, you can.
public class Calculator
makes the class accessible by code outside the package in which it resides. Skipping the public (e.g.class Calculator
) makes it package-private.In this problem, in Java, the Calculator class will be used (to create an object) by the
main()
method in the Solution class (which is in same package), so package-private access is enough.By contrast, in C++,
main()
is a function by itself (not part of any class).Further, in Java, the compiler will raise an error if you put a
public class Dog
in a file nameCat.java
(it should instead be inDog.java
).In HackerRank environment, if you declare
public class Calculator
, it doesn't complain about the file name (probably because then the code gets stored in a separateCalculator.java
file).However, it does complain at run-time, that the class Calculator does not contain a main method.
On your own machine, if you run
java Calculator
for a class that does not contain amain()
method, you will get the same run-time error.So it all boils down to Java-specific syntax and restrictions and the HackerRank test environment.
In C++, all you need to ensure is that the member
power()
is public - whether declared expilcitly public in aclass
, or public-by-default in astruct
.