+ 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(); }
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.
+ 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?
0
BlazingMagpie exactly my thought. so we cant declare and initialized in different lines in this case.,eh??