+ 2
What is the sequence of the execution in this code what will execute first and what will execute later.
#include <iostream> #include <string> using namespace std; class myClass { public: void setName(string x) { name = x; } string getName() { return name; } private: string name; }; int main() { myClass myObj; myObj.setName("John"); cout << myObj.getName(); return 0; }
5 Answers
+ 2
In this program , the as called with object the following is sequence
1. main()
2. setName() is called with 'John' as input
3. getName() is called which returns 'John'.
4. again main()
The control is back and program is finished right in main.
+ 2
Ya you are right! Nagy
+ 1
So the program begins by creating the object.
So the program start at main
And then whenever i call a function he goes back and search of the function that i called to execute it.
Did i thing right
+ 1
Nice, thank you bro
0
first execution begins from main() function:-
-> myClass myObj;
myObj.setName("John");/* which goes to
void setName(string x) {
name = x;
}
which get name "John" and then control back to
myObj.setName("John"); */ then statement
cout << myObj.getName(); /* controls goes to
string getName() {
return name;
} */
which return name "John" and then control back to main()
and prints name "John",
I hope this will help you.