Friday 15 April 2011

c++ - Constructor is not working properly -



c++ - Constructor is not working properly -

i need help building constructor initialized respective info when instantiated within main().

#include <iostream> using namespace std; class entity{ public: int x, int y, char icon; }; int main(){ entity pdata; pdata.x=4; pdata.y=3, pdata.icon='1'; cout<<pdata.x<<'\n'\; cout<<pdata.y<<'\n'\; cout<<pdata.icon<<\'n'\; }

i included illustration of need only... there no need include program. anyways need constructor initialized info in main instance(pdata) of entity created: know constructor has like

entity::entity(int x, int y, char icon){};

and 1 time instantiated in main like

entity pdata{3,4,'1'};

but isn't working me

oh way need constructor because that's assignment asking in first place here go copied right off doc file

"write parameterized constructor entity class sets x, y, , icon, , utilize when creating instance"

actually u have not defined constructor class entity(but compiler have defined allocate memory fellow member variable of entity).

class entity { public: int x,y; char icon; entity(int _x, int _y,char _icon) { x=_x; y=_y icon=_icon; } };

int main() { entity obj(4,3,'i'); homecoming 0; }

`

c++ visual-c++

No comments:

Post a Comment