What is the sequence of the execution in this code what will execute first and what will execute later. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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; }

5th Jun 2020, 2:55 PM
Abdallah Nagy
Abdallah Nagy - avatar
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.
5th Jun 2020, 6:05 PM
Nikhil Maroju
Nikhil Maroju - avatar
+ 2
Ya you are right! Nagy
5th Jun 2020, 6:11 PM
Nikhil Maroju
Nikhil Maroju - avatar
+ 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
5th Jun 2020, 6:10 PM
Abdallah Nagy
Abdallah Nagy - avatar
+ 1
Nice, thank you bro
5th Jun 2020, 6:12 PM
Abdallah Nagy
Abdallah Nagy - avatar
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.
18th Jun 2020, 10:09 AM
NEEL SHAH
NEEL SHAH - avatar