+ 1

Passing by value from main to class

The below code gives an error. Why can't zzz.c(2.2) in main() pass values to its corresponding function in class zzz?? #include <iostream> using namespace std; class zzz { public: int c(int x, int y) {return x*y;} }; int main() { cout << zzz.c(2,2); return 0; }

5th Aug 2020, 8:59 AM
Solus
Solus - avatar
4 Answers
+ 4
#include <iostream> using namespace std; class zzz { public: int c(int x, int y) {return x*y;} }; int main() { zzz z; cout << z.c(2,2); return 0; } // you forget to create an object of zzz
5th Aug 2020, 9:07 AM
minirkk
minirkk - avatar
+ 4
because you didn't decalre obj of that class. you have to decare first then you can use the function. int main(){ zzz obj; cout<<obj.c(2,2); }
5th Aug 2020, 9:11 AM
The future is now thanks to science
The future is now thanks to science - avatar
+ 3
First thing we don't call methods of a class with dot operator attached to classname In order to call the c(int,int) method you got two options either you make this method as static and then call it with the classname and scope resolution operator, ex: static int c(int x, int y) { return x*y) } cout << zzz::c(2,2); OR The second option is you create object of the zzz class and then call the method c(int, int) with that object, Ex: int main() { zzz obj; cout << obj.c(2,2); }
5th Aug 2020, 9:08 AM
ŠœŠ³. ŠšŠ½Š°ŠæšŸŒ 
ŠœŠ³. ŠšŠ½Š°ŠæšŸŒ  - avatar
+ 1
please attach your code to your description.
5th Aug 2020, 9:07 AM
The future is now thanks to science
The future is now thanks to science - avatar