+ 1

Why it does not run?if i change to private: int n=68; it runs actually.

#include <iostream> using namespace std; class MyClass { public : MyClass(); void myPrint(); private: int n; n=69; }; MyClass::MyClass() { } void MyClass::myPrint() { cout <<n<<endl; } int main() { MyClass obj; obj.myPrint(); }

14th Aug 2018, 9:04 AM
Amit Chhetri
Amit Chhetri - avatar
3 Answers
+ 1
It's probably because n is declared and assigned on separate lines. Compiler doesn't know what to do with n=69; when it's outside a method.
14th Aug 2018, 10:23 AM
BlazingMagpie
BlazingMagpie - avatar
+ 1
Vivek Mishra kya bol raha hai bhaisab? mera MyClass () and void myPrint () methods are set in public already but the variable n is initialized in private . this program doesnt run. however if i declare and initialized n in the same line as private,it runs. why?
14th Aug 2018, 10:25 AM
Amit Chhetri
Amit Chhetri - avatar
0
BlazingMagpie exactly my thought. so we cant declare and initialized in different lines in this case.,eh??
14th Aug 2018, 10:27 AM
Amit Chhetri
Amit Chhetri - avatar