c++ 声明一个CPU类
时间: 2024-10-15 08:27:14 浏览: 26
在C++中,为了声明一个表示CPU的类,我们可以按照面向对象的设计原则来构建。下面是一个简单的CPU类示例,包含一些基本属性如品牌、型号和核心数:
```cpp
class CPU {
private:
std::string brand; // CPU品牌
std::string model; // CPU型号
int coreCount; // 核心数
public:
// 构造函数,用于初始化CPU对象
CPU(const std::string& brand_, const std::string& model_, int coreCount_) :
brand(brand_), model(model_), coreCount(coreCount_) {}
// 获取CPU品牌
std::string getBrand() const {
return brand;
}
// 获取CPU型号
std::string getModel() const {
return model;
}
// 获取核心数
int getCoreCount() const {
return coreCount;
}
// 可能的设置方法
void setBrand(const std::string& newBrand) {
brand = newBrand;
}
void setModel(const std::string& newModel) {
model = newModel;
}
void setCoreCount(int newCoreCount) {
coreCount = newCoreCount;
}
// 显示CPU信息的公共成员函数
virtual void displayInfo() const {
std::cout << "CPU: Brand - " << brand << ", Model - " << model << ", Cores - " << coreCount << std::endl;
}
};
```
在这个`CPU`类中,我们有私有成员变量以及构造函数,getter和setter方法来访问和修改这些属性,以及一个虚函数`displayInfo()`用于显示CPU的信息。
阅读全文
相关推荐
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)