简体   繁体   English

用许多对象重载加法运算符C ++

[英]Overloading addition operator with many objects c++

I need to overload the addition operator of multiple different objects and return a 'cluster object: "Overload the addition operator(+) to add any combination of instances for Desktops, Laptops, and Clusters. This operator should return an object of type cluster" So if I had desktop1+laptop1+laptop2; 我需要重载多个不同对象的加法运算符,并返回一个'cluster对象:“重载加法运算符(+)以添加台式机,笔记本电脑和群集的实例的任意组合。此运算符应返回cluster类型的对象。所以如果我有desktop1 + laptop1 + laptop2; I would need it to return a cluster that adds all of the RAM, and other variables of each object. 我将需要它返回一个群集,该群集添加所有RAM和每个对象的其他变量。 But it will only be adding the variables that all of the objects inherit from the computer class. 但这只会添加所有对象从计算机类继承的变量。 Here is my code of the rest of the project. 这是我其余项目的代码。 Thanks! 谢谢!

#include <iostream>

using namespace std;

class computer
{
    public:
        double RAM;
        double CPUSpeed;
        int numberOfCores;
        double HDDSize;
        virtual void print();
};
class desktop : public computer
{
    public:
        bool hasMonitor;
        double monitorSize;
        friend istream& operator>> (istream &in, desktop &myDesktop);
};
class laptop : public computer
{
    public:
        double screenSize;
};
class cluster : public computer
{
    public:
        int numOfComp;
};
desktop::desktop()
{
    RAM = 0;
    CPUSpeed = 0;
    numberOfCores = 0;
    HDDSize = 0;
    hasMonitor = 0;
    monitorSize = 0;
}
laptop::laptop()
{
    RAM = 0;
    CPUSpeed = 0;
    numberOfCores = 0;
    HDDSize = 0;
    screenSize = 0;
}
cluster::cluster()
{
    RAM = 0;
    CPUSpeed = 0;
    numberOfCores = 0;
    HDDSize = 0;
    numOfComp = 0;
}
istream& operator>> (istream &in, desktop &myDesktop)
{
    in >> myDesktop.hasMonitor;
    in >> myDesktop.monitorSize;
    in >> myDesktop.RAM;
    in >> myDesktop.CPUSpeed;
    in >> myDesktop.HDDSize;
    in >> myDesktop.numberOfCores;
    return in;
}
istream& operator>> (istream &in, laptop &mylaptop)
{
    in >> mylaptop.RAM;
    in >> mylaptop.CPUSpeed;
    in >> mylaptop.HDDSize;
    in >> mylaptop.numberOfCores;
    in >> mylaptop.screenSize;
    return in;
}
istream& operator>> (istream &in, cluster &myCluster)
{
    in >> myCluster.RAM;
    in >> myCluster.CPUSpeed;
    in >> myCluster.HDDSize;
    in >> myCluster.numberOfCores;
    in >> myCluster.numOfComp;
    return in;
}
operator+(computer &myComp)
{
    return 

If you only want to add up the values common to all the classes that they inherit from computer, you can do it like this: 如果只想累加它们从计算机继承的所有类的通用值,则可以这样做:

cluster operator+(const computer& comp1, const computer& comp2)
{
    cluster ret;
    ret.RAM = comp1.RAM+comp2.RAM;
    ret.CPUSpeed = comp1.CPUSpeed+comp2.CPUSpeed;
    ret.numberOfCores = comp1.numberOfCores+comp2.numberOfCores;
    ret.HDDSize = comp1.HDDSize+comp2.HDDSize;
    return ret;
}

int main()
{
    laptop laptop1;
    desktop desktop1;
    desktop desktop2;
    cluster mycluster = laptop1+desktop1+desktop2;
    return 0;
}

The chained + operators are evaluated from the right leftwards. 链接的+运算符从右向左评估。 The function returns a cluster object, which can then be used as the right hand operand for the next addition. 该函数返回一个群集对象,然后该群集对象可用作下一个加法运算的右侧操作数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM