- 3

What is difference between 1 and 2 and why I am not getting the output in case 2??

1.include <iostream> #include<string> using namespace std; class my { public: void no(string x) { name = x; } string getyes() { return name; } private: string name; }; int main() { my z; z.no("No utput"); cout << z.getyes(); return 0; } 2.#include <iostream> #include<string> using namespace std; class my { public: void no(string x) { x = name; } string getyes() { return name; } private: string name; }; int main() { my z; z.no("No utput"); cout << z.getyes(); return 0; }

7th Jul 2021, 8:44 AM
Shahir
Shahir - avatar
4 Respuestas
+ 4
In 2 nd one you are assigning value to a local variable , i .e . x
7th Jul 2021, 8:49 AM
Abhay
Abhay - avatar
+ 4
Shaik.Shahir do you realise what assigning a value means ? And how value is assigned to left side operand instead of right side one . You never assigned value to name in 2nd case , how can you expect an output ?
7th Jul 2021, 8:53 AM
Abhay
Abhay - avatar
+ 1
Hooo!got it
7th Jul 2021, 8:55 AM
Shahir
Shahir - avatar
0
But why I am not getting the output in case 2??
7th Jul 2021, 8:50 AM
Shahir
Shahir - avatar