Ôîðóì » C/C++ äëÿ íà÷èíàþùèõ (C/C++ for beginners) » RFID container problem with add() » Îòâåòèòü

RFID container problem with add()

BasicNewbie1: [pre2]#include "ShippingContainer.h" #include "ManualShippingContainer.h" class RFIDShippingContainer : public ShippingContainer{ // derived class for RFID method of inventorying the container private: string content;// for the content of the item int quantity;// for the quantity of the added items. public: RFIDShippingContainer ();// constructors string getContent();// input string of the content. int add( string );// add the item, say add one crate of pears void setManifest( string ); // set the manifest virtual string getManifest(); // return the string of manifest }; RFIDShippingContainer ::RFIDShippingContainer(){ content =""; quantity = 0; } void RFIDShippingContainer :: setManifest( string theManifest ){ content = theManifest; } string RFIDShippingContainer :: getContent(){ return content; } string RFIDShippingContainer :: getManifest(){ cout<< content<<endl; return content; } int RFIDShippingContainer :: add( string thecontent ){ content = thecontent; /*quantity ++; return quantity;*/ }[/pre2]

Îòâåòîâ - 18

BasicNewbie1: for my shipping container [pre2]#include <iostream> #include <string> #ifndef SHIPPINGCONTAINER_H #define SHIPPINGCONTAINER_H using namespace std; class ShippingContainer{ private: int containerID; public: //ShippingContainer (int containerID); ShippingContainer(){ containerID = 0; }// constructors void setContainerID( int );//mutator int getContainerID();//accessor string IntToString(int i);// convert the ID integers to a string virtual void setManifest( string ) = 0; virtual string getManifest();//virtual function to output the empty string as the content of the shipping container. }; void ShippingContainer :: setContainerID( int thecontainerID ){ containerID = thecontainerID; } int ShippingContainer :: getContainerID(){ return containerID; } string ShippingContainer :: getManifest(){ //virtual function to output the empty string as the content of the shipping container. //cout<< ""; return("");// return an empty string. } #endif [/pre2]

BasicNewbie1: and my manual container [pre2]#include "ShippingContainer.h" #ifndef MANUALSHIPPINGCONTAINER_H #define MANUALSHIPPINGCONTAINER_H class ManualShippingContainer : public ShippingContainer{ // derived class for manual method of inventorying the container private: string Content; public: ManualShippingContainer ();// constructors void setManifest( string ); virtual string getManifest();//output the empty string as the content of the shipping container. string getContent();// input string of the content. }; void ManualShippingContainer :: setManifest(string theManifest ){ Content = theManifest; } ManualShippingContainer ::ManualShippingContainer(){ Content =""; } string ManualShippingContainer :: getContent(){ return Content; } string ManualShippingContainer :: getManifest(){ return Content; } #endif [/pre2]

BasicNewbie1: main.cpp [pre2]/* Name : Lim Boon Jye Student ID : 4372463 Modification Date : 1 October 2012 Purpose program : */ #include "ShippingContainer.h" #include "ManualShippingContainer.h" #include "RFIDShippingContainer.h" int main(){ ManualShippingContainer theManualShippingContainer; RFIDShippingContainer theRFIDShippingContainer; ShippingContainer *theShippingContainer[6] ; theShippingContainer[0] = new ManualShippingContainer; theShippingContainer[1] = new ManualShippingContainer; theShippingContainer[2] = new ManualShippingContainer; theShippingContainer[3] = new RFIDShippingContainer; theShippingContainer[4] = new RFIDShippingContainer; theShippingContainer[5] = new RFIDShippingContainer; theShippingContainer[0]->setContainerID(5); theShippingContainer[0]->setManifest("Manual Shipping Manifest"); theShippingContainer[1]->setContainerID(10); theShippingContainer[1]->setManifest("Manual Shipping Manifest"); theShippingContainer[2]->setContainerID(15); theShippingContainer[2]->setManifest("Manual Shipping Manifest"); theShippingContainer[3]->setContainerID(20); theShippingContainer[3]->setManifest("crate of apple"); theShippingContainer[3]->setManifest("crate of apple"); theShippingContainer[3]->setManifest("crate of apple"); theShippingContainer[3]->setManifest("crate of apple"); theShippingContainer[3]->setManifest("crate of oranges"); theShippingContainer[3]->setManifest("crate of oranges"); theShippingContainer[3]->setManifest("crate of oranges"); theShippingContainer[3]->setManifest("crate of bananas"); theShippingContainer[4]->setContainerID(25); theShippingContainer[4]->setManifest("crate of apples"); theShippingContainer[5]->setContainerID(30); theShippingContainer[5]->setManifest("crate of oranges"); theShippingContainer[5]->setManifest("crate of oranges"); theShippingContainer[5]->setManifest("crate of apples"); for(int i = 0; i < 6; i++){ int temp = theShippingContainer->getContainerID(); cout << "Container " << temp << " manifest: " << theShippingContainer->getManifest() << ", #" << temp << endl; } system( "pause" );//System Pause return 0; }[/pre2]


BasicNewbie1: sorry and here is my question .. [pre2]To model this application, write a base class called ShippingContainer that has a container ID number as an integer. Include member functions to set and access the ID number. Add a virtual function called getManifest that returns an empty string. The purpose of this function is to return the contents of the shipping container. Create a derived class called ManualShippingContainer that represents the manual method of inventorying the container. In this method, a human simply attaches a textual description of all contents of the container. For example, the description might be “4 crates of apples, 10 crates of pears.” Add a new class variable of type string to store the manifest. Add a function called setManifest that sets this string. Override the getManifest function so that it returns this string. Create a second derived class called RFIDShippingContainer that represents the RFID method of inventorying the container. To simulate what the RFID chips would compute, create an add function to simulate adding an item to the container. The class should store a list of all added items (as a string) and their quantity using the data structures of your choice. For example, if the add function are invoked three times as follows: rfidContainer.add(“crate of pears”); //add one crate of pears rfidContainer.add(“crate of apples”); //add one crate of apples rfidContainer.add(“crate of pears”); //add one crate of pears At this point, the data structure should be storing a list of two items: crate of apples and crate of pears. The quantity of apples is one and the quantity of pears is two. Override the getManifest function so that it returns a string of all items that is built by traversing the list of items. In the above example, the return string would be “2 crate of pears, 1 crate of apples.” Include other necessary functions, constructors, and destructor to all the classes above. Finally, write a main program that creates an array of pointers to 6 ShippingContainer objects. The array should be used to store both the ManualShippingContainer objects and RFIDShippingContainer objects. Construct the main program to allow the user to decide the number of each type of objects to be stored in the array. Instantiate all the objects accordingly and demonstrate how each objects behaves differently when the same instructions are given to them. For the ManualShippingContainer objects, you will have to invoke setManifest function to set the contents. For the RFIDShippingContainer objects, you will have to invoke add to set the contents (although, if this were real, the contents of the container would “add” themselves via the RFID chips instead of requiring a human to type them in). [/pre2]

Ñûðîåæêà: I have not understood what is the question? For the present I could make some remarks on your base class ShippingContainer. First of all you shall not define member functions in a header file if the definitions are outside the class and the functions have no the function speciifier inline. It would be better to define the default constructor as [pre2] ShippingContainer() : containerID( 0 ) { }[/pre2] Also you need a virtual destructor. The accessor shall have qualifier const [pre2]int getContainerID() const; [/pre2] It is not clear why does the following class member function string IntToString(int i); have the parameter? Also it is not clear what is the manifest because the class has no such a member

BasicNewbie1: sorry . the function for string IntToString(int i) ; i just delete it , unused function. i having the problem to pass the quantity if the add() function return same content [pre2]rfidContainer.add(“crate of pears”); //add one crate of pears rfidContainer.add(“crate of apples”); //add one crate of apples rfidContainer.add(“crate of pears”); //add one crate of pears [/pre2] having problem at here . what should i put inside my destructor ? just delete the allocation memory?

BasicNewbie1: https://www.dropbox.com/s/i0on46z3ittlmii/CSCI204-Assignment2-July2012.docx here is the link for the question. i scare that my coding will be complicated due the font size at here. i upload the files that .h and .cpp. kindly have a look for it.. https://www.dropbox.com/sh/0zdpc3dygou6kax/zNlm4biokg here is the files i already tried my best , and i hope i get understand with it. and i really tried hard. i not sure whether my main.cpp coding is correct or not. just hope someone will help me . sorry for it ..

Ñûðîåæêà: I have read the assignment at last.:) And now I have a question. Should you use the standard container std::list or build yourself a list of some data structure? It is obvious that the most derived class has to have some sort of list. And the method 'add' has to add to the list a new string if there is no such a string in the list. Otherwise it has to increase the count of the string.

BasicNewbie1: struct list{ int quantity string content } isnt like this? then hw to add to the list if thr no string? cnt get it and sorting for wat d?

Ñûðîåæêà: It is not a list. List is such data organization when elements are linked together. That means that the corresponding data structure shall contain a member (for a single linked list) or two members (for a double linked list) that stores a pointer to the next element or to the next and previous elements.

BasicNewbie1: realy no idea to it, for my ques i have to use link list ady? because i though my ques oni cover inheritance and virtual function oni... other al nt yet learn..

Ñûðîåæêà: If you want to write your own list instead of using std::list then the structure should look something as [pre2]struct list{ lisr *next; int quantity; string content; }; [/pre2]

BasicNewbie1: okay, so nw hw to move to the add function? with crate of apple and orange d . pas the content parameter . and hw to count the quantity ?

Ñûðîåæêà: Well, I wrote the assignment but next time I will never do that because it takes much time. [pre2] #ifndef SHIPPINGCONTAINER_H #define SHIPPINGCONTAINER_H #include <string> class ShippingContainer { public: ShippingContainer() : containerID( 0 ) {} virtual ~ShippingContainer() {} void setContainerID( int theContainerID ) { containerID = theContainerID; } int getContainerID() const { return ( containerID ); } virtual std::string getManifest() const { return ( "" ); } private: int containerID; }; #endif // SHIPPINGCONTAINER_H[/pre2] [pre2] #ifndef MANUALSHIPPINGCONTAINER_H #define MANUALSHIPPINGCONTAINER_H #include <string> #include "ShippingContainer.h" class ManualShippingContainer : public ShippingContainer { public: ManualShippingContainer() {} ~ManualShippingContainer() {} std::string getManifest() const { return ( manifest ); } void setManifest( const std::string &theManifest ) { manifest = theManifest; } private: std::string manifest; }; #endif // MANUALSHIPPINGCONTAINER_H[/pre2] [pre2] #ifndef RFIDSHIPPINGCONTAINER_H #define RFIDSHIPPINGCONTAINER_H #include <string> #include "ShippingContainer.h" class RFIDShippingContainer : public ShippingContainer { public: RFIDShippingContainer() : manifestList( 0 ) {} ~RFIDShippingContainer() { list *current = manifestList; while ( current ) { list *tmp = current; current = current->get_next(); delete tmp; } manifestList = 0; } std::string getManifest() const { std::string theManifest; list *current = manifestList; while ( current ) { if ( !theManifest.empty() ) theManifest += ", "; theManifest += current->toString(); current = current->get_next(); } return ( theManifest ); } void add( const std::string &theManifest ) { list *item = find( theManifest ); if ( item == 0 ) { item = new list( theManifest, manifestList ); manifestList = item; } else { ++( *item ); } } private: struct list { public: list( std::string theManifest, list *theNext = 0 ) : manifest( theManifest ), quantity( 1 ), next( theNext ) {} list * get_next() const { return ( next ); } bool operator ==( const std::string &theManifest ) const { return ( manifest == theManifest ); } list & operator ++() { ++quantity; return ( *this ); } std::string toString() const { return ( std::to_string( ( long long )quantity ) + " " + manifest ); } private: list *next; int quantity; std::string manifest; }; list *manifestList; list * find( const std::string &theManifest ) const { list *current = manifestList; while ( current && !(*current == theManifest ) ) current = current->get_next(); return ( current ); } }; #endif // RFIDSHIPPINGCONTAINER_H[/pre2] [pre2] #include "stdafx.h" #include <iostream> #include "ShippingContainer.h" #include "ManualShippingContainer.h" #include "RFIDShippingContainer.h" int _tmain(int argc, _TCHAR* argv[]) { const size_t TOTAL_CONTAINERS = 6; ShippingContainer *sc[TOTAL_CONTAINERS]; RFIDShippingContainer *RFID_item = new RFIDShippingContainer(); RFID_item->add( "crate of pears" ); RFID_item->add( "crate of apples" ); RFID_item->add( "crate of pears" ); sc[0] = RFID_item; ManualShippingContainer *Manual_item = new ManualShippingContainer(); Manual_item->setManifest( "5 crate of apples " ); sc[1] = Manual_item; for ( size_t i = 0; i < 2; i++ ) { std::cout << sc[ i ]->getManifest() << std::endl; } delete RFID_item; delete Manual_item; return 0; }[/pre2] Some notes on the code. I wrote the code by using MS VC++ 2010. It has its own peculirities. For example precompiled header #include "stdafx.h" or the definition of main. If you use another compiler you should remove the header and change the definition of main. Also this compiler has a bug so I used the following type casting std::to_string( ( long long )quantity ) You need not do the same if your compiler supports the new family of functions to_string. That is you may write simply std::to_string( quantity ) In the main I did not fill the whole array. I only set two elements of it to demonstrate that the program is working. You should rewrite the body of the main according to your assignment.

BasicNewbie1: ya i knw, next time i jus hope u guide me, if cnt understand thn oni guide me, or just provid a simply cod wil do i hope to dne myself and easily get understand tq

Ñûðîåæêà: BasicNewbie1 Take into account that copy constructors and copy assignment operators were omited though they are required. You can either declare them as deleted or as private members of the classes to suppress copying and assignment objects.

BasicNewbie1: bt for tis i hav to use boolean operator ? wat mean for take into accoun ? and i thought oni private data and public data in clas? stgl havd delete datd? for my knwledge, purpose of delete is to deallocate the memory .

Ñûðîåæêà: Where did you found the boolean operator? The third class has comparison operator to find a list item that contains required manifest. As for the private access control then methods including constructors, destructors and overload operators also can be private. As for the deleted copy assignment operator and copy constructor then you can declare them as deleted. If you want you can define the copy constructor and copy assignment operator for the third class of your assignment yourself or write simply [pre2] private: RFIDShippingContainer( const RFIDShippingContainer & ); RFIDShippingContainer & operator = ( const RFIDShippingContainer & );[/pre2]



ïîëíàÿ âåðñèÿ ñòðàíèöû