0

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; I would need it to return a cluster that adds all of the RAM, and other variables of each object. 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 
mrplow911
  • 44
  • 7

1 Answers1

1

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.

bumpt
  • 46
  • 3
  • I don't suppose you want to "add" resources, as the definition of a cluster would need to aggregate then. Right now, a cluster `is-a` computer, and my guess is it models `numOfComp` machines with some _minimum common set of specs_. Oh well, academia and contrived examples, everyday: http://coliru.stacked-crooked.com/a/5bfc44a3da7a77ae (throwing in the `virtual print()` method) – sehe Oct 06 '13 at 22:41