schmimona
schmimona

Reputation: 869

Overloading i/o operators in C++ for polynomials

I got this project where I have to overload the i/o operators to read and write polynomials. Unfortunately I can't seem to get it to work.

I have the header file:

#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
#include <iostream>

using namespace std;

class Polynomial
{
public:
    Polynomial();

Polynomial(int degree, double coef[]);

int degree;
double coef[ ];
friend istream& operator>>(istream&,Polynomial& );         
friend ostream& operator<<(ostream&,const Polynomial&);    

virtual ~Polynomial();
 };
#endif // POLYNOMIAL_H

and the cpp file:

#include "Polynomial.h"
#include<iostream>
#include<string>

using namespace std;

Polynomial::Polynomial()
{
    //ctor
}

Polynomial::~Polynomial()
{
    //dtor
}

Polynomial::Polynomial(int d, double c[])
{
    degree = d;
    double coef [degree+1];
    for(int i = 0; i < d+1; i++)
    {
        coef[i] = c[i];
    }

}

istream& operator>>(istream& x, const Polynomial& p)
{
    cout<<"The degree: ";
    x>>p.degree;

    for(int i = 0; i < p.degree+1; i++)
    {
        cout<<"The coefficient of X^"<<i<<"=";
        x>>p.coef[i];
    }
    return x;
}

ostream& operator<<(ostream& out, const Polynomial& p)
{
    out << p.coef[0];

    for (int i = 1; i < p.degree; i++)
    {
        out << p.coef[i];
        out << "*X^";
        out << i;
    }

    return out;
}

In the name I am trying to read a polynomial and then to write another one:

#include <iostream>
using namespace std;
#include "Polynomial.h"

int main()
{
    Polynomial p1();
    cin >> p1;

    int degree = 2;
    double coef [3];
    coef[0]=1;
    coef[1]=2;
    coef[3]=3;

    Polynomial p(degree, coef);

    cout<<p;


    return 0;
}

When I run the program it just freezes and I can't seem to find the error. Any ideas?

Upvotes: 0

Views: 1894

Answers (2)

Baum mit Augen
Baum mit Augen

Reputation: 50101

Polynomial::Polynomial(int d, double c[])
{
    degree = d;
    double coef [degree+1];
    for(int i = 0; i < d+1; i++)
    {
        coef[i] = c[i];
    }

}

Here, you create local array coef (with non-standard C++ btw) and then assign to it. Your member coeff is not initialized to anything meanigfull (and makes little sense the way it is right now in the first place).

Instead of double coef[] you should use std::vector like this:

struct polynomial {
    std::vector<double> coef;
    // No need for member varaible degree, vector knows its lengths
    polynomial (const std::vector<double> &coeffs) : coef (coeffs) {}
};

And then define all other constructors you need to do something meaningful. Alternatively, you can leave out constructors entirely and directly assign the coefficient vector. Then you can for example functions like

int degree (const polynomial &p) {
    return p.coef.size() - 1;
}

or

std::ostream &operator << (std::ostream &out, const polynomial p) {
    if (p.coef.size() == 0) {
        out << "0\n";
        return out;
    }
    out << p.coeff[0];
    for (int i = 1; i < p.coef.size(); ++i)
       out << " + " << p.coef[i] << "*X^i";
    out << "\n";
    return out;
}

Upvotes: 1

Ajay
Ajay

Reputation: 18431

(1)

double coef[];

This is non-standard approach to have un-sized/dynamic-sized array on stack. You better give the array some size, OR make it a pointer and allocate memory yourself; OR use vector<double>

(2) You are creating a local variable in constructor, which will hide the member-variable in class. If you are using pointer approach, you need to allocate it properly here in constructor. With either approach, you should initialize the (member) variable coef with zeros, ideally.

(3)

Polynomial p1();

This effectively declares a function named p1 which would return a Polynomial and not a variable of tyoe Polynomial. You may want to use:

Polynomial p1;

Upvotes: 0

Related Questions