Другие языки программирования и технологии

Классы. Объекты. С++

Реализуйте класс – телефонный номер, который отдельно хранит код страны (375 для РБ), код оператора и телефонный номер.
Реализуйте методы класса для:
конструкторы по умолчанию и с параметрами, get/set для доступа к отдельным полям, деструктор, вывод номера на экран
#include <iostream>
#include <string>
class Phone {
public:
Phone() = delete;
Phone(int code_countr, int carrier_code, int phone_number) :
code_country_(code_countr),
carrier_code_(carrier_code),
phone_number_(phone_number)
{
}
int code_country()const {
return code_country_;
}
int carrier_code()const {
return carrier_code_;
}
int phone_number()const {
return phone_number_;
}
void code_country(int value) {
code_country_ = value;
}
void carrier_code(int value) {
carrier_code_ = value;
}
void phone_number(int value) {
phone_number_ = value;
}
std::string to_string()const {
auto tail = phone_number_ % 100;
auto middle = phone_number_ / 100 % 100;
auto start = phone_number_ / 10000;
std::string box = '+' + std::to_string(code_country_) + " (" + std::to_string(carrier_code_) + ") ";
box += std::to_string(start) + '-';
if (middle < 10) box += '0';
box += std::to_string(middle) + '-';
if (tail < 10) box += '0';
box += std::to_string(tail);
return box;
}
private:
int code_country_;
int carrier_code_;
int phone_number_;
friend std::ostream& operator<<(std::ostream& out, const Phone& a) {
out << '+' << a.code_country_ << a.carrier_code_ << a.phone_number_;
return out;
}
};
int main() {
int code_country = 375;
int carrier_code = 77;
int phone_number = 5557575;
Phone subscriber(code_country, carrier_code, phone_number);
std::cout << subscriber << '\n';
std::cout << subscriber.to_string() << '\n';
system("pause");
}
// Конструктор по умолчанию бывает только когда нет явной реализации конструктора с параметрами или без.
// Конструктор без параметров явно удалён, ввиду того, что телефонного номера по умолчанию в природе не бывает. Если знаете таковой, то дайте позвонить)
// Реализация деструктора бесполезна ввиду отсутствия необходимости в конструкторах выделять память в куче. Деструктор по умолчанию справится с задачей утилизации объектов Phone.
Дмитрий Попов
Дмитрий Попов
95 486
Лучший ответ