如何使一个类的公共成员私有? 我对编程(一般)和C++(尤其是)很有新意。我正在尝试获取以下公共成员变量并将其设置为私有: int *coeff; int order;

如何使一个类的公共成员私有? 我对编程(一般)和C++(尤其是)很有新意。我正在尝试获取以下公共成员变量并将其设置为私有: int *coeff; int order;,c++,class,private-members,C++,Class,Private Members,不幸的是,我看到以下错误: “Poly::coeff”:无法访问类“Poly”中声明的私有成员 及 “Poly::order”:无法访问类“Poly”中声明的私有成员 这是我的密码: #include "stdafx.h" #include <iostream> #include <cstdlib> using namespace std; class Poly { public: int *coeff; int order; int ge

不幸的是,我看到以下错误:

“Poly::coeff”:无法访问类“Poly”中声明的私有成员

“Poly::order”:无法访问类“Poly”中声明的私有成员

这是我的密码:

#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;
}
#包括“stdafx.h”
#包括
#包括
使用名称空间std;
类聚
{
公众:
int*系数;
整数阶;
int getData();
整数显示(整数*系数,整数顺序);
空穴添加(聚P1,聚P2);
空隙减法(多边形P1、多边形P2);
空洞倍增(多边形P1、多边形P2);
//~Poly();
};
整数多边形::显示(整数*系数,整数顺序)
{
int i;
int j;
对于(i=顺序;i>=0;i--)
{
不能按顺序;我--)
{
加上[i]=P1.系数[i];
}
对于(i=P2.order;i>=0;i--)
{
加上[i]=P1.系数[i]+P2.系数[i];
}
}
如果(P1.顺序P1.order;i--)
{
加上[i]=P2.系数[i];
}
对于(i=P1.order;i>=0;i--)
{
加上[i]=P1.系数[i]+P2.系数[i];
}
}
cout=0;i--)
{
sub[i]=P1.coeff[i]-P2.coeff[i];
}
}
如果(P1.订单>P2.订单)
{
对于(i=P1.order;i>P2.order;i--)
{
sub[i]=P1.系数[i];
}
对于(i=P2.order;i>=0;i--)
{
sub[i]=P1.coeff[i]-P2.coeff[i];
}
}
如果(P1.顺序P1.order;i--)
{
sub[i]=-P2.系数[i];
}
对于(i=P1.order;i>=0;i--)
{
sub[i]=P1.coeff[i]-P2.coeff[i];
}
}
cout=0;j——)
{
mult[i+j]+=P1.coeff[i]*P2.coeff[i];
}

cout将某物设置为私有的想法意味着不允许您访问成员或朋友之外的功能(这就是私有的目的)。代码在
P1.display(P1.coeff,P1.order)
之类的
main
中失败,因为
main
不是
Poly
类的成员,因此不允许访问
Poly::coeff
Poly::order
(正如编译器告诉您的那样)

我建议您更改
Poly::display(int*coeff,int-order)
的定义,不要求您传入
coeff
order
,因为实例已经知道这些值是什么。我也会拿出
返回0
,因为它不会给你买任何东西

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";
}
void Poly::display()
{
int i;
int j;
对于(i=顺序;i>=0;i--)
{

cout将某个
私有化的想法意味着不允许您访问成员或
朋友
函数之外的功能(这就是
私有化的目的)。代码在
main
中失败,例如
P1.display(P1.coeff,P1.order)
因为
main
不是
Poly
类的成员,因此不允许访问
Poly::coeff
Poly::order
(正如编译器告诉您的那样)

我建议您将
Poly::display(int*coeff,int-order)
的定义更改为不要求您传入
coeff
order
,因为实例已经知道这些值是什么。我还将取出
return 0
,因为它不买任何东西

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";
}
void Poly::display()
{
int i;
int j;
对于(i=顺序;i>=0;i--)
{
库特
“任何人都可以提供有关如何最好地修改我的代码以使int*coeff和int order成为私有成员的指导吗?”

您在
main()中的代码

现在仍然试图直接访问这些
私有
类成员,这当然不起作用(这就是为什么我们将这些成员设置为
私有

对于您的情况,您根本不需要将这些变量作为参数传递给该函数,因为实际值已经可用于
class Poly
P1
实例

将您的成员函数签名更改为

int display();
和定义

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;
}
将其他签名/访问权限更改为
coeff
顺序
类似


注意:

您的类声明中有更多的缺陷

void addition(Poly P1, Poly P2);
无法正常工作。您希望使用实际实例作为左侧值,另一个作为常量右侧值进行操作。IMHO您的成员函数应该是这样的

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;
}
“任何人都可以提供有关如何最好地修改我的代码以使int*coeff和int order成为私有成员的指导吗?”

您在
main()中的代码

现在仍然试图直接访问这些
私有
类成员,这当然不起作用(这就是为什么我们将这些成员设置为
私有

对于您的情况,您根本不需要将这些变量作为参数传递给该函数,因为实际值已经可用于
class Poly
P1
实例

将您的成员函数签名更改为

int display();
和定义

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;
}
将其他签名/访问权限更改为
coeff
顺序
类似


注意:

您的类声明中有更多的缺陷

void addition(Poly P1, Poly P2);
无法正常工作。您希望使用实际实例作为左侧值,另一个作为常量右侧值进行操作。IMHO您的成员函数应该是这样的

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;
}

对于C++新的人来说,这是一个很好的代码。也许从较小的程序开始。添加允许访问私有数据成员的成员函数。