Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <utility>
- #include <iostream>
- #include <string>
- class CBaseEntity
- {
- private:
- std::string name;
- unsigned long days = 0;
- unsigned long price = 0;
- public:
- // Default constructor
- CBaseEntity() = default;
- CBaseEntity(const CBaseEntity&) = default;
- CBaseEntity(CBaseEntity&&) = default;
- // Data-based constructor
- CBaseEntity(std::string name, unsigned long days, unsigned long price)
- : name(std::move(name)), days(days), price(price)
- {}
- ~CBaseEntity() = default;
- // Total price getter
- unsigned long getTotalPrice() const noexcept
- {
- return days * price;
- }
- // Name getter
- const std::string &getName() const
- {
- return name;
- }
- // Name setter
- void setName(const std::string &name)
- {
- this->name = name;
- }
- // Days getter
- unsigned long getDays() const
- {
- return days;
- }
- // Days setter
- void setDays(unsigned long days)
- {
- this->days = days;
- }
- // Price getter
- unsigned long getPrice() const
- {
- return price;
- }
- // Price setter
- void setPrice(unsigned long price)
- {
- this->price = price;
- }
- // Copy operator overloading
- CBaseEntity &operator=(const CBaseEntity &) = default;
- // Input, output operator overloading
- friend std::ostream &operator<<(std::ostream &stream, const CBaseEntity &entity);
- };
- std::ostream &operator<<(std::ostream &stream, const CBaseEntity &entity)
- {
- stream << "\tName: " << entity.name << std::endl;
- stream << "\tDays: " << entity.days << std::endl;
- stream << "\tPrice for day: " << entity.price << std::endl;
- stream << "\tTotal price: " << entity.getTotalPrice() << std::endl;
- return stream;
- }
- int main()
- {
- size_t arraySize;
- std::cout << "Enter array size: ";
- std::cin >> arraySize;
- auto array = new CBaseEntity[arraySize];
- // Input all entities
- for (size_t i = 0; i <arraySize; i++) {
- std::string name;
- unsigned long days;
- unsigned long price;
- std::cout << "Entity index: " << i << std::endl;
- std::cout << "\tName: ";
- std::cin.ignore();
- std::getline(std::cin, name);
- std::cout << "\tDays: ";
- std::cin >> days;
- std::cout << "\tPrice for day: ";
- std::cin >> price;
- array[i] = CBaseEntity(name, days, price);
- }
- // Output
- for (size_t i = 0; i <arraySize; i++) {
- std::cout << "Entity index: " << i << std::endl;
- std::cout << array[i] << std::endl;
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment