+ 2

Solve it, please.

How can this code work?? #include <iostream> #include <string> using namespace std; class Hello { public: void Celeb(string x) { cout<< x << endl; string x = "Very thanks for 200 followers!" } public: string x; }; int main() { Hello obj; obj(); return 0; };

18th Oct 2019, 9:43 PM
__V__
__V__ - avatar
7 Réponses
+ 4
you have to call the method with the . (dot) operator. No clue why people downvoted AJ || ANANT. He's right. You use the object to reference the method. Instead of obj() , you would write obj.Celeb(); but you'd have to pass a string parameter with it. Not sure what you're trying to do, but to access a class method you must use the . operator in conjunction with the object. Example: class Student { private: testScore = 0; public: void setScore(int x) { testScore = x; } } int main() { Student student_object; student_object.setScore(100); return 0; }
19th Oct 2019, 9:26 PM
Stephen Matthew
Stephen Matthew - avatar
+ 3
Stephen Matthew I dont know why. I think they know better than me who downvoted.
19th Oct 2019, 10:10 PM
A͢J
A͢J - avatar
+ 2
You created object but not calling method.
18th Oct 2019, 10:06 PM
A͢J
A͢J - avatar
+ 2
Yes, what AJ said. If you create a method with a parameter, you must pass a parameter with it when you call it.
21st Oct 2019, 4:12 PM
Stephen Matthew
Stephen Matthew - avatar
+ 1
Ilove Bts Because you didn't pass the parameter in setHello() function from outside. Pass parameter in setHello function. Then it will work.. #include <iostream> #include <string> using namespace std; class Hello{ private: string fr_hel; string se_hel; public: void setHello(string t) { cout << t << endl; } }; int main() { Hello obj; obj.setHello("AJ"); return 0; }; It will print AJ
21st Oct 2019, 12:47 PM
A͢J
A͢J - avatar
0
What do u meant in this program dude!?
18th Oct 2019, 10:30 PM
SilentxKnight
SilentxKnight - avatar
0
Ilove Bts Check this tutorial. Then you will know how to call object. https://www.google.com/amp/s/www.geeksforgeeks.org/c-classes-and-objects/amp/
19th Oct 2019, 10:12 PM
A͢J
A͢J - avatar