SPECIFICATION: .......... #include <iostream> #include <sstream> //## class CDesktopPC class CDesktopPC { //// Constructors and destructors //// public : CDesktopPC(const std::string & man, const std::string proc, int ram, int disk); CDesktopPC(); ~CDesktopPC(); //// Operations //// int get_disk_size() const; std::string get_manufacturer() const; std::string get_processor() const; int get_ram_size() const; void set_disk_size(int ds); void set_manufacturer(const std::string manu); void set_processor(const std::string pro); void set_ram_size(int rs); virtual std::string to_string() const; //// Attributes //// private : int disk_size; //## attribute disk_size std::string manufacturer; //## attribute manufacturer std::string processor; //## attribute processor int ram_size; //## attribute ram_size }; ........... IMPLEMENTATION: ........... #include "CDesktopPC.h" CDesktopPC::CDesktopPC(const std::string & man, const std::string proc, int ram, int disk) : manufacturer(man),processor(proc),ram_size(ram),disk_size(disk) {} CDesktopPC::CDesktopPC() {} CDesktopPC::~CDesktopPC() { std::cout<<"Destructor of CDesktopPC."<<std::endl;} int CDesktopPC::get_disk_size() const {return disk_size;} std::string CDesktopPC::get_manufacturer() const { return manufacturer;} std::string CDesktopPC::get_processor() const {return processor;} int CDesktopPC::get_ram_size() const { return ram_size;} void CDesktopPC::set_disk_size(int ds) { disk_size=ds;} void CDesktopPC::set_manufacturer(const std::string manu) {manufacturer=manu;} void CDesktopPC::set_processor(const std::string pro) {processor=pro;} void CDesktopPC::set_ram_size(int rs) {ram_size=rs;} std::string CDesktopPC::to_string() const { //#[ operation to_string() const std::ostringstream sb; sb<<"manufacturer: "<<manufacturer <<"\nCPU: "<<processor <<"\nRAM: "<<ram_size<< " megabytes" <<"\nDisk: "<<disk_size<<" gigabytes"; return sb.str(); }
SPECIFICATION: ......... #include "CDesktopPC.h" class CNotebook : public CDesktopPC { //// Constructors and destructors //// public : CNotebook(const std::string & proc, int ram, int disk, double screen, double wei); CNotebook(const std::string & man, const std::string & proc, int ram, int disk, double screen, double wei); CNotebook(); ~CNotebook(); //// Operations //// double get_screen_size() const; double get_weight() const; void set_size(double sz); void set_weight(double we); std::string to_string() const; //// Attributes //// static const char * DEFAULT_LT_MAN; //## attribute DEFAULT_LT_MAN private : double screen_size; //## attribute screen_size double weight; //## attribute weight }; ....... IMPLEMENTATION: ....... #include "CNotebook.h" const char * CNotebook::DEFAULT_LT_MAN("H MARKA MOU"); CNotebook::CNotebook(const std::string & proc, int ram, int disk, double screen, double wei) : CDesktopPC(DEFAULT_LT_MAN,proc,ram,disk),screen_size(screen),weight(wei) {} CNotebook::CNotebook(const std::string & man, const std::string & proc, int ram, int disk, double screen, double wei) : CDesktopPC(man,proc,ram,disk),screen_size(screen),weight(wei) {} CNotebook::CNotebook() {} CNotebook::~CNotebook() {} double CNotebook::get_screen_size() const { return screen_size;} double CNotebook::get_weight() const { return weight;} void CNotebook::set_size(double sz) { screen_size=sz;} void CNotebook::set_weight(double we) { weight=we;} std::string CNotebook::to_string() const { std::ostringstream sb; sb<<CDesktopPC::to_string() <<"\nScreen size: "<<screen_size <<"\nWeight: "<<weight; return sb.str(); }
SPECIFICATION: ........ #include <stdexcept> template <typename Item_Type> class CVectorTemplate { //// Constructors and destructors //// public : CVectorTemplate(); virtual ~CVectorTemplate(); //// Attributes //// private : static size_t INITIAL_CAPACITY; //## attribute INITIAL_CAPACITY size_t current_capacity; //## attribute current_capacity size_t num_items; //## attribute num_items Item_Type* the_data; //## attribute the_data public : void erase(size_t index); //Remove an entry based on its index void insert(size_t index, const Item_Type& the_value); // Insert an entry to the data inserting it before the item at the specified index. Item_Type& operator[](size_t index); //Get a value in the vector based on its index. const Item_Type& operator[](size_t index) const; // Get a constant value in the vector based on its index. void pop_back(); //Remove the last item in the vector. void push_back(const Item_Type& the_value); //Insert a new item at the end of the vector. void reserve(size_t new_capacity); //Ensure that the capacity is at least a given size. void swap(CVectorTemplate<Item_Type>& other); //Exchanges the contents of this vector with another. CVectorTemplate(const CVectorTemplate<Item_Type>& other); //Make a copy of a CVectorTemplate. size_t CVectorTemplatesize() const; // Get the current size of the vector Item_Type& back(); //Return a reference to the last item. const Item_Type& back() const; //Return a const reference to the last item. size_t capacity(); //Get the current capacity of the vector bool emptyCVectorTemplate() const; //Determine if the vector is empty Item_Type& front(); //Return a reference to the first item const Item_Type& front() const; //Return a const reference to the first item CVectorTemplate<Item_Type>& operator=(const CVectorTemplate<Item_Type>& other); // Assign the contents of one vector to another. }; // ---------------------------------------------------------------------------- // End of CVectorTemplate.h // ---------------------------------------------------------------------------- template <typename Item_Type> size_t CVectorTemplate<Item_Type>::INITIAL_CAPACITY(10); template <typename Item_Type> CVectorTemplate<Item_Type>::CVectorTemplate() : current_capacity(INITIAL_CAPACITY),the_data(new Item_Type[INITIAL_CAPACITY]),num_items(0) {} template <typename Item_Type> CVectorTemplate<Item_Type>::CVectorTemplate(const CVectorTemplate<Item_Type>& other) : current_capacity(other.capacity),num_items(other.num_items),the_data(new Item_Type[other.current_capacity]) { for (size_t i=0;i<num_items;i++) the_data[i]=other.the_data[i]; } template <typename Item_Type> CVectorTemplate<Item_Type>::~CVectorTemplate() { delete[] the_data; } template <typename Item_Type> size_t CVectorTemplate<Item_Type>::CVectorTemplatesize() const { return num_items; } template <typename Item_Type> Item_Type& CVectorTemplate<Item_Type>::back() { if (num_items == 0) throw std::out_of_range ("Attempt to access back of empty vector"); return the_data[num_items-1]; } template <typename Item_Type> const Item_Type& CVectorTemplate<Item_Type>::back() const { if (num_items == 0) throw std::out_of_range ("Attempt to access back of empty vector"); return the_data[num_items-1]; } template <typename Item_Type> size_t CVectorTemplate<Item_Type>::capacity() { return current_capacity; } template <typename Item_Type> bool CVectorTemplate<Item_Type>::emptyCVectorTemplate() const {return num_items == 0;} template <typename Item_Type> void CVectorTemplate<Item_Type>::erase(size_t index) { // Validate index. if (index >= num_items) { throw std::out_of_range ("index to erase is out of range"); } // Move items below the removed one up. for (size_t i = index + 1; i < num_items; i++) { the_data[i - 1] = the_data[i]; } num_items--; } template <typename Item_Type> Item_Type& CVectorTemplate<Item_Type>::front() { if (num_items == 0) throw std::out_of_range ("Attempt to access front of empty vector"); return the_data[0]; } template <typename Item_Type> const Item_Type& CVectorTemplate<Item_Type>::front() const { if (num_items == 0) throw std::out_of_range ("Attempt to access front of empty vector"); return the_data[0]; } template <typename Item_Type> void CVectorTemplate<Item_Type>::insert(size_t index, const Item_Type& the_value) { // Validate index if (index > num_items) { throw std::out_of_range ("index to insert is out of range"); } // Ensure that there is space for the new item if (num_items == current_capacity) { reserve(2 * current_capacity); // allocate an expanded array } // Move data from index to num_items-1 down for (size_t i = num_items; i > index; i--) { the_data[i] = the_data[i - 1]; } // Insert the new item the_data[index] = the_value; num_items++; } template <typename Item_Type> CVectorTemplate<Item_Type>& CVectorTemplate<Item_Type>::operator=(const CVectorTemplate<Item_Type>& other) { //Make a copy of the other CVectorTemplate object. CVectorTemplate<Item_Type> the_copy(other); //above the "deep copy constructor" was used //Now we swap the contents of self with the copy(the_copy) we made before. swap(the_copy); //finally we return the object. Upon return the copy will be destroyed. return *this; } template <typename Item_Type> Item_Type& CVectorTemplate<Item_Type>::operator[](size_t index) { // Verify that the index is legal if (index >= num_items) { throw std::out_of_range ("index to operator[] is out of range"); } return the_data[index]; } template <typename Item_Type> const Item_Type& CVectorTemplate<Item_Type>::operator[](size_t index) const { // Verify that the index is legal if (index >= num_items) { throw std::out_of_range ("index to operator[] is out of range"); } return the_data[index]; } template <typename Item_Type> void CVectorTemplate<Item_Type>::pop_back() { num_items--; } template <typename Item_Type> void CVectorTemplate<Item_Type>::push_back(const Item_Type& the_value) { // Make sure there is space for the new item. if (num_items == current_capacity) { reserve(2 * current_capacity); // Allocate an expanded array } // Insert the new item. the_data[num_items] = the_value; num_items++; } template <typename Item_Type> void CVectorTemplate<Item_Type>::reserve(size_t new_capacity) { if (new_capacity > current_capacity) { if (new_capacity > 2 * current_capacity) current_capacity = new_capacity; else current_capacity *= 2; // Double the capacity. Item_Type* new_data = new Item_Type[current_capacity]; // Copy the data over for (size_t i = 0; i < num_items; i++) new_data[i] = the_data[i]; // Free the memory occupied by the old copy. delete[] the_data; // Now point to the new data the_data = new_data; } } template <typename Item_Type> void CVectorTemplate<Item_Type>::swap(CVectorTemplate<Item_Type>& other) { std::swap(num_items, other.num_items); std::swap(current_capacity, other.current_capacity); std::swap(the_data, other.the_data); } ........
SPECIFICATION: ........ #include "CNotebook.h" //## dependency CNotebook #include "vect1int.h" //## link itsVect1int #include "vect2int.h" //## link itsVect2int #include "vect3float.h" //## link itsVect3float class VectorOfNotebooks; //## link itsVectorOfNotebooks class bindingclass; //## link itsBindingclass class CTesterClass { //// Constructors and destructors //// public : CTesterClass(); ~CTesterClass(); //// Operations //// //## operation testCVectorTemplateClass() void testCVectorTemplateClass(); //// Additional operations //// //## auto_generated bindingclass* getItsBindingclass() const; //## auto_generated void setItsBindingclass(bindingclass* p_bindingclass); //## auto_generated vect1int* getItsVect1int() const; //## auto_generated vect2int* getItsVect2int() const; //## auto_generated vect3float* getItsVect3float() const; //## auto_generated VectorOfNotebooks* getItsVectorOfNotebooks() const; //## auto_generated void setItsVectorOfNotebooks(VectorOfNotebooks* p_VectorOfNotebooks); protected : //## auto_generated void cleanUpRelations(); //// Relations and components //// bindingclass* itsBindingclass; //## link itsBindingclass vect1int itsVect1int; //## link itsVect1int vect2int itsVect2int; //## link itsVect2int vect3float itsVect3float; //## link itsVect3float VectorOfNotebooks* itsVectorOfNotebooks; //## link itsVectorOfNotebooks }; ........ IMPLEMENTATION: ........ #include "CTesterClass.h" #include "bindingclass.h" //## link itsBindingclass #include "VectorOfNotebooks.h" //## link itsVectorOfNotebooks CTesterClass::CTesterClass() { itsBindingclass = NULL; itsVectorOfNotebooks = NULL; testCVectorTemplateClass(); } CTesterClass::~CTesterClass() { cleanUpRelations(); } void CTesterClass::testCVectorTemplateClass() { vect1int vect1; //gemise ton vect1 me 20 stoixeia ta poia periexoun tous akeraious 0 ws 19 for (int i = 0; i < 20; i++) { vect1.push_back(i); } bool pass1 = true; //tsekare to index tou vect1 for (int i = 0; i < 20; i++) { if (i != vect1[i]) { std::cout << i << " != vect1[" << i << "] == "<< vect1[i] << '\n'; pass1 = false; } } if (pass1) std::cout << "Index test passed\n"; std::cout<<"the last item of vect1int vect1 is" <<vect1.back()<<std::endl; std::cout<<"the first item of vect1int vect1 is" <<vect1.front()<<std::endl; //Check Out of range negative index bool passed1b = false; try { vect1[-1] = 0; } catch (std::out_of_range& ex) { passed1b = true; std::cout << "Out of range negative index test passed\n"; } if (!passed1b) std::cout << "Out of range negative index test failed\n"; //check Out of range index too large bool passed1c = false; try { vect1[vect1.CVectorTemplatesize()] = 0; } catch (std::out_of_range& ex) { passed1c = true; std:: cout << "Out of range index too large test passed\n"; } if (!passed1c) std::cout << "Out of range index too large test failed\n"; ///////////______________________________________________/////////////// vect2int vect2; //gemise ton vect2 me 20 stoixeia ta poia einai:0,2,4,6,8,10,12,14,16,18 for (int i = 0; i < 20; i += 2) { vect2.push_back(i); } for (int i = 0; i < 20; i += 2) { vect2.insert(i+1, i+1); } bool pass2 = true; //tsekare to index tou vect for (int i = 0; i < 20; i++) { if (i != vect2[i]) { std::cout << i << " != vect2[" << i << "] == "<< vect2[i] << '\n'; pass2 = false; } } if (pass2) std::cout << "Insert test passed\n"; std::cout<<"the last item of vect2int vect2 is" <<vect2.back()<<std::endl; std::cout<<"the first item of vect2int vect2 is" <<vect2.front()<<std::endl; ///////////______________________________________________/////////////// vect3float vect3; //gemise ton vect3 me 20 stoixeia ta poia einai:0.0,2.0,4.0,6.0,8.0,10.0,12.0,14.0,16.0,18.0 for (float i = 0.0; i < 20.0; i += 2.0) { vect3.push_back(i); } vect3.pop_back(); std::cout<<"the last item of vectfloat vect3 is" <<vect3.back()<<std::endl; ///////////______________________________________________/////////////// itsBindingclass= new bindingclass; //gemise ton vect4 me 20 stoixeia ta poia einai:0,2,4,6,8,10,12,14,16,18 for (int i = 0; i < 20; i += 2) { itsBindingclass->push_back(i); } itsBindingclass->pop_back(); itsBindingclass->pop_back(); std::cout<<"the last item of vectfloat vect4 is" <<itsBindingclass->back()<<std::endl; ///////////______________________________________________/////////////// itsVectorOfNotebooks = new VectorOfNotebooks; ///--------------///MY PROBLEM: ///--------------// ///////////////all next three statements cause a compile error //itsVectorOfNotebook->insert(new CNotebook("HP nwer", "Intel P9", 2028, 120,15.4,4.0)); //itsVectorOfNotebook->insert(CNotebook("HP nwer", "Intel P9", 2028, 120,15.4,4.0)); //itsVectorOfNotebook.insert(new CNotebook("HP nwer", "Intel P9", 2028, 120,15.4,4.0)); ///////////______________________________________________/////////////// ///////////______________________________________________/////////////// //CDesktopPC c1; CDesktopPC Mycomputer("HP", "Intel P4", 2028, 120); //CNotebook c3("Ace", "AMD Athlon 2000", 512, 60); CNotebook YourPC("Ace", "AMD Athlon 2000", 1024, 100, 15.5, 7.5); CNotebook HerPC("intel II", 128, 20, 17.0, 12.5); //cout << c2.manufacturer << ", " << c2.processor << endl; std::cout << Mycomputer.get_disk_size() << ", " << YourPC.get_ram_size() << std::endl; std::cout <<"My Computer is:\n "<< Mycomputer.to_string() <<std::endl; std::cout <<"Your Pc is:\n "<< YourPC.to_string() << std::endl; std::cout <<"Her Pc is:\n "<< HerPC.to_string() << std::endl; //sel196 virtual functions CDesktopPC* PC1; CDesktopPC* Laptop1; PC1= new CDesktopPC ("Asus","AMD3",256,40); Laptop1= new CNotebook ("Fujitsu","intel 3",1000,60,15.4,6.0); std::cout <<"PC1 is:\n "<< PC1->to_string() << std::endl; std::cout <<"Laptop1 is:\n "<< Laptop1->to_string() << std::endl; ///////////______________________________________________/////////////// bindingclass* CTesterClass::getItsBindingclass() const { return itsBindingclass;} void CTesterClass::setItsBindingclass(bindingclass* p_bindingclass) { itsBindingclass = p_bindingclass;} vect1int* CTesterClass::getItsVect1int() const { return (vect1int*) &itsVect1int;} vect2int* CTesterClass::getItsVect2int() const { return (vect2int*) &itsVect2int;} vect3float* CTesterClass::getItsVect3float() const { return (vect3float*) &itsVect3float;} VectorOfNotebooks* CTesterClass::getItsVectorOfNotebooks() const { return itsVectorOfNotebooks;} void CTesterClass::setItsVectorOfNotebooks(VectorOfNotebooks* p_VectorOfNotebooks) { itsVectorOfNotebooks = p_VectorOfNotebooks; } void CTesterClass::cleanUpRelations() { if(itsBindingclass != NULL) { itsBindingclass = NULL; } if(itsVectorOfNotebooks != NULL) { itsVectorOfNotebooks = NULL; } }
SPECIFICATION: ...... #include "CNotebook.h" //## dependency CNotebook #include "CVectorTemplate.h" //## class VectorOfNotebooks class VectorOfNotebooks : public CVectorTemplate<CNotebook> { //// Constructors and destructors //// public : VectorOfNotebooks(); ~VectorOfNotebooks(); }; ...... IMPLEMENTATION: ...... #include "VectorOfNotebooks.h" VectorOfNotebooks::VectorOfNotebooks() {} VectorOfNotebooks::~VectorOfNotebooks() {} ......
typedef CVectorTemplate<CNotebook> VectorOfNotebooks;
itsVectorOfNotebooks = new VectorOfNotebooks;
itsVectorOfNotebook->push_back(new CNotebook("HP nwer", "Intel P9", 2028, 120,15.4,4.0)); itsVectorOfNotebook.push_back(new CNotebook("HP nwer", "Intel P9", 2028, 120,15.4,4.0)); itsVectorOfNotebook->insert(0,new CNotebook("HP nwer", "Intel P9", 2028, 120,15.4,4.0)); itsVectorOfNotebook->insert(0,CNotebook("HP nwer", "Intel P9", 2028, 120,15.4,4.0));
Building ------------ CVectorTemplateComponent.exe ------------ Executing: "C:\aThesisEnv\Telelogic\Rhapsody 7.2\Share\etc\mingwmake.bat" CVectorTemplateComponent.mak build Setting environment for MinGW "make.exe" Compiling CTesterClass.cpp CTesterClass.cpp: In member function `void CTesterClass::testCVectorTemplateClass()': CTesterClass.cpp:68: warning: passing negative value `-0x000000001' for converting 1 of `Item_Type& CVectorTemplate<Item_Type>::operator[](size_t) [with Item_Type = int]' CTesterClass.cpp:135: error: `itsVectorOfNotebook' undeclared (first use this function) CTesterClass.cpp:135: error: (Each undeclared identifier is reported only once for each function it appears in.) make: *** [CTesterClass.o] Error 1 Build Done
itsVectorOfNotebook in one or more places (one of which is line 135 of CTesterClass.cpp) where you should have written itsVectorOfNotebooks .
itsVectorOfNotebooks = new VectorOfNotebooks; itsVectorOfNotebooks->push_back(CNotebook("HP nwer", "Intel P9", 2048, 120,15.4,4.0)); itsVectorOfNotebooks->insert(0,CNotebook("HP nwer2", "Intel P10", 2048, 340,15.4,3.0));
....... try { 68: vect1[-1] = 0; } catch ........
VectorOfNotebooks = new VectorOfNotebooks; itsVectorOfNotebooks->push_back(CNotebook("HP nwer", "Intel P9", 2048, 120,15.4,4.0)); itsVectorOfNotebooks->insert(0,CNotebook("HP nwer2", "Intel P10", 2048, 340,15.4,3.0));
| DaniWeb Message | |
| Cancel Changes | |