0

Function used before being declared?

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" why don't we get an error here, because the function setName is used before being declared.

1st Jun 2018, 2:37 PM
Kasul Kliff
Kasul Kliff - avatar
1 Odpowiedź
0
The language is cpp I think which is compiled all at once , the error ur talking about we get outside the classes try something like same in main(); u will get error but in classes it is all interconnected
23rd Jun 2025, 11:17 AM
RISHABH
RISHABH - avatar