arrays - Accessing values stored in object c++ -
i creating new instance of object storing each instance of virtual function array.
the problem cannot value of each object 1 time create it,
to clear want each instance of floors
create homecoming value int when called. virtual function build(int a)
returns int
current output
we adding spot value 0 in our array
i need value added output
so based on current illustration should show value 5
i assume should work because of mybuilding.add(new floors(5));
complex.h
class complex { protected: int num; public: virtual void build(int a) { num = a; } };
main.cpp
#include "floors.h" int main() { building mybuilding; mybuilding.add(new floors(5)); mybuilding.build(); homecoming 0; }
floors.h
class floors : public complex { private: int quantity; public: floors(const int& init_quant) : quantity(init_quant) { } int build(floors()) { std::cout << "this floors class" << quantity << '\n'; homecoming quantity; } };
and class handling array, building.h
class building { public: static const int max_materials = 100; complex* materials[max_materials]; int num_materials; public: building() : num_materials(0) {} ~building() { for(int = 0; < num_materials; ++i) { delete materials[i]; } } bool add(complex* s) { if(num_materials >= max_materials) { homecoming false; } materials[num_materials++] = s; homecoming true; } void build() { for(int = 0; < num_materials; ++i) { materials[i]->build(i); std::cout << "we adding spot value " << << " in our array" << '\n'; } } };
at point in time, c++ becoming rusty has been 2 years since programming in professionally. going reply no 1 else has, there better/more right reply given.
you getting output seeing because of line:
std::cout << "we adding spot value " << << " in our array" << '\n';
that line saying print out value of i
, not value contained within floor object when phone call build
.
you calling build(0)
in first iteration of loop. going phone call build
function in base of operations class complex
class has homecoming type of void
. since floors
kid of complex
, method signature method build
best matches 1 takes int , returns void. in example, have created floors
object quantity
of 5 when phone call build
method, calling base of operations class build
sets num
value of 0. not able access num
value building
class due beingness marked protected
.
as bit rusty , haven't experimented it, can't tell signature of build
method in floors
class is, can guarantee isn't think is. confused want be. want take different floor
object. way beingness used expect signature looking for.
int build() { std::cout << "this floors class" << quantity << '\n'; homecoming quantity; }
in order output think looking need alter build
method in building
class (note, have alter build method in floor class match method have listed in point 3):
void build() { for(int = 0; < num_materials; ++i) { int quantity = materials[i]->build(); std::cout << "we adding spot value " << quantity << " in our array" << '\n'; } }
c++ arrays