我的作业是使用以下数据成员/成员函数创建以下类。
- 计算机
- 内存(以 GB 为单位(。
- 处理器速度(以千兆赫为单位(。
- # 核心数
- 硬盘存储大小(以 GB 为单位(。
- 打印(虚拟功能(
- 桌面(继承自计算机(
- 有监视器
- 显示器尺寸(英寸(
- 笔记本电脑(继承自计算机(
- 屏幕尺寸
- 群集(继承自计算机(
- 计算机数量
应该添加一个虚拟打印功能,并为我拥有的每台台式机/笔记本电脑输入。有人可以帮助我弄清楚为什么它不接受任何输入吗?
#include <iostream>
using namespace std;
class computers{
public:
int ram;
double processor_speed;
int num_cores;
int hd_storage;
computers();
};
computers::computers()
{
ram=0;
processor_speed=0;
num_cores=0;
hd_storage=0;
}
class desktop: public computers{
public:
bool hasMonitor;
double monitor_size;
desktop();
friend istream& operator>>(istream& isObject, desktop& desk1);
};
desktop::desktop()
{
hasMonitor=true;
monitor_size=0;
}
istream& operator>>(istream& isObject, desktop& desk1)
{
isObject>>desk1.hasMonitor;
isObject>>desk1.monitor_size;
isObject>>desk1.ram;
isObject>>desk1.processor_speed;
isObject>>desk1.hd_storage;
isObject>>desk1.num_cores;
if(desk1.hasMonitor==0)
desk1.hasMonitor=false;
return isObject;
}
class laptop:public computers{
public:
int screen_size;
laptop();
friend istream& operator>>(istream& isObject,laptop& lap1);
};
laptop::laptop()
{
screen_size=0;
}
istream& operator>> (istream& isObject, laptop& lap1)
{
isObject>> lap1.ram;
isObject>> lap1.processor_speed;
isObject>> lap1.hd_storage;
isObject>> lap1.num_cores;
isObject>> lap1.screen_size;
return isObject;
}
class cluster:public computers{
public:
int num_of_comps;
friend istream& operator>>(istream& isObject,cluster& clust1);
cluster();
};
cluster::cluster()
{
num_of_comps=0;
}
istream& operator>>(istream& isObject,cluster& clust1)
{
isObject >> clust1.ram;
isObject >> clust1.processor_speed;
isObject >> clust1.hd_storage;
isObject >> clust1.num_cores;
isObject >> clust1.num_of_comps;
return isObject;
}
cluster operator+(const computers& comp1, const computers& comp2)
{
cluster mrcluster;
mrcluster.ram = comp1.ram+comp2.ram;
mrcluster.processor_speed = comp1.processor_speed+comp2.processor_speed;
mrcluster.num_cores = comp1.num_cores+comp2.num_cores;
mrcluster.hd_storage = comp1.hd_storage+comp2.hd_storage;
return mrcluster;
}
int main()
{
laptop laptop1;
desktop desktop1;
desktop desktop2;
cluster mycluster = laptop1+desktop1+desktop2;
system("pause");
}
您没有要求在main
函数中进行任何输入。 main
是程序的入口点,因此您想要完成的任何操作都必须源自那里(例如,函数调用以请求输入(。
你需要做 cin>> 笔记本电脑1
顺便说一句,我会称您的基类计算机而不是计算机