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? };
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
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
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) { } };