class CFlash { private: int m_Memory; //闪存容量,单位KB int m_Freq; //工作频率,单位MHZ public: CFlash (int Memory, int Freq); //容量和频率小于等于0为非法 int GetMemory(void)const; //获取容量 int GetFreq(void)const; //获取工作频率 friend CFlash operator+(const CFlash& Obj, int m); //闪存容量增大m KB friend ostream& operator<<(ostream& Out, const CFlash& Obj); //输出闪存的参数, //容量占5列,左对齐,右补空格 //频率占5列,左对齐,右补空格 }; 请为CFlash类完成上述所要求的各函数定义。
时间: 2024-01-05 20:03:28 浏览: 26
这是一个CFlash类的定义,需要实现以下函数:
```cpp
CFlash::CFlash(int Memory, int Freq)
{
if(Memory <= 0 || Freq <= 0) {
m_Memory = 0;
m_Freq = 0;
} else {
m_Memory = Memory;
m_Freq = Freq;
}
}
int CFlash::GetMemory() const
{
return m_Memory;
}
int CFlash::GetFreq() const
{
return m_Freq;
}
CFlash operator+(const CFlash& Obj, int m)
{
int newMemory = Obj.m_Memory + m;
int newFreq = Obj.m_Freq;
return CFlash(newMemory, newFreq);
}
ostream& operator<<(ostream& Out, const CFlash& Obj)
{
Out << setw(5) << left << Obj.m_Memory << setw(5) << left << Obj.m_Freq;
return Out;
}
```
其中,构造函数判断闪存容量和工作频率是否合法,如果不合法则初始化为0;GetMemory和GetFreq函数分别用于获取闪存容量和工作频率;operator+函数用于增加闪存容量,返回一个新的CFlash对象;operator<<函数用于输出闪存参数,按照要求左对齐,右补空格。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![exe](https://img-home.csdnimg.cn/images/20210720083343.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)