Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //main.cpp
- #include <iostream>
- #include <string>
- #include <vector>
- #include "Hotel.h"
- template<typename T>
- T getNumberFromCin(std::string,
- T min = std::numeric_limits<T>().min(),
- T max = std::numeric_limits<T>().max()
- );
- int main()
- {
- std::vector<Hotel> hotels;
- int count = 3;
- for (int i = 0; i < count; i++)
- {
- std::string name;
- int cost;
- std::cout << "Enter Hotel name >>> ";
- std::getline(std::cin, name);
- cost = getNumberFromCin("Enter Hotel cost >>> ", 0, 25000);
- hotels.push_back(Hotel(name, cost));
- }
- for (auto hotel : hotels) {
- std::cout << hotel << std::endl;
- }
- system("pause");
- return 0;
- }
- template<typename T>
- T getNumberFromCin(std::string str, T min, T max) {
- T num;
- std::cout << str;
- std::cin >> num;
- while (num < min || num > max || std::cin.fail())
- {
- std::cout << "Invalid input!" << std::endl << str;
- if (std::cin.fail())
- {
- std::cin.clear();
- std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
- }
- std::cin >> num;
- };
- std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
- return num;
- }
- //Hotel.h
- #pragma once
- #include <iostream>
- class Hotel {
- public:
- Hotel(std::string name, int cost);
- friend std::ostream& operator<< (std::ostream&, Hotel&);
- std::string getName();
- int getCost();
- void setCost(int);
- private:
- Hotel();
- std::string _name;
- int _cost;
- };
- //Hotel.cpp
- #include "Hotel.h"
- std::ostream& operator<< (std::ostream& stream, Hotel& obj) {
- return stream << obj._name << " : " << obj._cost;
- }
- Hotel::Hotel(std::string name, int cost): _name(name){
- if (cost >= 25000)
- throw new std::invalid_argument("Cost must be lower than 25000");
- _cost = cost;
- }
- std::string Hotel::getName() {
- return _name;
- }
- int Hotel::getCost() {
- return _cost;
- }
- void Hotel::setCost(int newCost) {
- _cost = newCost;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement