c++ - class with float variable -
i have simple class here variable. why not return value of variable 10.5
?
output
test! -1.09356e+09
code
#include "iostream" using namespace std; class txtbin{ protected: float area; public: txtbin(); float get_area(); }; txtbin::txtbin(){ float area = 10.5; } float txtbin::get_area(){ return area; } int main(int argc, char* argv[]){ txtbin a; cout << "test! " << a.get_area() << endl; return 0; }
this creating local variable, not initializing member:
txtbin::txtbin(){ float area = 10.5; // creates variable called area isn't used. }
you should initialize member this
txtbin::txtbin() : area(10.5) { }
or perhaps directly in class if using c++11 or newer:
class txtbin{ protected: float area = 10.5; public: txtbin(); float get_area(); };
Comments
Post a Comment