Antonio_Lab11
unknown
c_cpp
2 years ago
3.7 kB
8
Indexable
#include <iostream> #include <cstring> using namespace std; const int StrLength = 30; class CarryingTransport { protected: char route[30]; int speed; int serviceCost; CarryingTransport *next; CarryingTransport *prev; static CarryingTransport *last; public: CarryingTransport(char *route, int speed, int serviceCost); void AddElement(); static void DisplayList(); static void DeleteList(); virtual void Print() = 0; virtual ~CarryingTransport() {}; }; CarryingTransport::CarryingTransport(char *route, int speed, int serviceCost) { strcpy(this->route, route); this->speed = speed; this->serviceCost = serviceCost; } void CarryingTransport::AddElement() { if(last != 0) { this->prev = last; this->next = this; } last = this; } void CarryingTransport::DisplayList() { CarryingTransport *current = last; current->Print(); while(current != 0) { current = current->prev; current->Print(); } } void CarryingTransport::DeleteList() { CarryingTransport *current = last; while(current != 0) { last = current->prev; delete current; current = last; } } CarryingTransport * CarryingTransport::last = 0; class Plane : public CarryingTransport { private: int flightHeight; int weightCapacity; char productionCountry[25]; public: Plane(int flightHeight, int weightCapacity, char *productionCountry, char *route, int speed, int serviceCost) : CarryingTransport(route, speed, serviceCost) { this->flightHeight = flightHeight; this->weightCapacity = weightCapacity; strcpy(this->productionCountry, productionCountry); } void Print() { cout << "Cargo plane: " << route << ", weight capacity: " << weightCapacity << " tons" << ", flight height: " << flightHeight <<" km" << ", production country: " << productionCountry << endl; } }; class Truck : public CarryingTransport { private: bool isDiesel; int wheelsCount; int truckBodyVolume; public: Truck(bool isDiesel, int wheelsCount, int truckBodyVolume, char *route, int speed, int serviceCost) : CarryingTransport(route, speed, serviceCost) { this->isDiesel = isDiesel; this->wheelsCount = wheelsCount; this->truckBodyVolume = truckBodyVolume; } void Print() { cout << "Truck: " << route << ", is diesel: " << boolalpha << isDiesel << ", wheels count: " << wheelsCount << ", truck body volume: " << truckBodyVolume << " m2"<< endl; } }; class Train : public CarryingTransport { private: int tanksCount; bool isFuelOriented; char fuelUsed[20]; public: Train(int tanksCount, bool isFuelOriented, char *fuelUsed,char *route, int speed, int serviceCost) : CarryingTransport(route, speed, serviceCost) { this->tanksCount = tanksCount; this->isFuelOriented = isFuelOriented; strcpy(this->fuelUsed, fuelUsed); } void Print() { cout << "Cargo train: " << route << ", tanksCount: " << tanksCount << ", is fuel oriented: " << boolalpha << isFuelOriented << ", fuel used: " << fuelUsed << endl; } }; int main() { (new Plane(10, 880, "Belarus", "Belarus - Russia", 8000, 1000000))->AddElement(); (new Truck(true, 12, 880, "Minsk - Mogilev", 80, 5600))->AddElement(); (new Train(40, false, "coal", "Belarus - Lithuania", 200, 56000))->AddElement(); CarryingTransport::DisplayList(); CarryingTransport::DeleteList(); return 0; }
Editor is loading...