0

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; };

10/18/2019 9:43:40 PM

Ilove Bts

11 Answers

New Answer

+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; }

+3

Stephen Matthew I dont know why. I think they know better than me who downvoted.

+2

You created object but not calling method.

+2

Yes, what AJ said. If you create a method with a parameter, you must pass a parameter with it when you call it.

+1

Stephen Matthew I tried like this but it doesn't work whatever: #include <iostream> #include <string> using namespace std; class Hello{ private: string fr_hel; string se_hel; string t; public: void setHello(string t) { cout << t << endl; } }; int main() { Hello obj; obj.setHello(); return 0; };

+1

Finally I make it work doing a similiar code with classes and object: #include <iostream> using namespace std; class String{ public: string str; public: void printName(){ cin >> str; cout << "Il tuo nome è "<< str << endl; } }; int main() { String obj; obj.printName(); return 0; }

+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

0

What do u meant in this program dude!?

0

@AJ || ANANT || AC || ANANY || AY Can you make me an example,plz??

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/

0