user3407254
user3407254

Reputation: 55

How to make public members of a class private?

I'm brand new to programming (in general) and C++ (in particular). I'm trying to take the following public member variables and make them private:

int *coeff;
int order;

Unfortunately, I'm seeing the following errors:

'Poly::coeff' : cannot access private member declared in class 'Poly'

and

'Poly::order' : cannot access private member declared in class 'Poly'

Here's my code:

#include "stdafx.h"
#include <iostream>
#include <cstdlib>

using namespace std;

class Poly
{
public:

    int *coeff;
    int order;

    int getData();
    int display(int *coeff, int order);
    void addition(Poly P1, Poly P2);
    void subtraction (Poly P1, Poly P2);
    void multiplication (Poly P1, Poly P2);

//  ~Poly();
};

int Poly::display(int *coeff, int order)
{
    int i;
    int j;
    for (i = order; i >= 0; i--)
    {
        cout << coeff[i] << "x^" << i;
        if ((i - 1) != -1)
        {
            cout << "+";
        }
    }
    cout << "\n";
    return 0;
}

int Poly::getData()
{
    int i;
    cout << "Please enter the order of the polynomial: ";
    cin >> order;
    coeff = new int[order + 1];
    for (i = order; i >= 0; i--)
    {
        cout << "Please enter the coefficient of x^" << i << " :";
        cin >> coeff[i];
    }
    return 0;
}

void Poly::addition(Poly P1, Poly P2)
{
    int max;
    int i;
    max = (P1.order > P2.order) ? P1.order : P2.order;
    int *add = new int[max + 1];

    if (P1.order == P2.order)
    {
        for (i = P1.order; i >= 0; i--)
        {
            add[i] = P1.coeff[i] + P2.coeff[i];
        }
    }

    if (P1.order > P2.order)
    {
        for (i = P1.order; i > P2.order; i--)
        {
            add[i] = P1.coeff[i];
        }
        for (i = P2.order; i >= 0; i--)
        {
            add[i] = P1.coeff[i] + P2.coeff[i];
        }
    }

    if (P1.order < P2.order)
    {
        for (i = P2.order; i > P1.order; i--)
        {
            add[i] = P2.coeff[i];
        }
        for (i = P1.order; i >= 0; i--)
        {
            add[i] = P1.coeff[i] + P2.coeff[i];
        }
    }
    cout << "\nAddition:";
    display(add, max);
    cout << "\n";
}

void Poly::subtraction(Poly P1, Poly P2)
{
    int max;
    int i;
    max = (P1.order > P2.order) ? P1.order : P2.order;
    int *sub = new int[max + 1];

    if (P1.order == P2.order)
    {
        for (i = P1.order; i >= 0; i--)
        {
            sub[i] = P1.coeff[i] - P2.coeff[i];
        }
    }

    if (P1.order > P2.order)
    {
        for (i = P1.order; i > P2.order; i--)
        {
            sub[i] = P1.coeff[i];
        }
        for (i = P2.order; i >= 0; i--)
        {
            sub[i] = P1.coeff[i] - P2.coeff[i];
        }
    }

    if (P1.order < P2.order)
    {
        for (i = P2.order; i > P1.order; i--)
        {
            sub[i] = -P2.coeff[i];
        }
        for (i = P1.order; i >= 0; i--)
        {
            sub[i] = P1.coeff[i] - P2.coeff[i];
        }
    }
    cout << "\nSubtraction:";
    display(sub, max);
    cout << "\n";
}

void Poly::multiplication(Poly P1, Poly P2)
{
    int i;
    int j;
    int max;

    max = P1.order + P2.order;
    int *mult = new int[max + 1];

    for (i = P1.order; i >= 0; i--)
    for (j = P2.order; j >= 0; j--)
    {
        mult[i + j] += P1.coeff[i] * P2.coeff[i];
    }
        cout << "\nMultiplication:";
        display(mult, max);
}



int main()
{
    int choice;
    Poly P1, P2, P3;
    cout << "-------- Instructions --------" << endl;

    cout << "For polynomial 1... " << endl;
    P1.getData();

    cout << endl;

    cout << "For polynomial 2... " << endl;
    P2.getData();

    while (1)
    {
        cout << "\n******** Menu Selection ********" << endl;
        cout << "1: Addition\n2: Subtraction\n3: Mutiplication\n0: Exit" << endl;
        cout << "Please enter your choice (1, 2, 3 or 0):";
        cin >> choice;

        switch (choice)
        {
        case 1:
            cout << "\n-------- Addition --------\n";
            cout << "Polynomial 1: ";
            P1.display(P1.coeff, P1.order);
            cout << "Polynomial 2: ";
            P2.display(P2.coeff, P2.order);
            P3.addition(P1, P2);
            cout << "--------------------------\n";
            break;

        case 2:
            cout << "\n------ Subtraction ------\n";
            cout << "Polynomial 1: ";
            P1.display(P1.coeff, P1.order);
            cout << "Polynomial 2: ";
            P2.display(P2.coeff, P2.order);
            P3.subtraction(P1, P2);
            cout << "--------------------------\n";
            break;

        case 3:
            cout << "\n------ Subtraction ------\n";
            cout << "Polynomial 1: ";
            P1.display(P1.coeff, P1.order);
            cout << "Polynomial 2: ";
            P2.display(P2.coeff, P2.order);
            P3.multiplication(P1, P2);
            cout << "--------------------------\n";
            break;

        case 0:
            cout << "The program will now terminate.  Thank you." << endl;
            exit(0);

        default:
            cout << endl;
            cout << "You have entered an invalid selection." << endl;
            cout << "Please enter a positive integer between 0 and 3.";
            cout << endl;
        }
    }

    return 0;
}

Can anyone offer guidance on how to best modify my code so that int *coeff and int order are private members?

Thanks very much in advance. -Ryan

Upvotes: 0

Views: 131

Answers (2)

πάντα ῥεῖ
πάντα ῥεῖ

Reputation: 1

"Can anyone offer guidance on how to best modify my code so that int *coeff and int order are private members?"

Your code in main()

P1.display(P1.coeff, P1.order);

still tries to access these now private class members directly, which won't work of course (that's why we make these members private).

For your case, you don't need to pass these variables as parameters to that function at all, since the actual values are available for the P1 instance of class Poly already.

Change your member function signature to

int display();

and definition to

int Poly::display() {
    int i;
    int j;
    for (i = order; i >= 0; i--) {
        cout << coeff[i] << "x^" << i;
        if ((i - 1) != -1) {
            cout << "+";
        }
    }
    cout << "\n";
    return 0;
}

Note that accessing order or coeff inside the Poly::display() member function, can be read as equivalent to this->order and this->coeff.

Class member functions can be considered to have a this pointer implicitly, and can access (private) class member variables directly, without need to specify these as parameters.

After changing the stuff as proposed above, you can just call

P1.display();

Change other signatures/access to coeff, order analogous.


NOTE:

There are more flaws in your class declarations. Something like

void addition(Poly P1, Poly P2);

won't work well. You want to operate with the actual instance as the left hand value, and the other as a constant right hand value. IMHO your member function should look like this

Poly& addition(const Poly& rhs) {
     // perform addition operations with this and rhs
     return *this;
}

Poly addition(const Poly& rhs) const {
     Poly result = *this;
     // perform addition operations with result and rhs
     return result;
}

Upvotes: 2

Travis Gockel
Travis Gockel

Reputation: 27623

The idea of making something private means you're not allowed to access outside of member or friend functions (that's the purpose of private). The code is failing in main at things like P1.display(P1.coeff, P1.order) because main isn't a member of the Poly class and, as such, is not allowed to access Poly::coeff or Poly::order (as the compiler is telling you).

I would suggest you change the definition of Poly::display(int *coeff, int order) to not require you pass in coeff and order, since the instance already knows what these values are. I would also take out the return 0, since it isn't buying you anything.

void Poly::display()
{
    int i;
    int j;
    for (i = order; i >= 0; i--)
    {
        cout << coeff[i] << "x^" << i;
        if ((i - 1) != -1)
        {
            cout << "+";
        }
    }
    cout << "\n";
}

Inside that member function, the variables order and coeff are implicitly picking up the members of your class that were previously shadowed by the parameters you were passing.

Upvotes: 3

Related Questions