#include <iostream>
using namespace std;

class Television {
private:
    string brand;
    string model;
    double price;
    int volume;
    int channel;
public:
    void setBrand(string b) {
        brand = b;
    }
    string getBrand() {
        return brand;
    }
    void setModel(string m) {
        model = m;
    }
    string getModel() {
        return model;
    }
    void setPrice(double p) {
        price = p;
    }
    double getPrice() {
        return price;
    }
    void setVolume(int v) {
        if (v < 0) {
            volume = 0;
        } else if (v > 100) {
            volume = 100;
        } else {
            volume = v;
        }
    }
    int getVolume() {
        return volume;
    }
    void setChannel(int c) {
        if (c < 1) {
            channel = 1;
        } else if (c > 255) {
            channel = 255;
        } else {
            channel = c;
        }
    }
    int getChannel() {
        return channel;
    }
};

int main() {
    Television tv;
    tv.setBrand("Samsung");
    tv.setModel("ABC123");
    tv.setPrice(1999.99);
    tv.setVolume(50);
    tv.setChannel(100);

    cout << "Brand: " << tv.getBrand() << endl;
    cout << "Model: " << tv.getModel() << endl;
    cout << "Price: " << tv.getPrice() << endl;
    cout << "Volume: " << tv.getVolume() << endl;
    cout << "Channel: " << tv.getChannel() << endl;

    return 0;
}
``
用C++设计一个电视机类包含品牌、型号、价格、音量和频道等私有成员变量。为这个类添加公有成员函数用于设置和获取电视机的信息。要求设置电视机的信息时只能设置电视机的品牌、型号、价格以及音量和频道的信息获取电视机的信息时只能获取电视机的品牌、型号、价格、音量和频道的信息。其中音量和频道的取值范围分别为0到100和1到255超出范围的值应自动调整为最小或最大值。

原文地址: https://www.cveoy.top/t/topic/eDKt 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录