mercredi 27 avril 2022

I can't get the assignment operator = to overload in cpp [closed]

The comparison in the assignment operator works as expected, but when I try to use a temp variable and load then return that, all it returns is the defaults. Debugs show that the values are {...}, but I'm not sure what to with it to make it fill the temp variables and return them. Everything I've gone through says it should work.

main

    // Demonstrating class Polynomial's overloaded stream insertion 
// and stream extraction operators.
#include <iostream>

#include "Polynomial.h"
using namespace std;

int main() {


    Polynomial poly; // create object poly
    Polynomial poly2; // create object poly
    Polynomial poly3; // create object poly
    Polynomial poly4; // create object poly
    Polynomial poly5; // create object poly


    cout << "Enter polynomial number in the form 2x4:" << endl;
    // cin >> phone invokes operator>> by implicitly issuing
    // the non-member function call operator>>(cin, phone)
    cin >> poly;
    cout << "\nThe polynomial number entered was:\n";
    // cout << phone invokes operator<< by implicitly issuing
    // the non-member function call operator<<(cout, phone)
    cout << poly << endl;
    cout << "Enter polynomial number in the form 2x4:" << endl;
    // cin >> phone invokes operator>> by implicitly issuing
    // the non-member function call operator>>(cin, phone)
    cin >> poly2;
    cout << "\nThe polynomial number entered was:\n";
    // cout << phone invokes operator<< by implicitly issuing
    // the non-member function call operator<<(cout, phone)
    cout << "poly2 " << poly2 << endl;
    poly3 = poly + poly2;
    cout << "poly3 " << poly3 << endl;
    poly4 = poly - poly2;
    cout << "poly4 " << poly4 << endl;
    poly5 = poly;
    cout << "poly5 " << poly5 << endl;
  }

Header

#pragma once
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H

#include <iostream>
#include <string>

class Polynomial {
    friend std::ostream& operator<<(std::ostream&, const Polynomial&);
    friend std::istream& operator>>(std::istream&, Polynomial&);

public:
    // Default Constructor

    Polynomial(std::string coefficient = "1", std::string variable = "x",
        std::string exponent = "4");

    // Copy Constructor
    Polynomial(const Polynomial& copy)
        : coefficient{ copy.coefficient }, variable{ copy.variable },
        exponent{ copy.exponent } {
        std::cout << "Copy Constructor called" << std::endl;
    }

    void setPolynomial(std::string, std::string, std::string);
    Polynomial getPolynomial();
    // addition operator; Polynomial + Polynomial
    Polynomial operator+(const Polynomial&) const;
    // subtraction operator; Polynomial - Polynomial
    Polynomial operator-(const Polynomial&) const;
    // assigment operator; Polynomial - Polynomial
    Polynomial operator=(const Polynomial&) const;

private:
    std::string coefficient; // 
    std::string variable;    // 
    std::string exponent;    // 
};

#endif

Polynomial.cpp

// Overloaded stream insertion and stream extraction operators
// for class PhoneNumber.
#include "Polynomial.h"
#include <iomanip>
using namespace std;

// default constructor; conversion constructor that converts
Polynomial::Polynomial(std::string co, std::string va, std::string ex) {}
// Setters
void Polynomial::setPolynomial(std::string co, std::string va, std::string ex) {
    this->coefficient = co;
    this->variable = va;
    this->exponent = ex;
}
// Getters
Polynomial Polynomial::getPolynomial() { return *this; }
// overloaded stream insertion operator; cannot be a member function
// if we would like to invoke it with cout << somePhoneNumber;
ostream& operator<<(ostream& output, const Polynomial& number) {
    output << "Coefficient: " << number.coefficient
        << "\nVariable: " << number.variable
        << "\nExponent: " << number.exponent << "\n"
        << "" << number.coefficient << "" << number.variable << "^"
        << number.exponent << "\n";
    return output; // enables cout << a << b << c;
}

// overloaded stream extraction operator; cannot be a member function
// if we would like to invoke it with cin >> somePhoneNumber;
istream& operator>>(istream& input, Polynomial& number) {
    input >> setw(1) >> number.coefficient; // input area code
    input >> setw(1) >> number.variable;    // input exchange
    input >> setw(1) >> number.exponent;    // input line
    return input;                           // enables cin >> a >> b >> c;
}

// addition operator; Polynomial + Polynomial
// A member function takes an implicit first parameter
Polynomial Polynomial::operator+(const Polynomial& op1) const {
    Polynomial temp; // temporary result

    if (this->variable == op1.variable) {
        if (this->exponent == op1.exponent) {
            // Use stoi string to int
            int num1 = stoi(this->coefficient);
            int num2 = stoi(op1.coefficient);
            // use to_string to set coefficient
            std::string s = std::to_string(num1 + num2);
            temp.coefficient = s;
            temp.variable = this->variable;
            temp.exponent = this->exponent;
        }
        else {
            std::cout << "Exponents must match\n";
        }
    }
    else {
        std::cout << "Variables must match\n";
    }
    return temp; // return copy of temporary object
}
// substraction operator; Polynomial - Polynomial
// A member function takes an implicit first parameter
Polynomial Polynomial::operator-(const Polynomial& op1) const {
    Polynomial temp; // temporary result

    if (this->variable == op1.variable) {
        if (this->exponent == op1.exponent) {
            // Use stoi string to int
            int num1 = stoi(this->coefficient);
            int num2 = stoi(op1.coefficient);
            // use to_string to set coefficient
            std::string s = std::to_string(num1 - num2);
            temp.coefficient = s;
            temp.variable = this->variable;
            temp.exponent = this->exponent;
        }
        else {
            std::cout << "Exponents must match\n";
        }
    }
    else {
        std::cout << "Variables must match\n";
    }
    return temp; // return copy of temporary object
}

// assignment operator; Polynomial - Polynomial
// A member function takes an implicit first parameter
Polynomial Polynomial::operator=(const Polynomial& op1) const {
    // self assignment guard
    if (this == &op1) {
        return *this;//This returns as expected.
        // 
    } // This should create a new temp, assign the second's info to it and return it.
    // But all it does is return the default constructor values 1x4
    Polynomial temp;
    temp.coefficient = op1.coefficient;
    temp.variable = op1.variable;
    temp.exponent = op1.exponent;
    return temp;
}

Aucun commentaire:

Enregistrer un commentaire