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

标签 c++ oop input cin

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

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

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

#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 是您程序的入口点,因此您想要完成的任何事情都必须从那里开始(例如,函数调用以请求输入)。

关于c++ - 通过使用运算符获取的 cin 输入创建类 >>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19284787/

相关文章:

c++ - boost.python return_internal_reference 策略

oop - Kotlin 中的 OVERRIDE_BY_INLINE

c++ - 我可以直接调用 std::terminate 吗?

c++ - 需要有关类(class)的帮助

Java-如何在现有数组中插入用户输入位置

html - 为什么我的输入在右侧有填充?

c++ - 使用 cmake 将 libconfig 与 C++ 链接时出错?

c++ - 接受类参数的友元函数的尾随返回类型

c++ - 右值引用是否有reference_wrapper<>?

java - Java中while循环的顺序