C++ 电视机类设计:包含品牌、型号、价格、音量和频道
#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;
}
原文地址: http://www.cveoy.top/t/topic/nUYp 著作权归作者所有。请勿转载和采集!