Skip to content

Instantly share code, notes, and snippets.

@XoLinA
Created March 10, 2026 20:43
Show Gist options
  • Select an option

  • Save XoLinA/13d7754e04b24ce9acd057f596213763 to your computer and use it in GitHub Desktop.

Select an option

Save XoLinA/13d7754e04b24ce9acd057f596213763 to your computer and use it in GitHub Desktop.
Вступ до патернів проектування
#include <iostream>
#include <string>
using namespace std;
class Transport
{
public:
virtual void input() = 0;
virtual void info() = 0;
};
class Truck : public Transport
{
private:
string model;
int capacity;
public:
void input() override
{
cout << "Enter truck model: ";
cin >> model;
cout << "Enter truck capacity: ";
cin >> capacity;
}
void info() override
{
cout << "Truck model: " << model << endl<<"Capacity: " << capacity << " t." << endl;
}
};
class Ship : public Transport
{
private:
string name;
int crew;
public:
void input() override
{
cout << "Enter ship name: ";
cin >> name;
cout << "Enter crew size: ";
cin >> crew;
}
void info() override
{
cout << "Ship name: " << name << endl;
cout << "Crew: " << crew << endl;
}
};
class Airplane : public Transport
{
private:
string model;
int passengers;
public:
void input() override
{
cout << "Enter airplane model: ";
cin >> model;
cout << "Enter number of passengers: ";
cin >> passengers;
}
void info() override
{
cout << "Airplane model: " << model << endl;
cout << "Passengers: " << passengers << endl;
}
};
class TransportFactory
{
public:
virtual Transport* createTransport() = 0;
};
class TruckFactory : public TransportFactory
{
public:
Transport* createTransport() override
{
return new Truck();
}
};
class ShipFactory : public TransportFactory
{
public:
Transport* createTransport() override
{
return new Ship();
}
};
class AirplaneFactory : public TransportFactory
{
public:
Transport* createTransport() override
{
return new Airplane();
}
};
int main()
{
int choice;
cout << "Choose transport:" << endl<< "1 - Truck" << endl<< "2 - Ship" << endl<< "3 - Airplane" << endl;
cin >> choice;
TransportFactory* x = nullptr;
if (choice == 1)
x = new TruckFactory();
else if (choice == 2)
x = new ShipFactory();
else if (choice == 3)
x = new AirplaneFactory();
else
{
cout << "EEEEEEEERRROOOORRRRR" << endl;
return 0;
}
Transport* transport = x->createTransport();
transport->input();
transport->info();
delete transport;
delete x;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment