Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1)
- #include <iostream>
- using namespace std;
- class Room {
- public:
- Room() { }
- virtual void displayRoomInfo() = 0;
- virtual void displayRoomPrice() = 0;
- virtual ~Room() { }
- };
- class StandardRoom : public Room
- {
- private:
- bool hasBathroom;
- public:
- StandardRoom()
- {
- this->hasBathroom = false;
- }
- StandardRoom(bool hasBathroom)
- {
- this->hasBathroom = hasBathroom;
- }
- void displayRoomInfo()
- {
- cout << "This is a standard room with a queen-sized bed";
- if(hasBathroom) cout << " and a bathroom";
- cout << ".\n";
- }
- void displayRoomPrice()
- {
- cout << "The price for a standard room is $";
- if(hasBathroom) cout << "100";
- else cout << "80";
- cout << " per night.\n";
- }
- ~StandardRoom() { }
- };
- class DeluxeRoom : public Room {
- bool hasBalcony;
- public:
- DeluxeRoom() { this->hasBalcony = false; }
- DeluxeRoom(bool hasBalcony) { this->hasBalcony = hasBalcony; }
- void displayRoomInfo() {
- cout << "This is a deluxe room with a king-sized bed, a bathroom, a mini-fridge";
- if(hasBalcony) cout << " and a balcony";
- cout << ".\n";
- }
- void displayRoomPrice() {
- cout << "The price for a deluxe room is $";
- if(hasBalcony) cout << "200";
- else cout << "160";
- cout << " per night.\n";
- }
- ~DeluxeRoom() { }
- };
- int main() {
- Room* rooms[5];
- int testCase;
- cin >> testCase;
- if (testCase == 1) {
- cout << "TEST CASE 1: TESTING STANDARD ROOM CLASS" << endl;
- for (int i = 0; i < 5; i++) {
- rooms[i] = new StandardRoom();
- rooms[i]->displayRoomInfo();
- rooms[i]->displayRoomPrice();
- }
- } else if (testCase == 2) {
- cout << "TEST CASE 2: TESTING DELUXE ROOM CLASS" << endl;
- for (int i = 0; i < 5; i++) {
- rooms[i] = new DeluxeRoom();
- rooms[i]->displayRoomInfo();
- rooms[i]->displayRoomPrice();
- }
- } else {
- cout << "TEST CASE 3: TESTING BOTH CLASSES" << endl;
- for (int i = 0; i < 5; i++) {
- if (i % 2) {
- bool hasBalcony;
- cin >> hasBalcony;
- rooms[i] = new DeluxeRoom(hasBalcony);
- }
- else {
- bool hasBathroom;
- cin >> hasBathroom;
- rooms[i] = new StandardRoom(hasBathroom);
- }
- rooms[i]->displayRoomInfo();
- rooms[i]->displayRoomPrice();
- }
- }
- return 0;
- }
- -------------------------------------------------------------------------------------------------------------------------------
- 2)
- #include <iostream>
- #include <cstring>
- using namespace std;
- class RCPlane
- {
- protected:
- char *model;
- char proizvoditel[50];
- float osnovnatezhina;
- public:
- RCPlane()
- {
- this->model=new char[0];;
- }
- RCPlane(char *model,char *proizvoditel,float osnovnatezhina)
- {
- this->model=new char[strlen(model)+1];
- strcpy(this->model,model);
- strcpy(this->proizvoditel,proizvoditel);
- this->osnovnatezhina=osnovnatezhina;
- }
- virtual float calculateTotalWeight()=0;
- virtual void printInfo()=0;
- virtual ~RCPlane()
- {
- delete []model;
- }
- };
- class RCRacingPlane : public RCPlane
- {
- private:
- int brservo;
- public:
- RCRacingPlane():RCPlane() {}
- RCRacingPlane(char *model,char *proizvoditel,float osnovnatezhina,int brservo) : RCPlane(model,proizvoditel,osnovnatezhina)
- {
- this->brservo=brservo;
- }
- float calculateTotalWeight()
- {
- if(brservo>4)
- {
- int temp=brservo-4;
- return osnovnatezhina +9*temp;
- }
- else
- {
- return osnovnatezhina;
- }
- }
- void printInfo()
- {
- cout<<model<<", "<<proizvoditel<<", "<<brservo<<" - "<<calculateTotalWeight()<<endl;
- }
- ~RCRacingPlane() {}
- };
- class RCGlider : public RCPlane
- {
- private:
- bool sobiranje_p;
- public:
- RCGlider():RCPlane() {}
- RCGlider(char *model,char *proizvoditel,float osnovnatezhina,bool sobiranje_p) : RCPlane(model,proizvoditel,osnovnatezhina)
- {
- this->sobiranje_p=sobiranje_p;
- }
- float calculateTotalWeight()
- {
- if(sobiranje_p)
- {
- // osnovna+=osnovna+20;
- return osnovnatezhina +20;
- }
- else
- {
- return osnovnatezhina;
- }
- }
- void printInfo()
- {
- cout<<model<<", "<<proizvoditel<<", "<<sobiranje_p<<" - "<<calculateTotalWeight()<<endl;
- }
- ~RCGlider() {}
- };
- float heaviestRacingPlane(RCPlane *p[], int n)
- {
- float max=p[0]->calculateTotalWeight();
- for(int i=0;i<n;i++)
- {
- if(p[i]->calculateTotalWeight()>max)
- {
- max=p[i]->calculateTotalWeight();
- }
- }
- return max;
- }
- int main()
- {
- int n;
- //cout << "Enter the number of planes: ";
- cin >> n;
- RCPlane* planes[n];
- for (int i = 0; i < n; ++i)
- {
- char type[30];
- char model[30];
- char manufacturer[30];
- float basicWeight;
- int numberOfServos;
- bool foldingPropeller;
- //cout << "Enter plane type (RCRacingPlane/RCGlider): ";
- cin >> type;
- //cout << "Enter model: ";
- cin >> model;
- //cout << "Enter manufacturer: ";
- cin >> manufacturer;
- //cout << "Enter basic weight: ";
- cin >> basicWeight;
- if (strcmp(type, "RCRacingPlane") == 0)
- {
- //cout << "Enter numberOfServos: ";
- cin >> numberOfServos;
- planes[i] = new RCRacingPlane(model, manufacturer, basicWeight, numberOfServos);
- }
- else if (strcmp(type, "RCGlider") == 0)
- {
- //cout << "Does it have folding propeller (0/1): ";
- cin >> foldingPropeller;
- planes[i] = new RCGlider(model, manufacturer, basicWeight, foldingPropeller);
- }
- }
- RCPlane* heaviestPlane = nullptr;
- float maxValue = heaviestRacingPlane(planes, n);
- for (int i = 0; i < n; ++i)
- {
- if (planes[i]->calculateTotalWeight() == maxValue)
- {
- heaviestPlane = planes[i];
- break;
- }
- }
- if (heaviestPlane != nullptr)
- {
- cout << "Heaviest RCPlane:\n";
- heaviestPlane->printInfo();
- }
- // Deallocate memory
- for (int i = 0; i < n; ++i)
- {
- delete planes[i];
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment