initializing Arrays within constructor

Hi all, I have a header file with a class and constructor. within the constructor I have two arrays. The part I am struggling with is writing a the main.cpp file to test my functions and I do not know how to write the constructor with using the 2 arrays. Example of what I am looking to do is utilize constructor and have 2 vars. So I would have
Polynomial var1 numterms = 2, coef(2,3), expnt(4,2)
Polynomial var2 numterms = 2, coef(4,2), expnt(5,8)

How do I do this?
I am trying to perform polynomial addition.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#ifndef POLY_H
#define POLY_H

#include <iostream>

using namespace std;

//Base Class---------------------------------------------
class Polynomial {
	private:
		//number of coefficients & exponents
		int NumberofTerms;
		//arrays
		float *Coefficient_arr;
		float *Exponent_arr;
		//size
		int Capacity;
	public:
		//regular constructor
		Polynomial(int numterms, float coef[], float expnt[]);
		//deconstructor
		~Polynomial()
		//Setters - Getters
		float *get_coefficient() { return Coefficient_arr; }
		float *get_exponent() { return Exponent_arr ; }
		float get_numberofTerms() { return NumberofTerms;}
		void set_coefficient( float x ) { Coefficient_arr = x; }
		void set_exponent( float x ) { Exponent_arr = x; }
		//Overload addition
		Polynomial& operator += ( const Polynomial& poly );
		const Polynomial operator + (Polynomial poly1, const Polynomial& poly2);
		//Assignment operator
		Polynomial& Polynomial::operator=(Polynomial poly);
};
#endif 
Use std::vector<> https://cal-linux.com/tutorials/vectors.html
Rule of zero: http://en.cppreference.com/w/cpp/language/rule_of_three

Vector of coefficients and another vector of corresponding exponents:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
struct polynomial
{
    // note: you may want to ignore the std::move for now
    polynomial( std::vector<double> coefficients, std::vector<int> exponents )
        : coefficients( std::move(coefficients) ), exponents( std::move(exponents) )
    { if( !valid() ) throw std::invalid_argument( "unmatched terms" ) ; }

    std::size_t size() const { return coefficients.size() ; }

    // ...

    private:
        std::vector<double> coefficients ;
        std::vector<int> exponents ;

        bool valid() const { return coefficients.size() == exponents.size() ; }

        friend std::ostream& operator<< ( std::ostream& stm, const polynomial& poly )
        {
            for( std::size_t i = 0 ; i < poly.size() ; ++i )
                stm << '(' << poly.coefficients[i] << ',' << poly.exponents[i] << ") " ;
            return stm ;
        }
};


Perhaps better: vector of terms, with each term being a coefficient-exponent pair
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
struct polynomial2
{
    // note: you may want to ignore the std::move() for now
    explicit polynomial2( std::vector< std::pair<double,int> > terms ) : terms( std::move(terms) ) {}

    std::size_t size() const { return terms.size() ; }

    // ...

    private:
        std::vector< std::pair<double,int> > terms ; // http://en.cppreference.com/w/cpp/utility/pair

        friend std::ostream& operator<< ( std::ostream& stm, const polynomial2& poly )
        {
            for( const auto& term : poly.terms )
                stm << '(' << term.first << ',' << term.second << ") " ;
            return stm ;
        }
};

http://coliru.stacked-crooked.com/a/750fb99994703de9
thanks, but I need to have it be arrays for Coefficients and Exponents. So how would I initialize using the constructor in main.cpp . As such
Polynomial var1 numterms = 2, coef(2,3), expnt(4,2)
Polynomial var2 numterms = 2, coef(4,2), expnt(5,8)

- Overall goal is overload using the addition, subtraction, assignment and multiplication operator.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <stdexcept>
#include <memory>

struct polynomial
{
    static constexpr std::size_t capacity = 1000 ; // max 1000 terms

    // invariant: nterms <= capacity, if nterms > 0, then coeffs != nullptr and exps != nullptr
    polynomial( std::size_t nterms, const double coeffs[], const int exps[] ) : nterms(nterms) // note: const

    {
        if( nterms > capacity ) throw std::out_of_range( "capacity exceeded" ) ;

        if( nterms > 0 )
        {
            // http://en.cppreference.com/w/cpp/memory/uninitialized_copy
            std::uninitialized_copy( coeffs, coeffs+nterms, coefficients ) ;
            std::uninitialized_copy( exps, exps+nterms, exponents ) ;
        }
    }

    std::size_t size() const { return nterms ; }

    // addition etc.
    // ...

    private:
        std::size_t nterms ;
        double coefficients[capacity] = {} ; // initialise to all zeroes
        int exponents[capacity] = {} ; // initialise to all zeroes

        friend std::ostream& operator<< ( std::ostream& stm, const polynomial& poly )
        {
            for( std::size_t i = 0 ; i < poly.size() ; ++i )
                stm << '(' << poly.coefficients[i] << ',' << poly.exponents[i] << ") " ;
            return stm ;
        }
};

int main()
{
    const std::size_t n = 4 ;
    const double coefficients[4] = { 1.5, -2.3, -4.8, 6.7 } ;
    const int exponents[4] = { -2, 0, 3, 8 } ;

    std::cout << polynomial( n, coefficients, exponents ) << '\n' ;
}

http://coliru.stacked-crooked.com/a/5ba1d4bf767ae9f4
Topic archived. No new replies allowed.