COS1512 EXAM PACK
1 C STRINGS Error Correction If(str!=”STOP” If(strcmp(str,”STOP”); Str = “STOP” Strcpy(str,”STOP”); 2 RECURSION Void CountDown(int num) { If (num = 0) Cout 0 endl; Else { Cout num endl; CountDown(num-1); } } 3 POINTERS 3.1 The problem with this this code fragment is that the code deletes the pointer. Thereafter, the programmer is assigning the value to a pointer that doesn’t point to anything. The results are disastrous. 3.2 Correct the Code 3.2.1 5 : p1 = &x; 6 : p2 = &y; 3.2.2 7: cout *p1 “ “ *p2; 3.2.3 9: 30 3.3 typedef int* myPtr; myPtr = a; int size’; a = new int[size]; 4 DEFINE PRODUCT CLASS Product.h #ifndef PRODUCT_H #define PRODUCT_H #include iostream #include cstdlib #include string #include fstream JUNE 2017 P2 Page 2 of 6 using namespace std; class Product { public: Product(); ~Product(); long getNumber(); void reset(long rID, double rPrice, long rNum); void increaseRetailPrice(float incPercent); friend bool operator ==(const Product &p1, const Product &p2); friend Product operator -- (Product &P); friend istream& operator (istream& ins, Product &P); friend ostream& operator (ostream& outs, const Product &P); protected: private: long id; double price; long number; }; #endif // PRODUCT_H Product.CPP #include "Product.h" #include iostream #include cstdlib #include string using namespace std; Product::Product() { id = 0; price = 0.0; number = 0; } Product::~Product() { //dtor } long Product::getNumber() JUNE 2017 P2 Page 3 of 6 { return number; } void Product::reset(long rID, double rPrice, long rNum) { id = rID; price = rPrice; number = rNum; } void Product::increaseRetailPrice(float incPercent) { price = price + (price * incPercent); } bool operator ==(const Product &p1, const Product &p2) { if (( == ) && ( == ) && (r == r)) return true; else return false; } Product operator-- (Product &P) { --P.number; return P; } istream& operator (istream& ins, Product &P) { ins P.id P.price P.number; //ins P.price; //ins P.number; return ins; } ostream& operator (ostream& outs, const Product &P) { outs P.id endl; outs P.price endl; outs P.number endl; return outs;
Geschreven voor
- Instelling
- University of South Africa
- Vak
- COS1512 - Introduction To Programming II
Documentinformatie
- Geüpload op
- 9 november 2021
- Aantal pagina's
- 55
- Geschreven in
- 2021/2022
- Type
- Tentamen (uitwerkingen)
- Bevat
- Vragen en antwoorden
Onderwerpen
-
cos1512
-
cos1512 exam pack