mrplow911
mrplow911

Reputation: 44

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; 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 

Upvotes: 0

Views: 341

Answers (1)

bumpt
bumpt

Reputation: 46

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.

Upvotes: 1

Related Questions