#include <iostream> #include <string> using namespace std; class Box { public:
Box(int h,int w,int l):height(h),width(w),length(l){} int volume( ){return
height*width*length;}; private: static int height; // Static data members int width; int
length; }; int main() { Box b(2,3,4); cout<<"volume is "<<b.volume()<<endl;
return 0; }
Find out what's wrong with this program ?
error 1 error C2438: “height”: Static class data cannot be initialized through a constructor
Static data members cannot be assigned as normal data members through normal member functions , It needs to be assigned separately outside the class “int Box::height=2;”
#include <iostream> #include <string> using namespace std; class Box { public:
Box(int w,int l):width(w),length(l){} int volume( ){return
height*width*length;}; private: static int height; // Static data members int width; int
length; };int Box::height=2; int main() { Box b(3,4); cout<<"volume is
"<<b.volume()<<endl; return 0; }
Technology
Daily Recommendation