Question: Please answer in C++ programing language. The problem: Overload the + and = operators for the Square class we have provided you. The = should

Please answer in C++ programing language.

The problem:

Overload the + and = operators for the Square class we have provided you.

The = should make a deep copy of the Square object.

The + operator should concatenate the names of the two Squares and add their lengths.

We've included a main.cpp with some sample code to test these. The expected output is

Square a mysquare 2 Square b mysquare 2 Square c mysquaremysquare 4

Square.h

#pragma once

#include

class Square {

private:

std::string name;

double * lengthptr;

public:

Square();

Square(const Square & old);

~Square();

void setName(std::string newName);

void setLength(double newVal);

std::string getName() const;

double getLength() const;

Square & operator=(const Square & other);

Square operator+(const Square & other);

};

main.cpp:

#include

#include

#include "Square.h"

int main(){

Square a;

Square b = a;

Square c = a + b;

std::cout << "Square a " << a.getName() << " " << a.getLength() << std::endl;

std::cout << "Square b " << b.getName() << " " << b.getLength() << std::endl;

std::cout << "Square c " << c.getName() << " " << c.getLength() << std::endl;

return 0;

}

Here is what I need to implement - Square.cpp:

#include

#include

#include "Square.h"

Square::Square() {

name = "mysquare";

lengthptr = new double;

*lengthptr = 2.0;

}

void Square::setName(std::string newName) {

name = newName;

}

void Square::setLength(double newVal) {

*lengthptr = newVal;

}

std::string Square::getName() const {

return name;

}

double Square::getLength() const {

return *lengthptr;

}

Square::Square(const Square & other) {

name = other.getName();

lengthptr = new double;

*lengthptr = other.getLength();

}

Square & Square::operator=(const Square & other) {

if (this == &other) {

return this;

}

delete this;

}

Square Square::operator+(const Square & other) { }

Square::~Square() {

delete lengthptr;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!