내배캠 TIL

[내배캠 TIL 251211] C++ Class 활용한 예제

xodn246 2025. 12. 11. 20:54

온도계(섭씨 화씨 전환)

// Copyright [2025] Taewoo Kim
#include <iostream>

class Thermometer {
 public:
    void setCurrentTemp(float currentTemp) {
        if (currentTemp >= -100 && currentTemp <= 100)
            this->currentTemp = currentTemp;
        else
            std::cout << "잘못된 입력입니다." << std::endl; //NOLINT
     }

    float getCurrentTemp() {
        if (tempType) return currentTemp;
        else return (1.8f * currentTemp) + 32;
    }

    void setTempType() {
        if (tempType) {
            tempType = false;
            std::cout << "화씨 모드" << std::endl; //NOLINT
        } else {
            tempType = true;
            std::cout << "섭씨 모드" << std::endl; //NOLINT
        }
    }

    bool getTemptype() {
        return tempType;
     }

    Thermometer() {
        currentTemp = 0;
        tempType = true;
    }

 private:
    float currentTemp;
    bool tempType;
};

int main() {
    int inputNum = 0;
    float inputTemp = 0.0f;
    Thermometer therm;
    std::cout << "1) 온도 설정\n2) 표시 모드 전환\n3) 온도 표시" << std::endl; //NOLINT
    std::cout << "메뉴 선택 : "; //NOLINT
    std::cin >> inputNum;

    while (1) {
        switch (inputNum) {
        case 1:
            std::cout << "현재 온도 입력 : "; //NOLINT
            std::cin >> inputTemp;
            therm.setCurrentTemp(inputTemp);
            break;
        case 2:
            therm.setTempType();
            break;
        case 3:
            if (therm.getTemptype())
                std::cout << "현재 온도는 섭씨 " //NOLINT
                              << therm.getCurrentTemp() << "도 입니다." <<std::endl; //NOLINT
            else
                std::cout << "현재 온도는 화씨 " //NOLINT
                              << therm.getCurrentTemp() << "도 입니다." << std::endl; //NOLINT
            break;
        case 4:
            return 0;
        }
        std::cout << "1) 온도 설정\n2) 표시 모드 전환\n3) 온도 표시" << std::endl; //NOLINT
        std::cout << "메뉴 선택 : "; //NOLINT
        std::cin >> inputNum;
    }
}

 

차량 정보 출력 및 속도제어

// Copyright [2025] Taewoo Kim
#include <iostream>
#include <string>

class Car {
 public:
        void Accelerate() {
            velocity += 10;
        }
        void Break() {
            velocity -= 10;
        }
        void DisplayInfo() {
            std::cout << "Car Model : " << model << std::endl;
            std::cout << "Current Velocity : " << velocity << std::endl;
        }
        explicit Car(std::string model) {
            this->model = model;
            this->velocity = 0;
        }
 private:
        std::string model;
        int velocity;
};

int main() {
    std::string carModel;
    int inputNum = 0;

    std::cout << "Input your car model : ";
    std::getline(std::cin, carModel);
    Car myCar(carModel);
    std::cout << "1) Accelerate\n2) Break\n3) Exit\n";
    std::cin >> inputNum;
    while (1) {
        switch (inputNum) {
            case 1:
                myCar.Accelerate();
                myCar.DisplayInfo();
                break;
            case 2:
                myCar.Break();
                myCar.DisplayInfo();
                break;
            case 3:
                return 0;
        }
        std::cout << "1) Accelerate\n2) Break\n3) Exit\n";
        std::cin >> inputNum;
     }
}