通过使用运算符>>获取的cin输入创建类

时间:2013-10-09 23:54:51

标签: c++ oop input cin

我的任务是使用以下数据成员/成员函数创建以下类。

  • 计算机
    • RAM(以GB为单位)。
    • 处理器速度(以GHz为单位)。
    • #cores
    • 硬盘存储空间大小(以GB为单位)。
    • 打印(虚拟功能)
  • 桌面(继承自计算机)
    • hasMonitor
    • monitorSize(英寸)
  • 笔记本电脑(继承自电脑)
    • 屏幕尺寸
  • 群集(继承自计算机)
  • 计算机数量

我应该添加虚拟打印功能并为我拥有的每台台式机/笔记本电脑输入输入。有人可以帮我弄清楚为什么它不接受任何输入吗?

#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");
}

2 个答案:

答案 0 :(得分:1)

您不是要求main功能中的任何输入。 main是您的程序的入口点,因此您想要完成的任何事情都必须来自那里(例如函数调用以询问输入)。

答案 1 :(得分:0)

你需要做cin&gt;&gt; laptop1

btw我会把你的基类计算机称为计算机