Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Widget Class with dynamic memory and pointers
#include <iostream> #include <stdio.h> #include <sstream> using namespace std; class WIDGET { int IDNum; int PARTNum; string Name; public: WIDGET(){IDNum = 0;PARTNum = 0;} ~WIDGET(){} void setIDNum(int ID){IDNum = ID;} void setPARTNum(int PN){PARTNum = PN;} void setName(string N){Name = N;} int getIDNum() const{return IDNum;} int getPARTNum() const{return PARTNum;} string getName() const{return Name;} }; int main(){ WIDGET *Inventory[3]; WIDGET *pWidget; for (int i=0; i<3; i++) { string Name = "NULL"; pWidget = new WIDGET; pWidget->setIDNum(i +1); pWidget->setPARTNum(2001); cout << "Enter Widget Name: " << endl; //prompt user for name cin.clear(); //cin.flushall(); getline(cin,Name); pWidget->setName(Name); Inventory[i] = pWidget; } for (int i=0; i<3; i++) { cout << "Widget #" << i+1 << " ID Number: " << Inventory[i]->getIDNum() << endl; cout << "Widget #" << i+1 << " Part Number: " << Inventory[i]->getPARTNum() << endl; cout << "Widget #" << i+1 << " Name: " << Inventory[i]->getName() << endl; } for (int i=0; i<3; i++) { cout << "Deleting #" << Inventory[i]->getIDNum() << endl; delete Inventory[i]; Inventory[i] = NULL; } return 0; }
run
|
edit
|
history
|
help
0
Policy based smart pointer
Base conversion
3
cppPyClamInher
CodeChef P2 - FIZZA
ContainerVector2
HeapSort
Dar
variadic template
fibonacci