i am going to show you what i want in code cause its kinda hard to explain...
would i use a constructor for that or what?Code:class whatever { int int1; }; class whatever2:whatever { //is it possible to define the default value of int1 here? };
This is a discussion on help with class varialbe definition.. within the C++ Programming forums, part of the General Programming Boards category; i am going to show you what i want in code cause its kinda hard to explain... Code: class whatever ...
i am going to show you what i want in code cause its kinda hard to explain...
would i use a constructor for that or what?Code:class whatever { int int1; }; class whatever2:whatever { //is it possible to define the default value of int1 here? };
A default value, yes. You can do this many different ways:
With that you can create an object of type whatever2 and they will have a default value of 0 if you don't specify an argument when you instantiate the object or whatever value you do pass during instantiation:Code:class whatever { int int1; }; class whatever2 : public whatever { public: whatver2(int value = 0) : int1(value) { } };
Code:whatever2 foo1; // foo1.int1 = 0 whatever2 foo2(0); // foo2.int1 = 0 whatever2 foo3(10); // foo3.int1 = 10
I used to be an adventurer like you... then I took an arrow to the knee.
hk_mp5kpdw's example won't work because int1 is private in the base class. If it was public (usually bad design) then it would work. Another solution would be to have the base class constructor take a value for int1, and then call the base class constructor.Code:class whatever { int int1; public: whatever(int value = 0) : int1(value) }; class whatever2 : public whatever { public: whatever2() : whatever(5) { } };