0

Can anybody else explain flow of program (I mean, internal operations how it selects, jumps, assigns,etc)??

class myClass { public: myClass(string nm) { setName(nm); } void setName(string x) { name = x; } string getName() { return name; } private: string name; }; int main() { myClass ob1("David"); myClass ob2("Amy"); cout << ob1.getName(); } //Outputs "David"

8th Apr 2018, 1:12 PM
Vinay M
Vinay M - avatar
1 Respuesta
+ 10
'myClass ob1("David");' is equal to 'myClass ob1 = new myClass("David")' which calls the constructor 'myClass' from inside that class and passes the string 'David' which in turn is then passed to 'ob1.setName('David')' which sets 'ob1.name' to 'David'. Same steps for the second object. 'ob1.getName()' simply returns the 'ob1.name' property which is inacessible directly because it's private.
8th Apr 2018, 1:28 PM
Valen.H. ~
Valen.H. ~ - avatar