+ 3
Error token
What will be the output of this code? #include <iostream> using namespace std; class Calc{ public: int a(int x, int y){return x+y;} int b(int x, int y){return x-y;} int c(int x, int y){return x*y;} int d(int x, int y){return x/y;} }; int main() { cout<<Calc.c(2,2); return 0; }
3 ответов
+ 3
The output is an instantiation error.
Your code should be something like the following:
#include <iostream>
class Calc {
public:
int a(int x, int y){return x+y;}
int b(int x, int y){return x-y;}
int c(int x, int y){return x*y;}
int d(int x, int y){return x/y;}
};
int main() {
Calc calculate;
std::cout << calculate.c(2,2);
return 0;
}
i.e., you must actually instantiate a Calc object (being calculate).
+ 2
Edin Hajdarevic you don't get that you can't just say Calc.c() without instantiation of a Calc object?
C++ expects a primary-expression before the '.' token, hence the need for an object.
+ 1
I don't see why it's an error? I guess the output would be just 4, since 2*2 is 4. And I didn't get what are you talking about, Default