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